fbpx

Get the Official WPSaviour App Now!

Tips

My Useful WordPress Snippets List By Remi Corson

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("[email protected]");

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: http://mysite.com/referrer/remi (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';

Download The WPSaviour App Now

Related posts
Tips

How To Use Pop-ups Without Harming SEO

Tips

10 WordPress Resources You Need to Bookmark Right Now

Tips

An Intro to the Anatomy of a WordPress Theme

Tips

Redesigning Your WordPress Website To Add a Personal Touch (The Sequel)

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published.