Skip to content
Theme Hybrid
·
WordPress Plugins and Themes

Primary

  • Exhale
  • Members
  • Blog
  • Support
  • Pricing

Theme setup function

All themes available on Theme Hybrid have what’s called a “theme setup function,” which is a function that lives within the theme’s functions.php file. There are a few reasons for having a theme setup function.

  • To organize your theme’s functions.php file code.
  • To properly place certain theme-specific bits of code.
  • To make sure the parent and child theme are fired in the correct order.
  • To make sure actions/filters are executed on the appropriate hook at the right time.

If none of that makes sense to you, don’t worry. Just trust that it’s the right way to do things. Even the default themes packaged with WordPress have theme setup functions.

If you really want to dive into advanced functions file management, check out this tutorial on functions files I wrote on my personal blog.

Creating the theme setup function

If you’re creating a parent theme from Hybrid Core, you must add the following code to your functions.php file.

/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'themename_setup' );

/**
 * Theme setup function.  This function adds support for theme features and defines the default theme
 * actions and filters.
 *
 * @since 0.1.0
 */
function themename_setup() {

	/* Get action/filter hook prefix. */
	$prefix = hybrid_get_prefix();

	/* Calls to add_action(), add_filter(), and add_theme_support() will go here. */
}

That’s pretty much it. We’ll dive into usage later in this tutorial.

Creating a child theme setup function

If you’ve downloaded a child theme from here, your child theme should already have a setup function. So, you shouldn’t have to worry about creating one.

However, if you’re building a custom child theme, you’ll need to write some code similar to the theme setup function code described earlier. In your child theme’s functions.php file, add the following code.

/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'childthemename_setup', 11 );

/**
 * Theme setup function.  This function adds support for theme features and defines the default theme
 * actions and filters.
 *
 * @since 0.1.0
 */
function childthemename_setup() {

	/* Get action/filter hook prefix. */
	$prefix = hybrid_get_prefix();

	/* Calls to add_action(), add_filter(), and add_theme_support() will go here. */
}

You might have noticed that the following line of code looks different for child themes.

/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'childthemename_setup', 11 );

Compare it to the parent theme code.

/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'themename_setup' );

As you can see, there’s a third parameter of 11 added to the child theme code. This is to make sure the child theme setup function is executed after the parent theme setup function. The parent theme is technically executed at 10 (the default).

If you really wanted to and had a good reason for doing so, you could change the 11 to 9 for your child theme setup function to execute before the parent theme setup function.

Using the theme setup function

The theme (and child theme) setup function should be used to hold calls to the add_action(), add_filter(), and add_theme_support() functions. The usage of these particular functions is outside the scope of this tutorial, but this is where you’d place them.

So, any time you want to add an action, filter, or feature, you’d add the code just after the following line in your theme setup function.

/* Calls to add_action(), add_filter(), and add_theme_support() will go here. */

Suppose you wanted to add some text to the wp_footer hook with a custom function (action). Your functions.php file and theme setup function would look something like the following.

/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'themename_setup' );

/**
 * Theme setup function.  This function adds support for theme features and defines the default theme
 * actions and filters.
 *
 * @since 0.1.0
 */
function themename_setup() {

	/* Get action/filter hook prefix. */
	$prefix = hybrid_get_prefix();

	/* Calls to add_action(), add_filter(), and add_theme_support() will go here. */
	add_action( 'wp_footer', 'themename_footer_text' );
}

/**
 * Function to output some footer text.
 *
 * @since 0.1.0
 */
function themename_footer_text() {
	echo '<p>This is some text to add to the footer.</p>';
}

As you can see, the call to add_action() goes within the theme setup function. However, the custom function (action) you created goes outside the theme setup function.

A note about function names

I suppose this is as good a place to mention this as anywhere. When you create function names, they should be prefixed with the name of your theme.

For example, if your theme name is “Awesome,” your function names should start with awesome_. So, if you were creating a theme setup function, you’d name it awesome_setup().

The reason to prefix function names like this is so that you don’t conflict with plugins or even WordPress itself, causing a pretty nasty fatal error on your site. When you add a unique prefix, you can generally rest assured that you won’t run into any problems like that.

Powered by the vast and endless void.

  • Subscribe
  • Facebook
  • Twitter
  • GitHub
  • WordPress.org