Hooks and filters in Swift

Swift includes various hooks and filters so that you can modify the output and functionality in child theme. This article will explain you everything you need to know about hooks and lists the hooks and filter included in Swift.

Using a hook

Let me explain you with an example. Suppose you want to display different ad to people from different countries below navigation. In this case you can not use the default built in AD management as it doesn’t support conditional ads. So, we hook a function to the swift_after_header hook.

/*
   We are here adding the function swiftchild_geo_ads to
   swift_after_header hook at priority 15. If nothing is given in
   third argument, it takes 10 by default
*/
add_action('swift_after_header', 'swiftchild_geo_ads', 15)

/**
 * This is just a dummy function for the sake of this tutorial.
 * There is no function like is_india() or is_usa() in php or WordPress. You
 * have to define your own functions to identify countries.
 **/
/**
 * This is just a dummy function for the sake of this tutorial.
 * There is no function like is_india() or is_usa() in php or WordPress. You
 * have to define your own functions to identify countries.
 **/
function swiftchild_geo_ads(){
    if( is_india() ){
        echo '<a href="http://SwiftThemes.Com/sales-pitch-for-india"><img src="http://SwiftThemes.Com/banner-with-indian-clients.png" /></a>';
    }
    elseif( is_usa() ){
        echo '<a href="http://SwiftThemes.Com/sales-pitch-for-usa"><img src="http://SwiftThemes.Com/banner-with-us-clients.png" /></a>';
    }

}

The above code will add a geo targeted banner below the navigation.

How to remove a function from a hook

The following code removes the ad below navigation that’s added by Swift.

/**
 * Here hook is swift_after_header, function that's hooked to it
 * is swift_nav_ad at a priority 12
 **/
remove_action('swift_after_header','swift_nav_ad', 12 );

But the above code wont work, because of the order in which the remove_action and add_action work. In the above case remove_action is first excecuted, and then the add_action. To fix this, we use a wrapper function.

/*
 * To remove any functions added to hooks by Swift, add them to this function.
 */

function unhook_swift_functions() {
    /*
     * remove_action('swift_header','swift_nav_below_logo', 20 );
     *
     * Don't forget the position number if the original function has one
     */
}
add_action('init','unhook_swift_functions');

Note: This wrapper function is necessary only for the hooks added by swift.

Context aware hooks

Typically, if you would like to display a banner below navigation on the home page, your hooked function would look something like this

add_action('swift_after_header','swift_homepage_ad');
function swift_homepage_ad(){
    if(is_home()){
        echo '<a href="http://SwiftThemes.Com/"><img src="http://SwiftThemes.Com/home-page-banner.png" /></a>';
    }
}

But with context aware hooks, the logic is handled for you. You can simply extent the hook swift_after_header to swift_after_header_home. So our code to achieve the above functionality would be

add_action('swift_after_header_home','swift_homepage_ad');
function swift_homepage_ad(){
    echo '<a href="http://SwiftThemes.Com/"><img src="http://SwiftThemes.Com/home-page-banner.png" /></a>';
}

As you see we don’t need a if condition to check if its home. You can extend these hooks to lot of things, please check the list below.

There will be several contexts per page, creating a cascading effect. Lets pick “swift_before_content” hook for example and see how that same hook on a single post with the ID of 100 would create four different hooks:

  • swift_before_content
  • swift_before_content_singular
  • swift_before_content_singular-post { You can replace post with the name of post types, like page, product etc..)
  • swift_before_content_singular-post-100

For archives

  • swift_before_content_archive

Taxonomy pages

Consider a tag or category with name foo, then the hooks for that page will be

  • swift_before_content_taxonomy
  • swift_before_content_taxonomy-tag
  • swift_before_content_taxonomy-category

Author archives

Author archives with user nice name satish will have the following hooks

  • swift_before_content_user
  • swift_before_content_user-satish

Time date archives

  • swift_before_content_date
  • swift_before_content_day
  • swift_before_content_week
  • swift_before_content_month
  • swift_before_content_year
  • swift_before_content_time
  • swift_before_content_hour
  • swift_before_content_minute

Other hooks

  • swift_before_content_search
  • swift_before_content_error-404

Swift theme hook reference

swift_before_html

This hook is executed immediately after the opening body tag <body>, located in header.php

swift_after_html

This hooks is executed before the closing body tag </body>, its located in footer.php

swift_before_header

This hook is executed between the opening #wrapper div and header-container div, its located in header.php.

swift_before_header_ad function is hooked to this at a priority 8

swift_header

This hook is used to output most of the header content. It is executed between <header id=”header”>[…]</header> in header.php. The following functions are hooked to it

Function                Priority
swift_nav_above_logo        8
swift_branding              15
swift_nav_below_logo        20

swift_after_header

This function is executed after the closing of header container tag. It is located in header.php

swift_nav_ad function is hooked to it at priority 12

swift_before_branding

This hook is located inside swift_branding function and is not available if you remove the swift_branding function from the swift_header hook. This is executed before displaying the logo/site name.

swift_after_branding

This hook is similar to the above one, its executed after displaying the logo/site name

swift_header_ad function is hooked to it at priority 8

swift_before_main

This hook is executed right after opening the #main div that encloses main content area and sidebar. Located in header.php

swift_after_main

This hook is executed before closing the main div. It’s located in sidebar.php

swift_before_content

It is executed right after opening the #content div, its located in index.php, 404.php, tag.php, archives.php etc..

This hook is extended using the context tags explained above and is used to add breadcrumbs when yoast wordpress seo plugin is active at priority 8

Hook                            Function                 Priority
swift_before_content_archive    swift_breadcrumb_output     8
swift_before_content_search     swift_breadcrumb_output     8
swift_before_content_singular   swift_breadcrumb_output     8

swift_after_content

This hook is executed right before the closing of content div and is located in the same files as the above one.

Note: The above two hooks are not available in full width page templates.

swift_before_footer

This hook is executed after the opening <footer> tag in footer.php.

swift_footer_ad is hooked to this with priority 8

swift_after_footer

It is executed after the closing </footer> tag in footer.php