Top ten WordPress tips that can do amazing things

WordPress is regarded as one of the most popular content management system . According to a latest research, an overwhelming majority of web projects are powered by WordPress . Being a WordPress programmer, I love experimenting different strategies and always wanted to come up with a guide to solve some of the trickiest issues in WordPress.

Are you a beginner ? Start coding your own blog with some useful tips from here then … Hey wait ! are you an expert in WordPress? Then take a look at it before deciding that this is not for you. Below are 10 tips that can add a pinch of salt to your WordPress excellency.

1. How to modify user roles?

Have you ever wondered how to change the default user roles (Administrator, Editor, Author, Contributor, Subscriber) , here is an excellent snippet . Hook your functions.php with a few lines of code (Edit line 5 & 6 at your convenience ).

function wps_change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$wp_roles->roles['contributor']['name'] = 'Blogowner';
$wp_roles->role_names['contributor'] = 'Blogowner';
}
add_action('init', 'wps_change_role_name');

2. Downsize your wordpress database

Heard about transients? Those are a simple and standardized way of temporarily storing cached data in the database by giving it a custom name and a timeframe after which it will expire and be deleted. But sometimes these transients make a lot of trouble by taking up a large space. Getting rid of this is a pretty simple task. The following code snippet will work like a charm:

Go to your phpmyadmin and run a simple sql command

“DELETE FROM `wp_options` WHERE `option_name` LIKE ('%\_transient\_%')”;

3. How to pre-populate WordPress editor with default content

Quite often you come up with a situation where all your posts repeated piece of content, well, here is an easy way to pre-populate your wordpress editor with some custom contents. Hook this snippet into your functions.php

add_filter( 'default_content', 'default_editor_content' );

function default_editor_content( $content ) {
global $post_type;
switch( $post_type )
{
case 'post':
$content = 'Some useful contents for blog posts.';
break;

case 'page':
$content = 'Some useful contents for pages.';
break;

case 'portfolio':
$content = 'Some useful contents for your portfolio pages.';
break;

}
return $content;
}

4. Show a warning on your posts that it is outdated

Do you feel like warning your readers about the post that they are reading is old? Here is the solution, paste the following code in your single.php file ( Or use it inside the loop if you have a custom page).

$posttime = get_the_time('U');
$time_in_seconds = ((time() - $posttime));
$days_old = (($days_old_in_seconds/86400));
// change it as per your requirement
if ($days_old > 365) {
echo 'WARNING : This post is one year old';
}

5. Improve your loading time

Some sites might really lack efficiency in terms of the loading time, here is a simple tip to reduce the loading time of your site. Move all the javascript files to the footer.

6. How to display recently registered users

If you have user registration on your site , there might be a time where you want to show a list of the recent users who have registered on your site ( Well thats a nice trick to attract more user registration ). Simply paste this code on a place where you can attract your new users .

<ul class="recently-user">
    <?php $usernames = $wpdb->get_results("SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
        foreach ($usernames as $username) {
                echo '<li>' .get_avatar($username->comment_author_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";
        }
    ?>
</ul>

This will display 5 users , change it if you want to.

7. Get your posts filtered with category ‘OR’ author

Recently I have come come across situation where I had to get posts filtering with categories or the author of posts , WordPress provides a way for filtering with category AND author, but when it comes to OR there is no default way we can do it, Though I had to write a custom code for that , it is pretty simple .

$query = "SELECT DISTINCT wposts.* FROM $wpdb->posts wposts LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE wpostmeta.meta_key = 'post_type' AND wposts.post_status = 'publish' AND wpostmeta.meta_value = 'post' AND $wpdb->term_taxonomy.taxonomy = 'category' AND ( $wpdb->term_taxonomy.term_id IN($cat_list) OR wposts.post_author IN ($authors) ) ORDER BY wposts.post_date DESC ";

$results = $wpdb->get_results($query);
foreach ( $results as $result ) {
//output results as desired
}

Here $cat_list is an array of id of categories and $authors is an array of id of authors.

8. Check if a post has a gallery

Sometimes this small piece of code can come in handy , lately, I came across a situaiton which demanded the use of this code snippet. You no longer need to skim through the stackoverflow, if you come across the same. Here we go:
paste it in functions.php

function hasgallery(){
   global $post;
   return (strpos($post->post_content,'

[gallery’) !== false);
}
9. Force specific pages to be SSL secure

When you have a webserver secured with SSL, you need to use this code to protect the respective blog. Activating SSL on specific pages on a WordPress blog is definitely easy.
Just add the following snippet to the functions.php

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
if ( $post_id == 25 ) {
return true
}
return $force_ssl;
}
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);
// change it with your post id

10. Crop uploaded images instead of scaling it

This is a very useful tip if you want to crop your uploaded images instead of scaling it. This is a simple yet powerful snippet , Paste it in your functions.php and rest assured.

// Standard Size Thumbnail
if(false === get_option("thumbnail_crop")) {
add_option("thumbnail_crop", "1"); }
else {
update_option("thumbnail_crop", "1");
}

// Medium Size Thumbnail
if(false === get_option("medium_crop")) {
add_option("medium_crop", "1"); }
else {
update_option("medium_crop", "1");
}

// Large Size Thumbnail
if(false === get_option("large_crop")) {
add_option("large_crop", "1"); }
else {
update_option("large_crop", "1");
}

That’s it . Hope all or at least a few of these WordPress tips were new to you. You can find most these information on codex already , and that should be your first refuge when starting your coding journey, though you will have a super guide gathering some interesting useful tips here. Have a great coding day!

Talk to our WordPress Professionals

Latest Post