How To Disallow Specific Shortcodes in Post Content

June 21, 2013 · 9 comments

by Lars

Shortcodes are one of the most powerful tools the team behind WordPress have added, and it is especially useful for inserting reusable content into pages and posts. It has been widely used since it was introduced and most modern themes and plugins have shortcodes included to make their use more flexible. Some plugins are complete packages of useful functions made available as shortcodes – check this collection to see yourself. They can be placed anywhere from post, pages and even widgets if enabled.

Shortcodes are powerful and there may be situations where admins need to control the use of specific shortcodes to avoid that they are inserted into pages and posts. One example of this could be on large websites where multiple author produce and publish content or where visitors are allowed to submit content. In this case, you do not want shortcodes used for website structure and potentially confidential content to be exposed by mistake. Justin Tadlock figured out a hack that I think you may find useful. It is a hack… and some will not like diving into such invasive measure, but it works and solved the problem.


ElegantThemes
ThemeForest

Advertisement

Below is a standard definition of two new shortcodes:

add_action( 'init', 'my_add_shortcodes' );
 function my_add_shortcodes() {
 add_shortcode( 'shortcode_a', 'new_shortcode_a' );
 add_shortcode( 'shortcode_b', 'new_shortcode_b' );
 }

If you want to disallow these shortcodes in posts and pages, you can add this code. Note that the priority on the add_filter is set to 0 to make it execute as early as possible:

add_filter( 'the_content', 'my_post_content_remove_shortcodes', 0 );
 function my_post_content_remove_shortcodes( $content ) {
 /* Create an array of all the shortcode tags. */
 $shortcode_tags = array(
 'shortcode_a',
 'shortcode_b',
 );
 /* Loop through the shortcodes and remove them. */
 foreach ( $shortcode_tags as $shortcode_tag )
 remove_shortcode( $shortcode_tag );
 /* Return the post content. */
 return $content;
 }

These code remove the shortcode inside the content area of the posts. However, the shortcodes are really removed here and if they are needed oustside the loop there is a problem. To enable the shortcodes again, just call it back by using this code. Again note the priority of the add_filter call. This is the hack. Remove with high priority and add back with low priority:

add_filter( 'the_content', 'my_post_content_add_shortcodes', 99 );
function my_post_content_add_shortcodes( $content ) {
 /* Add the original shortcodes back. */
 my_add_shortcodes();
 /* Return the post content. */
 return $content;
 }

Author : Lars Vraa

Lars is passionate about web design, web development, SEO, social media and loves to look into new technologies, techniques, tools etc. and to write articles for tripwire magazine readers.

{ 9 comments… read them below or add one }

Amit June 10, 2013 at 1:07 pm

Yes I did,

The problem is how can I define nested ones ? and then how to get their maximum depth, this is where I’m stuck…
Amit

Reply

Amit June 10, 2013 at 12:17 pm

Sorry! the code doesn’t paste well :( you can see it here – http://wordpress.org/support/topic/find-depth-of-nested-shortcodes?replies=10#post-4288759

Amit

Reply

Lars June 10, 2013 at 1:02 pm
Amit June 10, 2013 at 12:15 pm

Thanks!

I am trying to find the depth of nested [shortcodes] using a recursive PHP function, now – first I thought trying inserting the shortcodes into an array (like you see here) & finding the array depth somehow, then I realized the [shortcodes] tags are very similar to HTML tags so was looking into recursive regexp HTML parsing, so I am trying to do this now & would love to get some feedback –

0) {
echo "Shortcode found: ".count($elements)."";

foreach ($elements as $element) {
echo "Tpl node: ".$element($element->node)."";
}
}
}
?>

Thanks :)

Reply

Amit June 10, 2013 at 12:16 pm

0) {
echo “Shortcode found: “.count($elements).”“;

foreach ($elements as $element) {
echo “Tpl node: “.$element($element->node).””;
}
}
}
?>

Reply

Amit June 9, 2013 at 11:53 am

Thanks!

I’m trying to use your code and find the depth of the shortcodes array – can you please take a look and let me know if I’m on the right track:

add_action( ‘init’, ‘AddShortcodes’ );
function AddShortcodes() {
add_shortcode( ‘test’, ‘test’ );
add_shortcode( ‘test’, ‘test’ );
}
add_filter( ‘the_content’, ‘findDepth’, 0 );
function findDepth( $content ) {
/* Create an array of all the shortcode tags. */
$shortcode_tags = array(
‘test’,
‘test’,
);
}
function array_depth($array) {
$max_depth = 1;

foreach ($array as $value) {
if (is_array($value)) {
$depth = array_depth($value) + 1;

if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}

Reply

Lars June 10, 2013 at 12:11 pm

Hi Amid, I see a few issues with your code.
1) the add_shortcode function takes two params and you need to supply unique tags and functions. See here for reference http://codex.wordpress.org/Function_Reference/add_shortcode
2) Then before I dive more into the rest of the code I need to ask what it is you want to do and why. ;)

Reply

Mass Web Solutions June 9, 2013 at 11:32 am

Besides shortcodes it is also possible to use the widget system and it is just as powerful.

Reply

Lars June 10, 2013 at 11:57 am

I fully agree on this… however it require the theme have well placed widget areas

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post:


Web Analytics