WordPress hook: Automatically add a Google+ button to your posts

December 8, 2011 at 12:59 pm

Google+ is a new service from Google, which can bring traffic to your blog just like other social networking sites as such as Twitter or Facebook. Today, I’m going to show you a simple piece of code to automatically add a Google+ button to your posts.

Open your functions.php file and paste the following code in it:

add_filter('the_content', 'wpr_google_plusone');
function wpr_google_plusone($content) {
	$content = $content.'<div class="plusone"><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
	return $content;
}
add_action ('wp_enqueue_scripts','wpr_google_plusone_script');
function wpr_google_plusone_script() {
	wp_enqueue_script('google-plusone', 'https://apis.google.com/js/plusone.js', array(), null);
}

Once saved, a Google+ button will be automatically added to all your posts.

Thanks to Spyre Studios for the great piece of code!

WordPress hook: Automatically remove p tags on images

November 16, 2011 at 10:58 am

By default, WordPress embed images in <p> tags. This is a good thing in most cases, but in case you don’t want those p tags to show, here is a great way to remove theme without having to hack the WordPress core.

Simply paste the following code on your theme functions.php file, save it, and the <p> tags are gone.

function filter_ptags_on_images($content){
    return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'filter_ptags_on_images');

Thanks to James for the great hack!

Add a favicon to your WordPress blog using a hook

February 1, 2010 at 8:11 am

Nowadays, any serious blog have its own favicon. To add yours into WordPress, you can directly edit header.php or you can use a more clean technique, using the power of WordPress hooks.

Just paste the following into your function.php file, save it, and you’re done.

function childtheme_favicon() { ?>
	<link rel="shortcut icon" href="<?php echo bloginfo('stylesheet_directory') ?>/images/favicon.png" > 
<?php }
add_action('wp_head', 'childtheme_favicon');

Don’t forget to change the favicon url if needed. Also, please note that if the wp_head() function haven’t been implemented in your theme, this recipe will not work.

By the way, if you’re looking for premium WordPress themes and plugins, just have a look to the dedicated category on CouponsForBloggers.com!

WordPress hook: Disable posts auto saving

July 31, 2009 at 9:43 am

WordPress have a very useful function that autosaves your posts while you’re typing them in the dashboard. Though, if for some reason you’d like to disable this function, it is very easy to do. Just read on!

To disable WordPress autosaving function, simply open your functions.php file and paste the following function:

function disableAutoSave(){
    wp_deregister_script('autosave');
}
add_action( 'wp_print_scripts', 'disableAutoSave' );

That’s all. Once you saved the file, WordPress will never autosave a post. To enable autosaving again, simply delete this code.

Also, remember that if you need help with your WordPress blog, I can help for a reasonable price. Just send me an email and i’ll see what I can do for you.

List all hooked WordPress functions

June 24, 2009 at 9:26 am

WordPress hooks are very useful because they allow you to “surcharge” an existing WP function with your own code. But when things goes wrong, it should be useful to be able to list all hooked WordPress functions. Here is the code to it.

The first thing to do is to paste the following function in your functions.php file:

function list_hooked_functions($tag=false){
 global $wp_filter;
 if ($tag) {
  $hook[$tag]=$wp_filter[$tag];
  if (!is_array($hook[$tag])) {
  trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
  return;
  }
 }
 else {
  $hook=$wp_filter;
  ksort($hook);
 }
 echo '<pre>';
 foreach($hook as $tag => $priority){
  echo "<br />&gt;&gt;&gt;&gt;&gt;\t<strong>$tag</strong><br />";
  ksort($priority);
  foreach($priority as $priority => $function){
  echo $priority;
  foreach($function as $name => $properties) echo "\t$name<br />";
  }
 }
 echo '</pre>';
 return;
}

Once done, simply call the list_hooked_functions() function to print on the screen all hooked WordPress functions. Please note that this function is for debugging purposes only.

list_hooked_functions()

Thanks to Rarst for contributing to WpRecipes with this very useful function! If you’d like to contribute to WpRecipes, don’t hesitate to contact me.

11 Professional Email Templates from ChocoTemplates – only $12

July 28, 2014 at 2:01 pm

Having a nice, professional-looking email template can certainly increase your chances of member engagement. So jump on this Mighty Deal from ChocoTemplates now and you can get yourself 11 high-quality email templates to work with.

This bundle includes more than 100 color variations and 500+ layered PSD files worth of email templates that are compatible with most of today’s popular email clients. And for a limited time only, thanks to this deal, you can get them all for just a little more than $1 per template.

ChocoTemplates Email Template Bundle Highlights:

  • 11 Professional Email Templates:
    When you email customers and clients you want to look as professional as you can. And that’s exactly what you can do with this bundle of 11 different high-quality email templates from ChocoTemplates.
  • 100+ Color Variations:
    Changing up the colors on your emails is a snap with this template. Just choose the color you’d like from 110 different color variations and you’re good to go.
  • Highly Customizable (PSD option):
    Choose the PSD files version of this deal and you’ll get all the necessary Photoshop files to customize as you see fit. That’s 528 layered PSD files to easily find and change what you need.
  • Compatible with Popular Email Clients:
    These ChocoTemplates email templates have been road tested in all major email clients. So you know your message will get through to your subscribers no matter what they’re using to read your emails.
  • MailChimp Ready:
    Use MailChimp for your email management? Great! These templates are MailChimp ready, making integration ridiculously simple.
  • CampaignMonitor Ready:
    Integrate your emails with CampaignMonitor to easily keep an eye on the overall success. All email templates come CampaignMonitor Ready, so you can get them hooked up in no time flat.

This collection of email templates normally sells for $242, but for a limited time only, you can get all 11 email templates for just $12 – That’s a killer savings of 95% off the regular price, OR get all 11 email templates PLUS the invaluable PSD files for just $18, instead of the regular price of $352.

See all the templates

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 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 allow more HTML tags in WordPress comments

March 5, 2013 at 4:40 pm

By default, WordPress accept a few HTML tags in comments, such as <a>, <strong>, etc… But what if you want to be able to use more HTML tags in WordPress comments? Just read on, I have the solution!

Simply paste the following code in your functions.php file. The list of tags to allow can be modified on line 4.

// Create function which allows more tags within comments
function allow_pres() {
  global $allowedtags;
  $allowedtags['pre'] = array('class'=>array());
}

// Add WordPress hook to use the function
add_action('comment_post', 'allow_pres');

Thanks to David Walsh for the handy tip!

How to create custom dashboard help messages

July 13, 2012 at 4:01 pm

If you’re building a site for a client and they have some problems with some parts of the dashboard, a good idea is to provide contextual help to the client.
The following hack will allow you to add a custom help messages for the blog admin.

The code have to be pasted into your functions.php file in order to work. Please edit line 4 with the desired help message.

function my_admin_help($text, $screen) {
	// Check we're only on my Settings page
	if (strcmp($screen, MY_PAGEHOOK) == 0 ) {
		$text = 'Here is some very useful information to help you use this plugin...';
		return $text;
	}
	// Let the default WP Dashboard help stuff through on other Admin pages
	return $text;
}

add_action( 'contextual_help', 'my_admin_help' );

Thanks to Studio Grasshopper for the code!