How to Display Author’s Gravatar on Single Post in WordPress

Here’s a tiny WordPress code snippet which allows the developers to display Post Author’s Gravatar image on individual post. In this code we are using get_avatar() function.

<?php echo get_avatar( get_the_author_meta( 'ID' ), '80' ); ?>

Where the get_the_author_meta( ‘ID’ ) function will return author ID for the author of current post and 80 is the height and width of image the above code will generate. You can also pass Author’s email address as first parameter instead of Author’s ID.

Useful Read: How to Change Author URL Base in WordPress

Remove Author From Recent Comments Widget

By Default, WordPress recent comment widget displays the comment author name and the title of post where the comment was posted. But, this widget doesn’t come with an option in dashboard to remove comment author name from the widget.

However, you can easily fix this issue by adding a small amount of code to your functions.php file located inside your theme directory.

function ccw_remove_comment_author( $array ) {
	global $post;
	$array['user_id'] = !$post->post_author;
	return $array;
}
add_action( 'widget_comments_args', 'ccw_remove_comment_author' );

Read Also : Remove Pingbacks from Recent Comments Widget

How to Add REST API Support to WordPress Custom Post Types

In this tutorials, we are going to add WP REST API support to custom post types in our WordPress application.

Adding REST API support while creating custom post type:

While registering a custom post type, you want to make it compatible with REST API, you need add few additional arguments to the register_post_type function. The three arguments you need to pass are as follows;

  1. show_in_rest (Default: False) – Setting this argument to true will make your post type available to REST API.
  2. rest_base (Default: Post type’s name) – The base slug of the URL which will be used to access via REST API.
  3. rest_controller_class (Default: WP_REST_Posts_Controller ) – Specify a custom controller, which should be a subclass of WP_REST_Controller.

For example, I need to create a custom post type “Movie” with REST API support, my code will look something like this,

add_action( 'init', 'ccw_register_movies_post_type' );
function ccw_register_movies_post_type() {
	$labels = array(
	    'name'               => _x( 'Movies', 'post type general name', 'your-plugin-textdomain' ),
	    'singular_name'      => _x( 'Movie', 'post type singular name', 'your-plugin-textdomain' ),
	    'menu_name'          => _x( 'Movies', 'admin menu', 'your-plugin-textdomain' ),
	    'name_admin_bar'     => _x( 'Movie', 'add new on admin bar', 'your-plugin-textdomain' ),
	    'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
	    'add_new_item'       => __( 'Add New Movie', 'your-plugin-textdomain' ),
	    'new_item'           => __( 'New Movie', 'your-plugin-textdomain' ),
	    'edit_item'          => __( 'Edit Movie', 'your-plugin-textdomain' ),
	    'view_item'          => __( 'View Movie', 'your-plugin-textdomain' ),
	    'all_items'          => __( 'All Movies', 'your-plugin-textdomain' ),
	    'search_items'       => __( 'Search Movies', 'your-plugin-textdomain' ),
	    'parent_item_colon'  => __( 'Parent Movies:', 'your-plugin-textdomain' ),
	    'not_found'          => __( 'No movies found.', 'your-plugin-textdomain' ),
	    'not_found_in_trash' => __( 'No movies found in Trash.', 'your-plugin-textdomain' )
	);

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'movie' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest'       => true,
        'rest_base'          => 'movie',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'supports'           => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author', 'comments' )
    );
    register_post_type( 'movie', $args );
}

Adding REST API support to existing custom post type:

Sometimes you may want to add REST API support to a custom post type which is already registered, for example a custom post type which belongs to a theme. In this scenario, you can use global $wp_post_types to perform this task.

The example code for adding REST API support to an existing post type is as follows,

add_action( 'init', 'ccw_rest_apr_support_custom_post_type', 25 );
function ccw_rest_apr_support_custom_post_type() {
	global $wp_post_types;
	$post_type = 'movie';
	if( isset( $wp_post_types[ $post_type ] ) ) {
		$wp_post_types[$post_type]->show_in_rest = true;
		$wp_post_types[$post_type]->rest_base = $post_type;
		$wp_post_types[$post_type]->rest_controller_class = 'WP_REST_Posts_Controller';
	}
}

Read Also: How to Get Featured Image Thumbnail URL Using WP REST API?

How to Get Featured Image Thumbnail URL Using WP REST API

By default, WordPress REST API only returns featured image ID in form of featured_media field. However you can easily resolve this issue by adding a small amount of code to your functions.php file located inside root of your theme directory.

To get featured image URL in JSON response, use the following code.

add_action( 'rest_api_init', 'ccw_insert_thumbnail_url' );
function ccw_insert_thumbnail_url() {
    register_rest_field( 'post',
        'ccw_thumbnail',
        array(
            'get_callback'    => 'ccw_get_thumbnail_url',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

function ccw_get_thumbnail_url($post){
	if(has_post_thumbnail($post['id'])){
		$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post['id'] ), 'full' );
		$imgURL = $imgArray[0];
		return $imgURL;
	}else{
		return false;	
	}
}

The name of the field in the json response will be ccw_thumbnail. You can change second parameter of wp_get_attachment_image_src function to get the URL of image with a different image size.

Read Also : How to Add Active Class to Current Menu Item in WordPress

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 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