How to automatically add paragraph tags in WordPress

December 5, 2013 at 4:54 pm

By default, WordPress automatically add paragraph tags to the content and the excerpt, using the wpautop() function. If you need to automatically add some <p> tags to any text, you can use the function as shown in today’s example.

In order to add paragraph tags to any text, simply use the wpautop() function, as shown below:

$my_text = 'Lorem ipsum dolor sit amet
consectetur adipiscing elit. 
Nulla pretium libero eget gravida rutrum.';

echo wpautop( $my_text );

Thanks to Daniel Pataki for the tip!

WordPress tip: How to display recently registered users

November 29, 2013 at 7:23 pm

If you allow userregistration on your WordPress blog or website, what about displaying the 5 latest registered users? Here is a simple and efficient recipe to do so.

Simply paste the code below where you want to display recently registered users. This code will display 5 users, you can change this number on line 2.

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

Thanks to emoticode for the snippet!!

How to add custom text to WordPress login page

November 25, 2013 at 6:37 pm

If for some reason you need to display a custom message on WordPress login page, here is a quick and easy recipe to do it.

Nothing complicated, paste the code below in your functions.php file. Message can be customized on line 3.

function wps_login_message( $message ) {
    if ( empty($message) ){
        return "<p class='message'>Welcome to this site. Please log in to continue</p>";
    } else {
        return $message;
    }
}
add_filter( 'login_message', 'wps_login_message' );

Thanks to WP Snippy for the tip!

How to clean up wp_head() without a plugin

November 20, 2013 at 6:10 pm

WordPress adds a lot of stuff through wp_head() hook included in most WordPress themes. Some of this stuff is useful, but some other isn’t. Here’s a quick recipe to clean up the wp_head() easily without using a plugin.

Paste the following lines of code into your functions.php file:

remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );

Thanks to Noumaan Yaqoob for the code!

How to change contents of a dashboard help tab

November 5, 2013 at 5:09 pm

WordPress has a little tab in the top-right corner that, when clicked, drops down some contextual help. Here is a super useful function to hook different help text to different pages.

Simply paste the code below into your functions.php file.

//hook loading of new page and edit page screens
add_action('load-page-new.php','add_custom_help_page');
add_action('load-page.php','add_custom_help_page');

function add_custom_help_page() {
   //the contextual help filter
   add_filter('contextual_help','custom_page_help');
}

function custom_page_help($help) {
   //keep the existing help copy
   echo $help;
   //add some new copy
   echo "<h5>Custom Features</h5>";
   echo "<p>Content placed above the more divider will appear in column 1. Content placed below the divider will appear in column 2.</p>";
}

Thanks to WP Tuts for the tip!

How to disable automatic updates in WordPress 3.7

October 28, 2013 at 4:53 pm

In WordPress 3.7, there’s a new and interesting feature which allow your blog to automatically upgrade when a new version is available. I think it’s really cool, but if you don’t like it for some reason, here’s a quick recipe to turn it off.

To disable automatic updates, open your wp-config.php file and paste the following line in it:

define( 'AUTOMATIC_UPDATER_DISABLED', true );

Once you saved the file, automatic updates will be turned off and you’ll have to manually upgrade your blog again.

Log in a WordPress user programmatically

October 24, 2013 at 4:06 pm

When building WordPress based websites, it can be very useful to know how to programmatically log in an user. Here is a reusable function to do it easily.

Here is the function, drop it in your functions.php file:

function auto_login( $user ) {
    $username = $user;
    if ( !is_user_logged_in() ) {
        $user = get_userdatabylogin( $username );
        $user_id = $user->ID;
        wp_set_current_user( $user_id, $user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user_login );
    }     
}

Then, to log in a user, do the following:

auto_login( 'admin' )

Thanks evilsocket for the code!

Template Monster giveaway: 3 WordPress themes to win!

October 18, 2013 at 4:05 pm

It’s been a long time since out last giveaway! Today, I’m glad to offer 3 outstanding WordPress theme from our sponsor TemplateMonster. Joining the giveaway is free and easy, so join in!

A word about Template Monster

Template Monster is a world leader in the web template business, with over 20,000 templates available. They provide HTML templates as well as theme for CMS such as WordPress, Joomla, and others.
To learn more about Template Monster or browse available themes, just have a look to their website.

How to join the giveaway?

Joining the giveaway is free and easy. First, have a look to Template Monster themes and find the theme you’d like to win. Then, simply leave a comment below to let me know which theme you’d like to win.

In one week I’ll randomly pick 3 winners. They’ll receive the chosen theme directly from TemplateMonster.

Good luck everyone!

WordPress tip: Add a custom message to the editing pane

October 17, 2013 at 4:18 pm

When creating a WordPress site for a non tech-savvy user, it can be very useful to be able to display a custom message on the post editor page. Here’s a very simple hook to do it.

Copy the following snippet and edit the message on line 5. Once done, paste it on your functions.php file and save.

function wptutsplus_text_after_title( $post_type ) { ?>
    <div class="after-title-help postbox">
        <h3>Using this screen</h3>
        <div class="inside">
            <p>Use this screen to add new articles or edit existing ones. Make sure you click 'Publish' to publish a new article once you've added it, or 'Update' to save any changes.</p>
        </div><!-- .inside -->
    </div><!-- .postbox -->
<?php }
add_action( 'edit_form_after_title', 'wptutsplus_text_after_title' );

Thanks to WP Tuts for this great tip!

How to order posts by two meta values

October 15, 2013 at 4:20 pm

By default, WordPress allows you to sort results of a query by one meta value, but what if you need to sort results by two meta values (For example date and time)? Here ‘s a working example.

Paste the code below in your template file where you need to sort the results of the query.

<?php 

$query = "SELECT wposts.*, wpostmeta1.*, wpostmeta2.* 
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta1, $wpdb->postmeta wpostmeta2
WHERE wposts.ID = wpostmeta1.post_id
AND wposts.ID = wpostmeta2.post_id
AND wpostmeta1.meta_key = 'date'
AND wpostmeta2.meta_key = 'time'
ORDER BY wpostmeta1.meta_value ASC,
         wpostmeta2.meta_value ASC";

$results = $wpdb->get_results($query);

foreach ( $results as $result ) {
         //output results as desired
} ?>

Please note that this query is given as an example and might need to be adapted to fit your specific needs. If you need help at a cheap rate, contact WPCAT.