How to create a directory within the uploads folder

April 23, 2013 at 6:23 pm

When developing a plugin or theme, it can be useful to be able to programmatically create a directory within the wp-content/uploads folder. Here is a handy piece of code to do it.

Simply paste this code snippet on your functions.php file (or plugin file if you’re creating a plugin)

function myplugin_activate() {
    
    $upload = wp_upload_dir();
    $upload_dir = $upload['basedir'];
    $upload_dir = $upload_dir . '/mypluginfiles';
    if (! is_dir($upload_dir)) {
       mkdir( $upload_dir, 0700 );
    }
}
 
register_activation_hook( __FILE__, 'myplugin_activate' );

Thanks to Jean Galea for the snippet!

How to display an author bio excerpt on your WordPress blog

April 16, 2013 at 3:52 pm

WordPress offers users the option to add biographical info for each author. This bio can be displayed on your theme using the the_author_meta( ‘description’ ) function. But what if you’d like to display only a bio excerpt? Here’s a function to do it.

The first thing to do is to create the function. To do so, paste the following code into your functions.php file:

<?php
	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'));
		$authorDescription = explode(" ", get_the_author_meta('description'));
		$displayAuthorPageLink = count($authorDescription) > $word_limit ? $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>' : '' ;
		$authorDescriptionShort = array_slice($authorDescription, 0, ($word_limit));
		return (implode($authorDescriptionShort, ' ')).$displayAuthorPageLink; 		
	}
?>

Once done, simply call the function when needed, as shown below:

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

Thanks to Tim Marcher for the tip!

How to detect a comments page on your WordPress blog

April 15, 2013 at 4:04 pm

On WordPress dashboard, there’s an option to divide comments lists in sub pages. Unfortunately, there’s no built-in conditional tag to know if you’re currently on a comment page. So let’s built one!

Simply put the following code anywhere on your theme files. If you’re on a comment page, the conditional statement will return true, so any code within brackets will be executed.

$cpage = get_query_var( 'cpage' );
if ( is_singular() && $cpage > 0 ){
    // Your code here
}

This code works on posts, pages, attachments as well as all custom post_types.

Thanks to Daniel Roch for the cool tip!

How to force your WordPress blog to break out of frames

April 11, 2013 at 4:54 pm

Lots of scrapers are framing, defaming, and/or stealing your precious content. Here is a super useful snippet to force your blog to break out of frames so your pages will be served only from the original domain, not some Google adsense site created by a random scraper.

Nothing complicated, just paste the code below into your functions.php file, save it, and you’re done.

// Break Out of Frames for WordPress
function break_out_of_frames() {
	if (!is_preview()) {
		echo "\n<script type=\"text/javascript\">";
		echo "\n<!--";
		echo "\nif (parent.frames.length > 0) { parent.location.href = location.href; }";
		echo "\n-->";
		echo "\n</script>\n\n";
	}
}
add_action('wp_head', 'break_out_of_frames');

Thanks to WP Mix for this very handy tip!

How to redirect to post if search results only returns one post

April 4, 2013 at 4:41 pm

When a reader search your site using WordPress built-in search engine, the results are displayed as a list. Here is a snippet to improve the search engine by automatically redirecting the reader to the post if only one post is found by WordPress search engine.

Just paste the following code snippet into your functions.php file:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

Thanks to Paulund.co.uk for the snippet!

How to directly include Typekit fonts on your WordPress theme

April 1, 2013 at 4:36 pm

Typekit is a useful service which allow you to use countless different fonts on your website or blog. They do provide a WordPress plugin to easily add their fonts to your WordPress site, but there’s no built-in solution for integrating Typekit fonts directly to your theme. Here’s a recipe to do it.

Edit the code below and update the .js file path on line 2. Then, simple paste it on your functions.php file. Save the file, and you’re done!

function theme_typekit() {
    wp_enqueue_script( 'theme_typekit', '//use.typekit.net/xxxxxxx.js');
}
add_action( 'wp_enqueue_scripts', 'theme_typekit' );

function theme_typekit_inline() {
  if ( wp_script_is( 'theme_typekit', 'done' ) ) { ?>
  	<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<?php }
}
add_action( 'wp_head', 'theme_typekit_inline' );

Thanks to Devin for this useful tip!

Themes4All giveaway: 3 premium themes to win!

March 28, 2013 at 5:05 pm

Do you want a brand new premium theme for your WordPress blog? Today you have a chance to win a premium theme from our friends at Themes4All.com. Joining the contest is free and easy… Good luck!

About Themes4All.com

Our sponsor is Themes4All.com, which is a newcomer in the WordPress premium theme business. They specialize in low cost/high quality themes.
They already released 20+ themes, each priced $10. Click here to view the available themes.

How to join the giveaway?

Joining the giveaway is free and easy: First, go to Themes4All.com and create a free account. Registration grant you access to a free theme each month. In March, the free theme is Bryder.

Once done, simply add a comment to this post to let me know which theme you’d like to win. In one week (Thursday April 4, 2013), I’ll randomly pick 3 winners. Each of theme will receive the chosen theme by email, from the Themes4All.com staff.

Good luck everyone!

How to activate link manager on WordPress 3.5 (and newer)

March 25, 2013 at 5:14 pm

As of WordPress version 3.5 (and newer) they have left out the Link Manager link in the admin area. here is a quick recipe to bring it back!

Just paste this code into your functions.php file, and you’re done!

<?php

	//Activate the Link Manager built in to the WordPress admin
	add_filter( 'pre_option_link_manager_enabled', '__return_true' );

?>

Thanks to Tim Berneman for this tip!

How to only display the author posts in the admin post list

March 19, 2013 at 6:27 pm

When you’re running a multi-author blog, it can be very useful to allow each author to only see his own posts in the admin post list. Here is a simple code snippet to do it.

Paste the code below into your functions.php file. Once you saved the file, authors will only see their own posts in the admin post list.

<?php

function mypo_parse_query_useronly( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
        if ( !current_user_can( 'level_10' ) ) {
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}

add_filter('parse_query', 'mypo_parse_query_useronly' );

?>

Thanks to WP Snippets for the code!

LawyerWordPressThemes giveaway: Win an awesome theme for your blog

March 13, 2013 at 5:10 pm

Do you want to win an awesome premium theme for your blog? I’m glad to introduce our new giveaway in partnership with our friends from LawyerWordPressThemes, a premium theme company specializing in lawayer themes.

About Lawyer WordPress Theme

Our sponsor is LawyerWordPressThemes, a new WordPress premium theme seller which specialize in the law/attorney niche. But of course, the “Lawyer” theme can be used for any activity you want to. It’s a visually pleasant theme built on quality code.

How to join the giveaway?

Joining the giveaway is free and easy: Just leave a comment on this post to take part to the contest. In one week (wednesday March 20) I’ll randomly pick one lucky winner and I’ll send him a coupon code that will allow him to get the theme for free. That’s it! Good luck everyone!