Execute Shortcodes in the Multimedia Box

Applies to Version(s): Thesis 1

WordPress includes a WordPress Shortcode API which is utilized by many plugins to create “shortcuts” which can be used in your post content area. But in some cases, you may wish use shortcodes in other areas of your site.

For example, let’s say you have this very simple shortcode function activated on your site (whether by having placed the code in custom_functions.php, or by installing a plugin):

function hello() {
    return 'Hello, World!';
}

add_shortcode('hw', 'hello');

What this shortcode function does is replace any instance of [hw] in your posts with the words Hello, World! instead.

But suppose you wanted to be able to execute that same shortcode from the Thesis Multimedia Box – you would simply place the following code in your custom_functions.php file:

function custom_execute_shortcode() {
	$myfunction= '[hw]';
	$myfunction_parsed = do_shortcode($myfunction);
	echo $myfunction_parsed;
}
add_action('thesis_hook_multimedia_box','custom_execute_shortcode');

and the output from the shortcode will appear in the Multimedia Box on your live site!

On a Per Post/Page Basis

The code above works only when you’ve enabled Custom Code for the Multimedia Box default setting (i.e., across your entire site). If you want to use another option (such as Rotating Images) for the default setting, but still be able to execute shortcodes on a per-post or per-page basis, follow the instructions below instead.

First, add the following code to your custom_functions.php file:

function custom_execute_shortcode() {
	global $post;
	$hw_enabled = get_post_meta($post->ID, 'enable_hw', true);
	if ($hw_enabled) { 
		$myfunction = '[hw]';
		$myfunction_parsed = do_shortcode($myfunction);
		echo $myfunction_parsed;
	}
}
add_action('thesis_hook_multimedia_box','custom_execute_shortcode');

Then, whenever you’re creating a new post where you want your shortcode to be enabled in the Multimedia Box, do the following:

  1. Make sure the Custom Multimedia Box Code field (under Multimedia Box Options) is blank.
  2. Check the box Access the Multimedia Box Hook (within the same meta box) to enable the hook.
  3. Create a new Custom Field with a Key of enable_hw and a Value of 1.

Publish the post, and the executed shortcode should now appear in the Multimedia Box!