WordPress is a specially crafted platform to publish content on the web. Many of the big giant content publishers are using WordPress like CNN, NYTimes, etc. You, as a content writer, find WordPress deadly easy and super cool to work on because WordPress provides many customizations to make your content looks good, and also many of us are spending time to add some spice to WordPress. Here I want you to add even more to your content writing experience.
Following are the three most wanted WordPress tricks to make your writing experience easy and even better.
How to restrict the authors to view only the comments which belong to their own posts in the admin area?
Just open up functions.php of your current theme and put this code in it, but keep in mind if you put this code in your functions.php will only be limited to your current theme. So better to make a plugin.
require( ABSPATH . WPINC . '/pluggable.php' ); function myplugin_get_comment_list_by_user($clauses) { if (is_admin()) { global $user_ID, $wpdb; $clauses['join'] = ",". $wpdb->base_prefix."posts"; $clauses['where'] .= " AND ".$wpdb->base_prefix."posts.post_author = ".$user_ID." AND ".$wpdb->base_prefix."comments.comment_post_ID = ".$wpdb->base_prefix."posts.ID"; }; return $clauses; }; // ensure that editors and admins can moderate everything if(!current_user_can('edit_others_posts')) { add_filter('comments_clauses', 'myplugin_get_comment_list_by_user'); }
As you know the comments are stored in a separate table and are identified by post_id. So, I have just modified the query which is run by WordPress whenever the comments page opened in the admin.
How to set the first image of the post as a featured image?
Sometimes, we need to set our post’s first image as a featured image and WordPress doesn’t give any support for this thing. You can use the following code snippet to get this functionality.
add_filter('the_content', 'set_post_featured_image'); function set_post_featured_image($content) { global $post; if (!has_post_thumbnail()) { $all_attached_images = get_children(array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order' )); if ($all_attached_images) { foreach ($all_attached_images as $attached_image) { set_post_thumbnail($post->ID, $attached_image->ID); break; } $content = the_post_thumbnail() . $content; } } return $content; }
I am applying a filter to the contents of the post and the job done.
How to retrieve your Gravatar image?
Having a profile picture on your profile means great credibility and recognition on social networks and other websites, which is usually known as “avatar image”.
WordPress provides its own service know as Gravatar. We will walk you through that how to retrieve your profile image from Gravatar.
WordPress has a built-in function to retrieve an image from Gravatar, this function required two parameters: ID or usre_email of the user and the size of an image.
$user_id = get_the_author_meta('ID'); echo get_avatar($user_id, 80);
If you want to use user email, use get_the_author_meta() function with user_email
$user_id = get_the_author_meta('user_email'); echo get_avatar($user_id, 80);
This will give us an image of 80px.
If you know any other way of doing these tricks for WordPress, let us know in the comments below.