thesis wordpress theme tutorial how-to use shortcodes in the multimedia box

Thesis WordPress Theme: Adding Shortcodes to the Multimedia Box

by Dixie

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed or get email updates. Cool, man!

I like the Thesis theme–a lot. And that multimedia box in Thesis is incredibly handy to display all kinds of fancy blogging goodness. But what if you’ve got a plugin with WordPress shortcodes that you’d love to call in the MM box? Shortcodes, in case you didn’t know, extremely handy shortcuts that you can either create or are created by some WP plugins that let you insert stuff painlessly, by adding something like [myshortcode parameter="variable"] to include your specially generated content. Shortcodes work in the sidebar using the default WP text widget, so it would be natural to expect them to work in the MM box, right?

Not exactly. If you optimistically try plugging the shortcode in the Thesis custom multimediabox code, you’ll be disappointed in short order. It doesn’t work out of the box. But with a little PHP code, you can indeed weave some MM Box magic and be happily on your way. [1]

Adding your Custom Shortcode Function

To get your shortcode parsed, you’ll need to set up a custom function. Don’t be scared. It’s not as hard as you may think.

Using the OpenHook Plugin

OpenHook plugin for Thesis is like the lazy web developers’ way to add custom functions. Hop into your OpenHook, and put something like this in the MultiMedia Box area:

<?php
$myfunction= '[your-shortcode-here]';
$myfunction_parsed = do_shortcode($myfunction);
echo $myfunction_parsed;?>

Make sure you check the “Execute PHP” checkbox under this code snippet in OpenHook, since…well, you want it to execute the PHP. Duh.

Without the OpenHook Plugin

You can still easily achieve this without the OpenHook plugin; you’ll be wrapping your code snippet in a function and adding it to your custom_functions.php file directly. With Thesis 1.6+, you can even edit the custom_functions.php in the Thesis options from the WP admin. How’s that for convenient?

We’re looking at something like this: [2]

<?php
function custom_mm_shortcode() {
$myfunction= '[your-shortcode-here]';
$myfunction_parsed = do_shortcode($myfunction);
echo $myfunction_parsed;
}
add_action('thesis_hook_multimedia_box','custom_mm_shortcode');?>

Displaying Your New Multimedia Box Shortcoded Content

custom multimedia boxNow that you have your custom function set up commanding  Thesis to parse your shortcode in the MM box, you need to call it. To display your shortcode content on any individual post or page, go down to the Multimedia Box options and check the “Access Multimedia Box Hook” checkbox. Pretty darned simple, don’t you think?

If you want the multimedia box to default to your special shortcode–still allowing you to override it on a case-by-case basis–then you need to go into the Thesis Options, Design Options. You’ll be setting your the default to “Custom Code” and leaving the box empty, as pictured.

There. That wasn’t so bad, was it?

Note: This tutorial only allows for one shortcode to be selected for the multimedia box display. Feed your need for higher flexibility with WP Conditionals.

Another example (added for Benito)–later removed because of syntax error in the code. There’s a reason why I don’t code PHP for a living.

Footnotes
  1. Thanks to Rick B. for this easy fix to include shortcode in the MM box. []
  2. Please: If you see a problem with my code, mention it in the comments so I can correct. I am not a programmer; I am a hack. []
Tagged: | |

{ 16 comments… read them below or add one }

Scott April 20, 2010 at 12:03 pm

Nicely done – thank you, however, if you could show how to make it work on the page / post specific multimedia boxes, you’d be a hero to thousands – (well, maybe hundreds)…

Reply

serge January 23, 2012 at 9:26 am

hi, i’m not a hero but still … lol the code is here : http://thesistut.com/2012/how-to-enable-shortcode-thesis-multimedia-box/

cheers,
serge

Reply

Dixie April 20, 2010 at 3:01 pm

How can I pass up an opportunity like that, Scott? 8)

To have different shortcodes parsed on different pages, you’d need a uniquely named function set up for each specific shortcode and use conditionals to call the proper bit of code. See the link in gray box above for WP conditionals to use; you can select by just about any combination of conditions you can imagine. Use the examples on the conditionals page to piece together the scenario you want.

For OpenHook, add your if/then statements around each function and, of course, be sure to check the “Execute PHP” checkbox.

For custom_functions.php, you need your if/then statements surrounding the add_action line, as this is the one that executes the code in custom functions.

Does that help, or just muddying the water for ya?

Reply

Benito April 25, 2010 at 11:03 am

Wow. Awesome post. I’m sure a cleverer coder could figure this out using what you’ve explained but is there a way to execute shortcode directly within custom_functions.php to create an arbitrary div? Ultimately, I’m trying to create a email newsletter capture form to be placed at the top-right of each page (sorta like what you have popping up at goodkarmahost.com/blog, but using a pre-installed plugin I have called cformsII)

Here’s the closest I can get, but I feel like I’m missing something.

* Custom Div Box
——————————————————————–*/
function custom_divs() {
?>

<?php echo do_shortcode("“); ?>

<?php
}
add_action('thesis_hook_before_html', 'custom_divs');

Reply

Benito April 25, 2010 at 11:06 am

Oops, the comment stripped out some of my code. Adding spaces to try to make it work.

/* Custom Div Box
——————————————————————–*/
function custom_divs() {
?>

<?php echo do_shortcode(" “); ?>

<?php
}
add_action('thesis_hook_before_html', 'custom_divs');

Reply

Benito April 25, 2010 at 11:08 am

arg.. using brackets. SOO sorry for spamming your post.

/* Custom Div Box
——————————————————————–*/
function custom_divs() {
?]
[div id="email-collect"]
[?php echo do_shortcode("[!--cforms name='email collect'--]“); ?]
[/div]

[?php
}

add_action(‘thesis_hook_before_html’, ‘custom_divs’);

Reply

Dixie May 17, 2010 at 11:40 am

Benito – Sorry it took me a while to get back to you. First time I tried, my code highlighter had some issues and it took me a bit to figure it out.

I added an example to the end of post with code. You have the right idea, but need to remember you’re working in PHP so you have to echo the HTML part of your code to get it to work. Hope that helps!

Reply

Benito May 17, 2010 at 3:53 pm

WOW, You absolutely rock. Thanks for getting back to me at all. I’ll try this asap and see how it works. You are a web goddess!

Reply

Benito May 17, 2010 at 5:11 pm

Ok, I tried your code in my custom_function.php and got a parse error so i tried tweaking a few things. I also added a conditional statement so I can work on a test page to try this thing out.

I’m not having much luck getting the do_shortcode to run. Below is the code I ended up with and I am seeing the proper cformsII shortcode get populated in the html but it doesnt seem to be translating the shortcode.

I’ve added the test page where I’m trying to make this happen. The content area contains the raw form as intended just so I know the form is configured correctly. What do you think?

[* Custom Email Collection Box
--------------------------------------------------------*]
function custom_optin() {

[* check to see if homepage. has to happen inside the function *]
if (is_page(‘test’) || is_page(’125′)) {
?]
[?php
$myfunction= '[!--cforms name="email collect"--]‘;
$myfunction_parsed = do_shortcode($myfunction);
echo ‘[div id="email-collect"]‘;
echo $myfunction_parsed;
echo ‘[/div]‘;
} }
add_action(‘thesis_hook_before_html’,’custom_optin’);?]
[?php

Reply

Dixie May 17, 2010 at 5:54 pm

Benito, I’m really not enough of a coder to effectively troubleshoot someone else’s code, as you noted by the syntax error on what I gave you first. I don’t code for clients because of my limited skillset in that area.

Seeing as how you’re close already, though, I do have a suggestion: why don’t you drop the shortcode approach entirely? Cforms allows you to call forms directly in PHP:

Alternatively, you can specifically insert a form (into the sidebar for instance etc.) per the PHP function call insert_cform(‘XYZ’);, or alternatively insert_cform(); for the default/first form and/or insert_cform(‘X’); for any other form.

Reply

Benito May 17, 2010 at 7:52 pm

Hey, thanks so much for your tips thus far! I’ll work through it and try the direct PHP call. Thanks again!

Reply

Benito May 17, 2010 at 11:07 pm

YES! A direct php call worked wonderfully. Not as flexible as learning to use shortcode throughout the custom_functions.php for any plugin but it gets the job done.

For a final recap here is the final code that worked for me. (I replaced angle brackets with square ones.)

/* Custom Email Collection Box
——————————————————–*/
function custom_optin() {
/* check to see if homepage. has to happen inside the function */
if (is_page(‘test’) || is_page(’125′)) {
echo ‘[div id="email-collect"]‘;
echo insert_cform(’2′);
echo ‘[/div]‘;
}
}
add_action(‘thesis_hook_before_html’,’custom_optin’);

Thank you soo much again!

Reply

Adam Garcia September 21, 2012 at 1:55 am

How do I center shortcode for a slider plugin in Thesis? I put the Slidery plugin code in thesis_hook_after_title but have tried several ways to center it to no avail. 1. By HTML Code that only centered the text in the slider. 2. With the code you provided but I am unsure if I did it correctly, it didn’t sdeem to do anything. Help Please!

Reply

Dixie September 21, 2012 at 10:12 am

Adam – I haven’t worked with that particular slider to know how it’s set up. But I wonder, have you just tried putting the shortcode in a div aligned to center? That’s where I would start.

Reply

Adam Garcia September 25, 2012 at 4:07 pm

I tried that, but all that did was center the text on the slider AND…made the slider go over the comment box area. i thought that was pretty weird. Again, i think I put in the php method wrong can you give me some exact guidance, if possible.

Reply

Dixie September 25, 2012 at 4:17 pm

Andy, I’m sorry but I’m not in a position to directly troubleshoot with you. I would suggest you work with Firefox’s Firebug extension and do real-time experimentation with the CSS, as it really sounds like that’s the problem.

You could also try the slider in stock theme to check for conflicting CSS, as presentation issues usually are about CSS conflicts.

If the shortcode is displaying the slider, then it IS being parsed. So your problem is almost certainly about CSS and not the PHP itself (unless you’ve got CSS in your PHP code, but it still boils down to the CSS).

If you get no joy there, maybe try the support forum for your plugin. Good luck!

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: