• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2784

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Consultation
  • Full Access
  • Log in

Hooks and Filters Guide for Beginners

In this beginners guide we’ll take a look at some basic usage of hooks and filters. I’ll also provide some working examples which you can use or modify for use on your own site.

Hooks and filters are included in both the WordPress files and some theme frameworks.

Hooks

Hooks are included in both the WordPress core files and some parent theme files like the Genesis theme framework. They enable you to hook in content in a specific location in the file.

An example is the wp_head hook in WordPress. You can use this hook in your child theme to add content in that location.

The hook works in any theme as its a WordPress hook and not specific to any theme.

Example:

add_action('wp_head', 'add_content_to_head');
function add_content_to_head() {
echo 'Your Content';
}

Here’s a list of WordPress hooks you can use in any theme including Genesis.

Some premium themes like Genesis also include action hooks which you can use in a child theme to do the same thing.

Here’s a visual map which includes all the the action hooks and the position they output your content in the Genesis theme framework.

An example is the genesis_header hook in Genesis. You can use this hook in an child theme which runs on the Genesis theme framework to add content in that location.

The hook only works in child themes built on Genesis and will not work in any other theme.

Example:

add_action('genesis_header', 'add_content_to_header');
function add_content_to_header() {
echo 'Your Content';
}

Here’s what the hook looks like if you opened the header.php file in the Genesis theme framework:

do_action( 'genesis_header' );

Filters

Filters enable you to modify the output of an existing function and are included in both the WordPress core files and some parent theme frameworks like Genesis.

Here’s a list of the filters you can only use with StudioPress or other child themes which run on the Genesis framework.

Here’s a list of the filters included in WordPress.

Here’s an example of how you can use a WordPress filter in any child theme including all official StudioPress and Community themes:

add_filter( 'comment_author_says_text', 'custom_comment_author_says_text' );
function custom_comment_author_says_text() {
return 'author says';
}

The above code can be used in a child theme to modify the author says text in your comments. It works in any theme.

Here’s another example which customizes the length of excerpts to 50 words:

add_filter( 'excerpt_length', 'change_excerpt_length' );
function change_excerpt_length($length) {
return 50; 
}

Here’s an example of using a filter which only works in Genesis child themes.

add_filter( 'genesis_footer_creds_text', 'custom_footer_creds_text' );
function custom_footer_creds_text() {
	echo '<div class="creds"><p>';
	echo 'Copyright &copy; ';
	echo date('Y');
	echo ' &middot; <a href="http://mydomain.com">My Custom Link</a> &middot; Built on the <a href="http://www.studiopress.com/themes/genesis" title="Genesis Framework">Genesis Framework</a>';
	echo '</p></div>';
}

This filter is added to your child themes functions.php to enable the customization of the footer credit links and copyright without editing the core Genesis footer file.

You can see this filter has been added to the genesis/lib/structure/footer.php and looks like this in the file:

$creds_text     = apply_filters( 'genesis_footer_creds_text', $creds_text );

You can also use hooks and filters in plugins to do exactly the same thing and the code won’t be lost when you update the parent theme or WordPress.

Basically, hooks and filters enable you to customize and modify both WordPress and your parent theme without editing the core WordPress files or your parent theme files.

Its significantly easier to customize a child theme when the parent theme framework includes hooks and filters because you never need to edit the parent themes template files or WordPress core files. This way you can safely change themes as well.

Note: You can use WordPress hooks and filters in child themes running Genesis but you cannot use Genesis hooks and filters in themes not running Genesis.

Learn More About Hooks & Filters

  • Guide To Using Hooks In Custom Functions
  • Action Hooks For Parent Theme Development
  • What is a Hook? Basic Guide For Beginners

Reader Interactions

Comments

  1. Seb Gates says

    August 23, 2013 at 9:13 am

    Hi Brad, could you answer me a question about Hooks and Filters? If I remove post meta information like author, date, tags and categories using a remove_action, will this remove the information for Google bot when it crawls the site? Or does it simply remove it from the front end view? I have a few sites that I removed the meta info from but I would still like Google to index the tags.

    Log in to Reply
    • Brad Dalton says

      August 23, 2013 at 7:15 pm

      Hi Seb

      In that case use CSS code as then it will still output in the source code.

      Otherwise PHP generally removes the HTML output that it creates in the source code.

      Google scans the source code.

      Log in to Reply
  2. Juan Colome says

    August 15, 2013 at 12:28 am

    invalid link for your hooks and filters 🙁

    Log in to Reply
    • Brad Dalton says

      August 15, 2013 at 9:02 am

      Which one Juan?

      I tested them and they all work for me.

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags