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.

Genesis Framework Simple Reference Guide

In Genesis Framework Simple Reference Guide, Let’s start with a small intro about Genesis framework. Genesis framework is most flexible WordPress theme with more customization option. Genesis framework let you develop a site on WordPress in a way you want it. If you prefer WordPress for development this framework will give you more power and flexibility to develop the website. You can create your own customized theme using Genesis framework.

Genesis Framework Simple Reference Guide

 

Genesis framework is one-time purchase.

So, it’s worth buying and use it for multiple site development

Genesis Framework Simple Reference Guide

Genesis framework’s customization based on the hooks, filters. Using these options you can develop a website right from the scratch.

Hooks –   Inject the desired code at desired place without changing any WordPress default file.

Filters – allow you to modify/decorate  the existing function

Download and install the Genesis Theme and also install the child theme for Genesis and activate it.

Do all the customization in child themes function.php

This will keep the parent theme clean.

You can put your custom contents section in the required place by creating/registering the sidebar and  hook it in function.php ( child theme)

Refer the below image for the Genesis Hook guide

Genesis Framework Simple Reference Guide

the hook guide will help you to understand the names given to refer section of the website. Use this hook name to replace it with required custom contents.

Registering sidebar in Genesis

If you want to put your custom HTML code or content in place of the hook names, first you need to register the sidebar. The registered sidebar will appear in the Appearance – > Widget section of the WordPress.

Once the register sidebar appears in the widget section drag drop the require widget to the corresponding sidebar. You can also insert custom content blocks to the registered sidebar. These content block let you add custom codes.

genesis_register_sidebar( array(
 'id'          => 'menu_header_right',
 'name'        => __( 'Menu - Header Right', 'custom_name' ),
 'description' => __( 'custom_description.', 'custom_name' ),
) );

Add the above code snippet to the child themes function.php. Now you can see the new sidebar available in widget page with the name mentioned in the code.

Genesis Framework Simple Reference Guide

Hook the registered sidebar

After registering the sidebar,  we need to determine the place where the appropriate sidebar needs to be hooked.

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>',
  ));
 }
}

This piece of code will hook the custom sidebar in the place of  “genesis_header_right”  as mentioned in the hook guide. With the help of  ‘before’ & ‘after’ attribute, you can wrap side bar with a custom HTML & class for styling.

Genesis Filters

Filters in Genesis tends access to modify the result of the existing function. I will explain the filter concept with the example.

In a breadcrumb, the default separator will be “/” and the breadcrumb will look like “Home/some_page” If I want to modify the default separator of the breadcrumb from “/” to “>” which can be achieved by filters.

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

Genesis general settings

Apart from the custom hooks & filters Genesis framework has some general settings.

  • Default site layout
  • Section for adding logo image
  • Breadcrumb options
  • Comment and Track Backs
  • content archive
  • Blog page template
  • Header / footer scripts.

These titles itself reveal the core purpose of the settings.

Genesis framework simple reference guide gave a basic idea about implementation for more hooks and filters visit the link below.

Most common hooks in genesis framework used for website development are available here.

Ready made themes also available in Genesis framework. Check it out!

Password protection to Website using htaccess

Password protection to Website using .htaccess is much easier than you ever think. This .htaccess authentication is not only for protection. As a developer, we always have multiple staging and dev environment before production. So authenticating those web servers with .htaccess is recommended.  Especially authenticating website with .htaccess will protect your environment from unauthorized access and unwanted SEO crawlings.

password protection htaccess

The site protected with htaccess will prompt for the authentication whenever it gets the hit. 

You can setup this .htaccess authentication within two simple steps.

  • Create encrypted password file.
  • Configure and map the password file in .htaccess

Creating an encrypted password file

First of all, create a new password file in the document root. This password file will hold all username with the respective password in encrypted format. Each encrypted username and password will be in new line. You can create your own encrypted username and password set without relying on any online tools.

$ htpasswd -nbm username Password

Copy the generated encrypted password to the password file.

 “myName:$2y$05$LwGdq19WrsD9w0VzFg0ZuetbL5/bw.S7yyDsjj1rXS13buZuWRBNW”

Note: Give desired random file name to the password file preceded with “.” .

htpasswd command executable is found in the apache/bin if you are using XAMPP in local dev machines. 

 

Configure and map the password file in .htaccess. 

We are almost done, the next thing is to add the below mention lines to your .htaccess file in the document root.

AuthType Basic
AuthName "My Protected Area"
AuthUserFile /absolute/path/to/password/file
Require valid-user

Note:

  • in addition use the full absolute path in the AuthUserFile . If you use relative path it will result in 500 Server error
  • If you use relative path / password file is not accessible  will result in 500 Server error

As a result, you can see an authentication popup whenever you hitting the URL freshly.

htaccess authentication

 

Know furthermore about the encryption options available.

Know furthermore about the htpasswd attributes