My WordPress plugin creates a custom post_type and the custom page which uses that post_type displays regular posts (of post_type=”post”) filtered to only show those that have been assigned to the same Category as the custom post_type page itself.

Then I created a custom shortcode intended to run Javascript code that is ONLY loaded on pages of my custom post_type.  So the shortcode is basically useless everywhere else on the blog, and consequently I don’t want it to show the custom shortcode wrapper around that content.  In fact I want the user, in my plugin’s admin page, to decide whether they want the shortcode content to show up at all when the regular posts are viewed outside of my custom post_type.

My first thought was to simply use Conditional Tags to limit showing the shortcodes to my custom post_type pages.  It works if the shortcode is in the body text of the custom post_type page itself, but if the shortcode is inside a regular post being displayed on a custom post_type page it doesn’t work (because as far as it’s concerned it is “in” a regular post type, not a custom post type).

I went off searching for other conditional tag and filtering options and the search wasn’t bearing any fruit.  However I did stumble upon some posts about using a conditional tag to set a global variable and then using the variable as a stand-in check inside the shortcode function.  With that I was able to only execute the shortcode function on my custom post_type pages except then the actual shortcode text (i.e., [myshortcode]….[/myshortcode]) would display around the content on every other part of the blog.  Yuck!

Next I tried moving my shortcode function and registration off the plugin.php page and into the single-custom.php which also worked in the sense that the shortcode ONLY processed on my custom post_type pages, but again left the actual shortcode code text in the post everywhere else.

Then it occurred to me that maybe I could declare how the shortcode works generally in my plugin file and redefine it in my custom page file.  So in my plugin.php file:

function my_custom_shortcode( $atts, $content = null ) {
 return $content;
add_shortcode(‘myshortcode’, ‘my_custom_shortcode’);

You might wonder “What is the point of that?!”  Well, it prevents the actual shortcode code (the stuff in square brackets) from showing up on pages elsewhere on the blog.  Essentially it is a shortcode that just echoes the content of the shortcode back to the page untouched.

Then in my single-custom.php page I redefine it locally:

function my_shortcode_in_page( $atts, $content = null ) {
‘link’ => ‘#’,
‘id’ => ”
), $atts));
$wrapped ='<div class=”shortcode” id=”‘.$id.'”><span class=”shorty”><a href=”‘.$link.'”>’.$content.'</a></span></div>’;
return $wrapped;
add_shortcode(‘myshortcode’, ‘my_shortcode_in_page’);

Now it shows the  unaltered content in regular blog pages but shows my shortcode wrapper on my custom post_type pages – and ONLY those pages!  But then what if the $content is something that, itself, is kind of useless or odd on pages that won’t use the shortcode?  For example if you use an image thumbnail for the shortcode link it wouldn’t be a link at all on other pages, so why have it there at all?  So I added a field to my plugin’s Options page to let the end user decide if they wanted [myshortcode] content to be hidden elsewhere on the blog.  All that does is set up a variable in the database that is part of the plugin initialization (it checks for the value and sets the global variable).  Then the function in my plugin.php only needs a simple check:

function my_custom_shortcode( $atts, $content = null ) {
if ( $hide_myshortcode === “1” ) {
// do nothing //
} else {
return $content;

Now, if the user set $hide_myshortcode variable to “1” the shortcode returns nothing, which hides whatever is inside the shortcode tag.  If the variable equals some other number (the Options page toggles it between “0” and “1”) then the content inside the shortcode is returned.