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.


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

WordPress hack: Redirect to a custom page after registration

October 9, 2013 at 4:33 pm

Here is a very handy code snippet to redirect the user to a custom page after registration on your WordPress blog or website. Super useful for those using WP as a CMS!

Simply drop this snippet in your functions.php or a plugin.

function __my_registration_redirect(){
    return home_url( '/my-page' );
add_filter( 'registration_redirect', '__my_registration_redirect' );

Thanks TheDeadMedic for the tip!

WordPress tip: How to get the first link in post

October 7, 2013 at 3:41 pm

Remember when I shown you how to get the first image from a post? Today, I’m going to let you know how you can easily grab the first link from the post content.

Put the function below in your functions.php file, then use it inside the loop in your template files.

function get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );

    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );

Thanks to Filip Stefansson for the tip!

By the way, I just created a new service called WPCAT, where I offer my help to fix or enhance your WordPress site for a cheap rate. Don’t wait and contact me right now if you need any help with WordPress!

How to change author url base on your WordPress site

October 2, 2013 at 4:23 pm

In WordPress, author profile are by default accessible using the url yoursite.com/author/name. But what if you want to use the term “profile” instead of “author” in the url? Here is a handy recipe to do so.

Pasting the following code on your functions.php file will change the default yoursite.com/author/name to yoursite.com/profile/name.
Replace profile on line 4 by any slug you want.

add_action('init', 'cng_author_base');
function cng_author_base() {
    global $wp_rewrite;
    $author_slug = 'profile'; // change slug name
    $wp_rewrite->author_base = $author_slug;

Thanks to Kevin Chard for the cool tip!

WordPress tip: author bio excerpt

September 26, 2013 at 7:07 pm

Would you like to be able to show a short intro of the author bio instead of his complete bio? Here’s a code snippet to make an excerpt of the bio, with a link to the author page, where you can show the full bio.

Let’s start by creating the function. The code below have to be pasted into your functions.php file.

	function author_excerpt (){	                     					
		$word_limit = 20; // Limit the number of words
		$more_txt = 'read more about:'; // The read more text
		$txt_end = '...'; // Display text end 
		$authorName = get_the_author();
		$authorUrl = get_author_posts_url( get_the_author_meta('ID'));
		$authorDescriptionShort = wp_trim_words(strip_tags(get_the_author_meta('description')), $word_limit, $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>');
		return $authorDescriptionShort; 		

Once done, you can use the function. To do so, use the code below where you’d like to display an author bio excerpt:

<?php  if (function_exists('author_excerpt')){echo author_excerpt();} ?>

Thanks to Tim Marcher for submitting this recipe!

How to paginate WordPress like Dribbble

September 24, 2013 at 5:46 pm

Are you using Dribbble? If you checkout a user on Dribbble there are two shots or thumbnails listed on the right hand side that will paginate the previous or next shot uploaded by the user. Here is a recipe to replicate this functionality on your WordPress site.

Just paste the code below on your single.php file, where you want to display the dribbble-like pagination:

<?php $prev = get_previous_post(); $next = get_next_post();
	<div class="float--left folio">
		<a href="<?php echo get_permalink($prev->ID); ?>" title="<?php echo esc_attr($prev->post_title); ?>">
			<?php echo get_the_post_thumbnail($prev->ID, 'thumbnail'); ?>
	<div class="float--right folio">
		<a href="<?php echo get_permalink($next->ID); ?>" title="<?php echo esc_attr($next->post_title); ?>">
			<?php echo get_the_post_thumbnail($next->ID, 'thumbnail'); ?>

This recipe has been submitted by Elliott Richmond. Thanks!

Remove “Plugins” and “Other News” widgets from WordPress dashboard

September 17, 2013 at 5:02 pm

If for some reason you don’t like to see the “plugins” and “other news” dashboard widget, here’s a way to remove them using some easy WordPress hooks.

Simply paste the code below in your functions.php file. Once you saved the changes, the “plugins” and “other news” widgets will not be shown again.

//Remove unwanted widgets from Dashboard
function remove_dashboard_widgets() {
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');

Thanks to WP Guru for the code!