How to Add Active Class to Current Menu Item in WordPress

By default, WordPress adds current-menu-item class to current navigation menu item. This is very useful for writing CSS code. However, sometimes you may want to add your own custom class to current menu item. You can easily perform this task by adding a small amount of code to your functions.php file located inside your theme directory.

add_filter('nav_menu_css_class' , 'ccw_add_active_class_to_nav_menu' , 10 , 2);
function ccw_add_active_class_to_nav_menu($classes, $item){
     if( in_array('current-menu-item', $classes) ){
             $classes[] = 'active ';
     }
     return $classes;
}

Recommended : Integrating Facebook Login into Laravel Application

How to Add Custom Post Types to RSS Feeds in WordPress

In order to add custom post types to your RSS Feeds copy the following code snippets and paste it inside your functions.php file located inside your theme’s folder.

function add_cpts_to_rss_feeds($query) {
	if (isset($query['feed']) && !isset($query['post_type']))
	{
                $query['post_type'] = get_post_types();
        }
	return $query;
}
add_filter('request', 'add_cpts_to_rss_feeds');

The above code will all the custom post types on your website to your RSS feeds.

Quick Tip: Time ago implementation in PHP

If you want to add only a specific posts to your RSS feeds use the following code snippet.

function add_cpts_to_rss_feeds($query) {
	if (isset($query['feed']) && !isset($query['post_type']))
        {
		$query['post_type'] = array('post','movie','show','song');
        }
	return $query;
}
add_filter('request', 'add_cpts_to_rss_feeds');

As you can see, along with default post we have added movie, show and song post types to the feeds.

How to Add Categories and Tags for WordPress Pages

By default, WordPress doesn’t associate any taxonomy with pages. However, you can easily add default tag and category taxonomies to pages.

Read Also: Applying Categories, Tags and Custom Taxonomies to Media Attachments

Paste the following code into your functions.php file to add categories and tags to WordPress pages.

function ccw_add_category_and_tag_to_pages() {
	//add tags to pages
	register_taxonomy_for_object_type( 'post_tag', 'page' );
	//add categories to pages
	register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'ccw_add_category_and_tag_to_pages' );

We are using register_taxonomy_for_object_type function to perform this task.

How to Embed Facebook Status in WordPress

Facebook is the largest social networking website in the world, which has more than 1 billion active users across the globe.  Millions of exciting messages, photo and news shared on Facebook everyday.

Facebook allows you to embed this information directly into your website.

In this post, we are going to embed a Facebook status in out WordPress blog post. To embed a status into the blog post, follow the steps given below.

Laravel Tip : Rendering a View to a String

1. Click the date or time link which will take you to that page of that specific status

How to Embed Facebook Status Posts in WordPress

How to Embed Facebook Status Posts in WordPress

2. Copy the URL from the address bar.

How to Embed Facebook Status Posts in WordPress

How to Embed Facebook Status Posts in WordPress

3. Paste the URL inside you post editor

How to Embed Facebook Status Posts in WordPress

How to Embed Facebook Status Posts in WordPress

And that’s it.

Here’s an example of embedded status in WordPress blog post,

https://www.facebook.com/coffeecupweb/posts/1596784437264447

How To Remove Capital P Dangit Filter From WordPress

By default, WordPress comes bundled with a filter hook called capital_P_dangit which is used to fix incorrect capitalization. Whenever you type the word “WordPress” with a lowercase p it automatically converts that p into an upppercase P.

Must Read : How to Add Page Slug in Body Class of your WordPress Themes

However, if you prefer not to use this feature, you can remove it by adding a small amount of code to your functions.php file located inside your themes folder.

remove_filter( 'the_title', 'capital_P_dangit' );
remove_filter( 'the_content', 'capital_P_dangit' );
remove_filter( 'comment_text', 'capital_P_dangit' );

For more information : https://codex.wordpress.org/Function_Reference/capital_P_dangit

Using after_setup_theme hook in WordPress

after_setup_theme is an action hook in WordPress which is called on every page after the theme initialization process is completed. Primarily, this hook is used to:

  1. Perform basic theme setup and registration.
  2. Override parent theme functionality via child themes.

Usage:

add_action( 'after_setup_theme', 'ccw_theme_setup_callback' );

function ccw_theme_setup_callback() {
	/* Add your custom hooks or theme support */
}

In the above code, ccw_theme_setup_callback is the callback function which is executed each time after_setup_theme called. You can use this function to register your custom hooks or to add theme support for a specific feature.

For example,

add_action( 'after_setup_theme', 'ccw_theme_setup_callback' );

function ccw_theme_setup_callback() {
        /* Add Theme Support */
	add_theme_support( 'post-thumbnails' );

        /* Register Navigation Menu */
        register_nav_menu( 'top', __( 'Top Menu', 'ccw-theme' );
        
        /* Register theme sidebar */
        add_action( 'widgets_init', 'ccw_register_sidebar' );
}

function ccw_register_sidebar() {
        /* Code to register your sidebar */
}

In this example, We are adding theme support for post thumbnails, registering our navigation menu and executing ccw_register_sidebar function which allow you to register a sidebar.

You can also use this hook to override or remove parent theme’s action via child theme.

add_action( 'after_setup_theme', 'ccw_remove_parent_action' );

function ccw_remove_parent_action()
{
         remove_action( 'widgets_init', 'footer_sidebar_init' );
}

The above code will remove footer_sidebar_init action after theme is loaded.

Quick Tip : How to Show Total Number of Attachments in WordPress

How to Add a Class Name to WordPress Post Thumbnail

WordPress allows you to display post thumbnail or featured image of a post by using the_post_thumbnail() function. But, sometimes adding a post thumbnail is not enough. You may want to add your own custom CSS class to the image tag generated by this function. You can easily do that by passing an array of parameters to the function.

Quick Tip : How to convert a negative number to a positive number

<?php the_post_thumbnail('full', array('class' => 'class-name')); ?>

For more information : https://codex.wordpress.org/Function_Reference/the_post_thumbnail

How to Display Total Products Count in WooCommerce

This a handy WordPress code snippet which can be used to display the total products count in your WooCommerce shop. To do this, we are going to use WP_Query. 

Useful Read: How to Hook Into WooCommerce to Trigger Something After an Order is Placed

$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1 );
$products = new WP_Query( $args );
echo $products->found_posts;

 

Creating Custom Shortcodes For Contact Form 7

With more than 30,000,000 downloads, contact form 7 is one of the most popular contact form plugins available for WordPress. It allows you to build multiple forms and customize them.

Useful Read : How To Disable Directory Browsing in WordPress?

In this post, I am sharing a snippet which can be used to build a custom contact form 7 shortcode for your form. To create a custom shortcode, we are going to use wpcf7_add_shortcode() function.

//Contact Form Shortcode
wpcf7_add_shortcode('postdropdown', 'vks_createbox', true);
function vks_createbox(){
	global $post;
	$args = array( 'post_type' => 'motorcycle', 'posts_per_page' => -1 );
	$myposts = get_posts( $args );
	$output = "<select name='cursus' id='cursus'><option>--Select a Model--</option>";
	foreach ( $myposts as $post ) : setup_postdata($post);
			$title = get_the_title();
			$output .= "<option value='$title'> $title </option>";
	endforeach;
	$output .= "</select>";
	return $output;
}

The above code will create a shortcode [postdropdown], which generates a select box with custom post type’s (motorcycle’s) title as its options.