In this article we will follow up important WordPress Theme Development functions and hooks available inside it. This article is written keeping theme development guidelines in mind, mentioned at WordPress Developer Doc and WordPress Codex .

01. wp_head() function

Just before closing head tag ( </head> ) we need to call wp_head() function.
This function actually provides you a WordPress action to work on. If you go to the definition of the function wp_head() you will find do_action( ‘wp_head’ ) .

So in simple terms after calling wp_head(), you are free to add styles, scripts, custom-css code, tracking-code, analytic-code, etc to your theme’s head.

Sample code :

add_action( 'wp_head', 'add_custom_code_to_theme_head', 10 );
function add_custom_code_to_theme_head() {
	/* your custom code here */
}

02. wp_footer() function

Similarly we have wp_footer() function. Just before closing body tag ( </body> ) we need to call wp_footer() function. If you go to the definition of the function wp_footer() you will find do_action( ‘wp_footer’ ) .

So after calling wp_footer(), you are free to custom code to your theme’s body.

Sample code :

add_action( 'wp_footer', 'add_custom_code_to_theme_footer', 10 );
function add_custom_code_to_theme_footer() {
	/* your custom code here */
}

03. body_class() function

One more important function to take care of is body_class(). This function must be called inside your opening body tag ( <body> ). Like this : <body <?php body_class(); ?>>

This function is responsible of adding default classes to your body tag. Using these class you can easily identify the post-id, post-type, etc. These class comes handy when you are planning to style different post-types or pages differently.

Sometimes there comes a situation when you need to add your custom class in a specific condition. WordPress as always has taken care of that for you. When you go deep inside body_class() function, there exists a WordPress filter to alter the class as per your requirement. $classes = apply_filters( ‘body_class’, $classes, $class );

Sample code :

add_filter( 'body_class', 'alter_body_class', 10 );
function alter_body_class( $classes ) {
	if ( true ) { /* your condition here */
		$classes[] = 'your-custom-class';
	}
	return $classes;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.