My Useful WordPress Snippets List By Remi Corson – WPSaviour

By WP Saviour •  Updated: 09/28/20 •  4 min read

As a WordPress developer i take advantage of fairly often the identical snippets to keep away from shedding time when creating a brand new plugin. And right this moment is nice day simply because i’m going to share a few of them with you. The snippets beneath will not be associated and you should use them in your personal customized plugins and themes.

Encoding Emails and avoiding  spam

To begin i’d such as you to introduce you a easy a fantastic snippet utilizing a WordPress native operate referred to as: antispambot(). It’s very straightforward to make use of nevertheless it’s very helpful. The intention is to encrypt emails which are displayed on the frontend to keep away from spambots to catch them and spam you or tour purchasers. Simply place the e-mail as a operate parameter and WordPress does the job:

echo antispambot("[email protected]");

Change “Enter Title Right here” placeholder

Subsequent, while you add a brand new submit, a brand new web page or a brand new customized submit kind, the title discipline has a placeholder that claims “Enter title right here”. I actually like when the placeholder textual content takes into consideration the submit kind you’re including. For instance if it’s a ebook, the placeholder ought to present “Enter Guide Title Right here”. To take action, use this code:

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

Merely outline the submit kind.

Verify if a plugin is energetic

I created a number of plugins dependent of BBpress. As i didn’t need my plugins to load if BBpress was lacking i take advantage of this code. So easy, however so helpful!

include_once( ABSPATH . 'wp-admin/contains/plugin.php' ); if (is_plugin_active('plugin-directory/plugin-file.php')) { }

Displaying date utilizing the WordPress default format

In practically each plugin or theme this isn’t completed correctly. Sure, i’m speaking about the best way dates are displayed. As many builders do themes or plugins in English they use the US date format, when Europeans use these things date aren’t not appropriately formatted. So, a easy option to show all dates with the identical format is to make use of the format retailer in your personal WordPress set up choices utilizing the snippet beneath. That’s to say:

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

Get a person ID by its login

The opposite day i’ve been requested to create a referrer plugin for WordPress. My shopper wished the urls to be kind of formatted like that: (the place, clearly, “remi” is the referrer). To take action, i used the rewrite guidelines (see my earlier submit), and likewise a little bit operate to retrieve the person ID from his login:

$my_user = '';$person = get_user_by('login', $my_user );
$user_id = $person->ID;

Get a media URL (by its ID)

Fundamental however good, right here is a straightforward option to get the URL of an attachment:

wp_get_attachment_url( $id );

Routinely add content material to each submit

That is an instance of find out how to add any kind of content material to each submit of your WordPress set up. You’ll be able to for instance use this methodology so as to add content material to feed solely, on posts that match some standards and many others…

operate add_post_content($content material) { if(!is_feed() && !is_home()) { $content material .= '
copyright '.date('Y').'

'; } return $content material; } add_filter('the_content', 'add_post_content');

Echo a shortcode out of the loop

This one of many extra properly no snippets, nonetheless as i’m requested practically on a regular basis find out how to do it, right here is find out how to show the content material of a shortcode out of the loop:

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

Show “human time”

And to complete the record of snippets, a fast operate that lets you show time like Twitter of Fb do: “11 min in the past”, “1 hour in the past”, “yesterday” and many others…

echo human_time_diff(get_the_time('U'), current_time('timestamp')).' in the past';

gp-6160741 as-1304644

WP Saviour

I am a WordPress specialist. My mission is to help you create beautiful websites with ease!