A WordPress shortcode that ignores content

Comments is a common feature of all programming languages. Apart from their obvious original purpose, they offer coders another ability: They can temporarily deactivate a piece of code by commenting it out, rather than completely deleting it.

Why shouldn’t authors have the same ability? Why shouldn’t they be able to deactivate a piece of content, so it doesn’t render, but without having to completely delete it?

This is exactly what our plugin will provide: a shortcode named ignore, which eliminates its enclosed content. And it goes like this:

<?php
/**
 * Plugin Name: Ignore Shortcode
 * Description: Adds a [ignore] shortcode which simply ignores (doesn't render) the containing content.
 * Version: 0.1.0
 * Author: geomagas
 *
 */

add_shortcode('ignore','ignore_shortcode');

function ignore_shortcode($atts,$content='')
    {
	return '';
	}

Ok, I admit it, that was too easy… So let’s make it a little more “complex”: Let’s add a parameter, an attribute in shortcode-speak, that will make it work in three levels: it will either ignore the whole content, or the shortcodes enclosed or …nothing at all. In other words, if we write

[ignore level='content']

it will be equivalent to

[ignore]

and will eliminate all enclosed content, while if we do

[ignore level='shortcodes']

the enclosed shortcodes will be ignored and treated as content. Moreover, if we write

[ignore level='none']

everything will work as if ignore wasn’t even there! So let’s go:

<?php
/**
 * Plugin Name: Ignore Shortcode
 * Description: Adds a [ignore] shortcode which simply ignores (doesn't render) the containing content.
 * Version: 0.1.1
 * Author: geomagas
 *
 */

add_shortcode('ignore','ignore_shortcode');

function ignore_shortcode($atts,$content='')
    {
	$default_atts=array('level'=>'content');
	$atts=shortcode_atts($default_atts,$atts);
	switch($atts['level'])
		{
		case 'none': return do_shortcode($content);
		case 'shortcodes': return $content;
		default: return '';
		}
	}
	

Well, that’s about it. The first Totally Useless WordPress Plugin is done. See you next time with something even more useless!

2 Comments

Leave a Reply

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