Most Common Genesis Hooks and Filters – [WordPress]

I  will list all the most common Genesis hooks and filters used by me while developing a website without boring you with more contents.

For a basic overview of Genesis framework read here.

Genesis hooks filtersWhile customizing a WordPress,  use a child theme to make all your changes so your parent theme will remain clean.

Below mentioned are the common tasks achieved by Genesis hooks and filters while developing a website

  • Change number of columns in Footer.
  • Registering the custom sidebar
  • Hooking registered side bar to appropriate section
  • Change the default position of the primary menu
  • Remove the site title for Frontpage
  • Change the Copyrights content
  • Modify Breadcrumb Prefix and Format
  • Create a new custom post type.
  • Add the custom post to archive query.
  • Remove comments section on Custom post type

Add the following code snippets to the function.php of Genesis child theme.

Change number of columns in Footer

This line will be already there in the function.php. Increase or decrease the footer column count. You can see the respective number of footer sidebars registered in the widget area.

add_theme_support( 'genesis-footer-widgets', 3 );

Registering the custom sidebar

This code snippets let you create and register a custom sidebar which allows you to add the widgets to your website

genesis_register_sidebar( array(
 'id'          => 'Unique_id_for_custom_sidebar',
 'name'        => __( 'Name for Custom sidebar', 'theme_name' ),
 'description' => __( 'Description for custom sidebar', 'theme_name' )
));

Genesis Hooks to pull registered side bar to appropriate section

This Genesis hook method allows you to replace the sidebar content in the place of declared hook section
Refer here for Complete hook Guide.
You can add custom HTML wrapper to the custom sidebar with the help of “before” and “after” parameter.

add_action( 'genesis_header_right', 'menu_header_right');
function menu_header_right() { 
 if (is_active_sidebar( 'menu_header_right' ) ) { 
   genesis_widget_area( 'menu_header_right', array( 
    'before' => '<div class="menu_header_right widget-area"><div class="wrap">',
    'after' => '</div></div>', ) );
 }
}

Change the default position of the primary menu

These Genesis hooks allow you to change the default position of the primary menu by removing it from default position and add it to the appropriate section using the hook name.

here I remove the primary menu from its default position and add it to genesis_header_right

remove_action( 'genesis_after_header','genesis_do_nav' ) ;
add_action( 'genesis_header_right','genesis_do_nav' );

Remove the site title for Frontpage

Remove the title section of the page if it is front page using condition and remove action.

add_action( 'get_header', 'remove_title_frontpage' );
 function remove_title_frontpage() {
  if ( is_front_page()) {
  remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
 }
}

Change the Copyrights content

Here am used a filter to change the content of the existing footer credits function. Replace the echo command content with the appropriate HTML code.

add_filter( 'genesis_footer_creds_text', 'footer_creds_text' );
function footer_creds_text () {
	echo 'Custom HTML Code';
}

Modify Breadcrumb Prefix and Format

Modify the args[‘sep’] value to change the breadcrumb separator. You can also add the prefix to the breadcrumb. Here am adding a single space for better visibility and readability.

add_filter( 'genesis_breadcrumb_args', 'custom_breadcrumb' );
function custom_breadcrumb( $args ) {
  $args['labels']['prefix'] = '';
  $args['sep'] = ' > ';
  return $args;
}

Create a new custom post type

By creating a custom post type you can have a separate post section under WordPress admin menu with given name. The below code snippet will create custom post type “News” and add it to the WordPress admin menu. Change the values from “News” to require name if you want to create your own post type

add_action( 'init', 'create_custom_post_news' );

function create_custom_post_news() {

	$labels = array(
			'name' => __( 'News' ),
			'singular_name' => __( 'News' ),
			'all_items' => __('All News'),
			'add_new' => _x('Add new News', 'News'),
			'add_new_item' => __('Add new News'),
			'edit_item' => __('Edit News'),
			'new_item' => __('New News'),
			'view_item' => __('View News'),
			'search_items' => __('Search in News'),
			'not_found' =>  __('No News found'),
			'not_found_in_trash' => __('No News found in trash'),
			'parent_item_colon' => ''
			);

			$args = array(
			'labels' => $labels,
			'public' => true,
			'has_archive' => true,
			'exclude_from_search' => false,
			'menu_icon' => 'dashicons-admin-post',
	    'rewrite' => array('slug' => 'news'),
	     'taxonomies' => array( 'category', 'post_tag' ),
	      'supports'  => array( 'title', 'editor', 'thumbnail' , 'custom-fields', 'excerpt', 'comments', 'genesis-cpt-archives-settings' )
	 );

		register_post_type( 'news', $args);

}

Add the custom post to archive query

The newly created custom posts are not available in the search result of the website.  Add the newly created post to the archive with the following code.
Add the name given in register_post_type to the $query->set( ‘post_type’, array()) seperated by coma

add_action('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','news') );
  }
}

Remove comments section on Custom post type

Here I removed the comment section for the custom post type news.

add_action( 'init', 'remove_custom_post_comment' );

function remove_custom_post_comment() {
    remove_post_type_support( 'custom_post_name1', 'comments' );
}

These are the most commonly used hooks and filters while developing the normal website. Start developing your WordPress site with Genesis framework.