Get the Official WPSaviour App Now!

Tips

My Useful WordPress Snippets List By Remi Corson

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!

// Embody if use on the frontend, not wanted within the admin
include_once( ABSPATH . 'wp-admin/contains/plugin.php' );
	
if (is_plugin_active('plugin-directory/plugin-file.php')) {
     //plugin is activated
}

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: http://mysite.com/referrer/remi (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 = ''; // may be $_GET['user'] or query_vars
$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 .= '<p>copyright '.date('Y').'</p>';
	}
	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';

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogTips

Top 5 free payment gateway plugins for WordPress in India (2020)

Beginner’s GuideBlogTips

Best WooCommerce International Payment Gateways for WordPress in 2020

Tips

How To Use Pop-ups Without Harming SEO

Tips

An Intro to the Anatomy of a WordPress Theme

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published.

Fear Of Missing Out?

Sign up with your email address to receive WordPress tips and updates
SUBSCRIBE NOW
Terms and Conditions apply
close-link
Click Me