Skip to content →

Category: WordPress Snippet

Highlight WordPress nav menu item based on current post

wp_nav_menu() function automatically generates css class for styling current menu item or current menu ancestor item. But in both cases your current post/page/custom post type item has to be included in the navigation menu as top menu item or sub-menu item respectively. What if your single post/page/custom post type is not included in the navigation menu but you still want to highlight it’s post type? Let me give you an example. Lets assume we have a custom post type called “Event” and its archive page is included in our primary navigation menu as a Link. Whenever we will navigate to…

Leave a Comment

Redirect a specific user role to a different location after login in WordPress

A small yet very handy code snippet to redirect user to a different location by user role after a successful login in WordPress .   function zt_login_redirect( $redirect_to, $request, $user ) { // check if there is any user if ( is_array( $user->roles ) ) { // look for the specific user role we want to redirect i.e subscriber if ( in_array( ‘subscriber’, $user->roles ) ){ return home_url( ‘/your/redirect/location/’ ); } } } add_filter( ‘login_redirect’, ‘zt_login_redirect’, 10, 3 );  

Leave a Comment