Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

My Useful WordPress Snippets List By Remi Corson

February 5, 2013

As a WordPress developer i use very often the same snippets to avoid losing time when creating a new plugin. And today is great day just because i’m going to share some of them with you. The snippets below are not related and you can use them in your own custom plugins and themes.

Encoding Emails and avoiding  spam

To start i’d like you to introduce you a simple a great snippet using a WordPress native function called: antispambot(). It’s very easy to use but it’s very useful. The aim is to encrypt emails that are displayed on the frontend to avoid spambots to catch them and spam you or tour clients. Just place the email as a function parameter and WordPress does the job:

echo antispambot("");

Change “Enter Title Here” placeholder

Next, when you add a new post, a new page or a new custom post type, the title field has a placeholder that says “Enter title here”. I really like when the placeholder text takes into account the post type you’re adding. For example if it’s a book, the placeholder should show “Enter Book Title Here”. To do so, use this code:

function change_default_title( $title ){
     $screen = get_current_screen();
     if  ( '_your_custom_post_type_' == $screen->post_type ) {
          $title = 'The new title';
     return $title;
add_filter( 'enter_title_here', 'change_default_title' );

Simply define the post type.

Check if a plugin is active

I created a few plugins dependent of BBpress. As i didn’t want my plugins to load if BBpress was missing i use this code. So simple, but so useful!

// Include if use on the frontend, not needed in the admin
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if (is_plugin_active('plugin-directory/plugin-file.php')) {
     //plugin is activated

Displaying date using the WordPress default format

In nearly every plugin or theme this isn’t done properly. Yes, i’m talking about the way dates are displayed. As many developers do themes or plugins in English they use the US date format, when Europeans use these items date aren’t not correctly formatted. So, a simple way to display all dates with the same format is to use the format store in your own WordPress install options using the snippet below. That’s to say:

date( get_option('date_format'), strtotime( $date ) )

Get a user ID by its login

The other day i’ve been asked to create a referrer plugin for WordPress. My client wanted the urls to be more or less formatted like that: (where, obviously, “remi” is the referrer). To do so, i used the rewrite rules (see my previous post), and also a little function to retrieve the user ID from his login:

$my_user = ''; // can be $_GET['user'] or query_vars
$user = get_user_by('login', $my_user );
$user_id = $user->ID;

Get a media URL (by its ID)

Basic but nice, here is a simple way to get the URL of an attachment:

wp_get_attachment_url( $id );

Automatically add content to every post

This is an example of how to add any type of content to every post of your WordPress install. You can for example use this method to add content to feed only, on posts that match some criteria etc…

function add_post_content($content) {
	if(!is_feed() && !is_home()) {
		$content .= '<p>copyright '.date('Y').'</p>';
	return $content;
add_filter('the_content', 'add_post_content');

Echo a shortcode out of the loop

This one of the more well no snippets, however as i’m asked nearly everyday how to do it, here is how to display the content of a shortcode out of the loop:

echo do_shortcode('[shortcode option1="value1" option2="value2"]'); 

Display “human time”

And to finish the list of snippets, a quick function that allows you to display time like Twitter of Facebook do: “11 min ago”, “1 hour ago”, “yesterday” etc…

echo human_time_diff(get_the_time('U'), current_time('timestamp')).' ago';
Article by Remi guest author
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.


  1. AJ Clarke | WPExplorer

    Thanks for sharing these snippets!

    For the Get a user by the ID we also have the function “wp_get_current_user()” is there a reason you don’t use that?

    • Remi

      Actually wp_get_current_user() allows you to retrieve data of the current logged in user, but get_user_by() doesn’t take into account if the user is logged in or not. These two functions are really different and do not offer the same benefits. As i explained in the post, get_user_id() is an ideal solution for a referrer system, where wp_get_current_user() wouldn’t work.

  2. Daniel Jones (@SimpleSites4U)

    Great post, the antispam one is very cool indeed, thanks

    • Remi

      Hi, yes the antispambot() is a must have function! 😉

  3. TiborP

    Thanks for sharing these! I didn’t know the title placeholder replacement yet.

    And if I may shamelessly plug myself: I actually wrote a plugin using the antispambot() function, so you can easily use it in posts/pages with a button or shortcode. You can find it here:

  4. Aurélien Denis

    Awesome list! I especially love the one about the title placeholder. 🙂

  5. BoiteAWeb (@BoiteAWeb)

    Hello Rémi,
    date( get_option(‘date_format’), strtotime( $date ) )
    date_i18n( get_option(‘date_format’), strtotime( $date ) )

    and for $screen->post_type
    i use $GLOBALS[‘typenow’] (or global $typenow;)

    See you !

    • Remi

      Yes you’re right! Thanks for the heads up!

  6. Rami

    I realy like the antispambot() function. Thanks for sharing.

  7. Shawn

    Remi, can ask what code you are using for the Author Bio + social…etc above? Clean and slick.

  8. Colin Murphy

    Hi Remy,

    A very good list.

    I would also like to suggest wp_trim_words as another useful function for WordPress in relation to creating custom excerpts for content that is not post content such as meta data.

    • AJ Clarke | WPExplorer

      Oh ya, wp_trim_words() is one of those functions I use on a daily basis as well. Combined with strip_shortcodes()

      • Remi

        There’s also wp_html_excerpt()

      • Colin Murphy

        Thanks AJ and Remi, I never knew those functions existed.

        I would also use apply_filters(‘the_content’, $html) after trimming the content.

  9. Mathew Porter

    Thanks, some great little snippets here to reference instead of trawling through reems of code or my memory.

  10. Jeremy

    Thanks super usefull article.
    For info the “See all” link in the author bio is broken, returning the homepage 🙁

    • AJ Clarke | WPExplorer

      Yes we are aware of the broken link. Unfortunately there is a bug in WordPress SEO that keeps coming back with each update. We’ll fix it up as soon as we get a chance. Thanks for letting us know though!

  11. masum65

    Thank you for this nice code collection. It is helpful for me like other.Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.