You are here:Home » WordPress » Plugin Shortcode Conflicts in WordPress

Plugin Shortcode Conflicts in WordPress

We all have experienced this: you buy a premium theme that is really great, and you also buy an amazing plugin (one of mine for example!) but putting the theme and the plugin together just doesn’t work at all. So what? Is the plugin or the theme having bugs?
Most part of the time, when this happens it’s when you are using shortcodes. Let’s imagine you just bought a plugin to handle “testimonials”. You read carefully the documentation and you know that to display the testimonials you need to include the following shortcode [testimonials]. But when you do so, nothing appears.
The reason why the shortcode isn’t replaced by testimonials, is because your theme AND the plugin define the same exact shortcode.
As plugins files are loaded before themes files they are overwritten and not fully taken into account in this case.
That’s why i wanted to show you a simple method to deregister the theme shortcode and replace it by the plugin’s one.

Step 1: Create A Simple Plugin

We need to create a simple plugin:
<?php
/*
Plugin Name: Avoid Shortcodes conflicts
Plugin URL: http://remicorson.com
Description: A little plugin to avoid conflicts bewteen shortcodes
Version: 1.0
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: rc_asc
Domain Path: languages
*/

Step 2: Check If The Shortcode Exists

This is the most important step. It’s where you check for the existence of a shortcode. WordPress has a “$shortcode_tags” global variable that store the list of all registered shortcodes. So, we just need to go through this variable and check if the shortcode we are looking for makes part of it.
/**
 * Check 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 ) {
	
	global $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;

}
This is the generic function that we will use in a more specific function including the shortcode slug to look for. This function returns TRUE if the shortcode exists, or FALSE if it doesn’t.

Step 3: Remove The Shortcode & Register The New One

The next function is using the rc_asc_shortcode_exists() we just created. It simply check for the existence of the shortcode, replaces it if it exists, or add the shortcode if it’s not already registered.
/**
 * Check if a shortcode is already registered and replace 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 Function

All you have to do is to define the content of the “my_testimonials_shortcode()” function, and you’re done!
/**
 * Creates the new shortcode
 *
 * @since 1.0
 *
 * @return void
 */
function my_testimonials_function() {

	return 'this replaces the previous shortcode!';
}
As you can see the previously declared shortcode is now replaced by the right shortcode.

0 comments:

Post a Comment