Disable “Install the WooThemes updater plugin to get updates”

Here’s a single line code which allows you to get rid of annoying WooCommerce admin notices i.e “Install the WooThemes updater plugin to get updates for your WooThemes plugins”.

All you need to do is copy the following code snippet and paste it inside your functions.php file located inside your themes directory.

remove_action('admin_notices', 'woothemes_updater_notice');

Something you may not know: How to Hook Into WooCommerce to Trigger Something After an Order is Placed

WordPress Asks for FTP Details While Installing Plugins

When you setup a WordPress on Ubuntu Linux and try to install a new plugin from plugin repository it prompts you for FTP Details.

With a few Google searches I managed to find a solution for this problem. To fix this issue all you need to do is run the following command on your terminal.

sudo chown -R www-data:www-data /[folder-path]/[name-of-your-project-folder]

You can also fix this issue by adding following line to your wp-config.php file.

define('FS_METHOD', 'direct');

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

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 Allow SVG Through WordPress Media Uploader

Here’s a reusable piece of PHP code which allows the users to upload SVG files via WordPress media uploader. All you have to do is copy the following code and paste it inside the functions.php file located inside your themes directory.

function ccw_upload_svg_files($mimes) {
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}
add_filter('upload_mimes', 'ccw_upload_svg_files');

Read Also: Restricting Users to View Only their own Media Library Items

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