Get the Official WPSaviour App Now!

BlogTutorials

How to Avoid Theme & Plugin Shortcode Conflicts in WordPress

All of us have expert this: you buy a premium theme that is really good, and in addition you moreover buy an incredible plugin (definitely one in every of mine as an illustration!) nevertheless inserting the theme and the plugin collectively merely doesn’t work the least bit. So what? Is the plugin or the theme having bugs? Lets study to Steer clear of Theme & Plugin Shortcode Conflicts

Most part of the time, when this happens it’s when you end up using shortcodes. Let’s take into consideration you merely bought a plugin to take care of “testimonials”. You study fastidiously the documentation and also you acknowledge that to point out the testimonials it’s advisable embody the following shortcode [testimonials]. Nevertheless everytime you obtain this, nothing appears.

The rationale why the shortcode isn’t modified by testimonials, is because of your theme AND the plugin define the equivalent exact shortcode.

As plugins recordsdata are loaded sooner than themes recordsdata they’re overwritten and by no means completely considered on this case.

That’s why i needed to point you a straightforward method to deregister the theme shortcode and substitute it by the plugin’s one.

Step 1: Create A Simple Plugin

We’ve to create a straightforward plugin:

<?php
/*
Plugin Establish: Steer clear of Shortcodes conflicts
Plugin URL: http://remicorson.com
Description: A bit plugin to stay away from conflicts bewteen shortcodes
Mannequin: 1.0
Creator: Remi Corson
Creator URI: http://remicorson.com
Contributors: corsonr
Textual content material Space: rc_asc
Space Path: languages
*/

Step 2: Take a look at If The Shortcode Exists

That’s essential step. It’s the place you confirm for the existence of a shortcode. WordPress has a “$shortcode_tags” world variable that retailer the guidelines of all registered shortcodes. So, we merely should bear this variable and confirm if the shortcode we’re looking out for makes part of it.

/**
 * Take a look at if a shortcode is already registered
 *
 * @since 1.0
 *
 * @param $shortcode string The shortcode slug to test
 *
 * @return void
 */
function rc_asc_shortcode_exists( $shortcode = false ) {
	
	world $shortcode_tags;

	//echo '<pre>'; var_dump($shortcode_tags); echo '</pre>';
 
	if ( ! $shortcode )
		return false;
 
	if ( array_key_exists( $shortcode, $shortcode_tags ) )
		return true;
 
	return false;

}

That’s the generic function that we’re going to use in a additional specific function along with the shortcode slug to seek for. This function returns TRUE if the shortcode exists, or FALSE if it doesn’t.

Step 3: Take away The Shortcode & Register The New One

The following function is using the rc_asc_shortcode_exists() we merely created. It merely confirm for the existence of the shortcode, replaces it if it exists, or add the shortcode if it’s not already registered.

/**
 * Take a look at if a shortcode is already registered and substitute it
 *
 * @since 1.0
 *
 * @return void
 */
function rc_asc_replace_shortcode() {

	$shortcode = 'testimonials';
	
	if( rc_asc_shortcode_exists( $shortcode ) ) {
		remove_shortcode( $shortcode );
		add_shortcode( $shortcode, 'my_testimonials_function' );
	} else {
		add_shortcode( $shortcode, 'my_testimonials_function' );
	}
}

Step 4: Define The New Shortcode Carry out

All you need to do is to stipulate the content material materials of the “my_testimonials_shortcode()” function, and in addition you’re executed!

/**
 * Creates the model new shortcode
 *
 * @since 1.0
 *
 * @return void
 */
function my_testimonials_function() {

	return 'this replaces the sooner shortcode!';
}

As you presumably can see the beforehand declared shortcode is now modified by the becoming shortcode. That’s how we’ll stay away from theme & plugin shortcode conflicts

 

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogShowcase

7 Best Shopify Alternatives in 2020 (Cheaper and More Powerful)

Beginner’s GuideBest WordPress PluginsBlog

14 Best AI Chatbots Software for Your Website (Comparison)

Beginner’s GuideBlogTips

Magento vs WooCommerce – Which one is Better? (Comparison)

Beginner’s GuideBlogTips

12 Best Calculator Plugins for Your WordPress Site

Sign up for our Newsletter and
stay informed

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