Shortcode in a Template
Shortcodes in WordPress are bits of text you can use in the content area to invoke some kind of function to accomplish certain tasks. For example, video embedding in WP 2.9+ uses the [video] [/video] shortcode. You can write your own shortcodes, and plugins often offer their functionality via shortcodes as well.
But what if you want to use a shortcode from within a template instead of with the content of a Post/Page? You can invoke it with a special function:
<?php echo do_shortcode("[shortcode]"); ?>
THANK YOU! I have needed to do this so many times and couldn’t find an easy way – I can’t believe it’s this simple. Whew!
I need help how do I get my images to pop up in a thickbox with its description on WordPress vertical image menu or other plugins. I need a plugin that’s vertical scrolling with pop up image and description
Hey Michelle,
Can you tell me where the php code goes? I have tried to place it in the space that I need a form to show up but I get an error and the whole page disappears. Please help?
Very ussefull, thanks for sharing =)
wow. this. just… works. ugh. Thank you a million as usual Chris. damn your head is full of useful crap!
I have a small addition – Using shortcode in a custom field.
I am trying to do this for code that is already written to get a picture and description if it not NULL.
I am trying to add code to check for a slideshow, photo, and description.
Each of my slideshows have a ID#. I have added a field in the database that has the gallery ID # (gallery. This is my code but it is not working.
300) {
echo “”;
}
else if ($height > 300) {
echo “”;
}
else {
if ($width <= 300) {echo "”;
}
else {echo “”;
}
}
}
if ($page_row[description]) { ?>
<?php $text = stripslashes($page_row[description]); echo $text;
//echo splitchars($text, 80, "\n”);?>
If I put a value in …….
if ($page_row[gallery]) {echo do_shortcode(‘[slideshow id=1]‘);}
everything works fine, but I need it to find which gallery goes with which member and place that gallery on the Page.
Thanks, Linda
I cant get this working, im trying to use it with the embed shortcode with content from a custom field, could you give an example of its usage?
Found a solution!
Should anyione be interested the solution is:
where $full is the value of the url to be embed (eg http://www.youtube.com/watch?v=wLh6F7G7rPA) from a custom field
@davidswain
I used your solution, however I was running into problems with other filters applied to ‘the_content’… so after much searching… I finally came upon a better solution to this (see below) and wanted to save others the aggravation… :) (also posted this over on the wordpress.org forums)
$wp_embed = new WP_Embed();
$post_embed = $wp_embed->run_shortcode('[embed width="800"]' . $post_embed . '[/embed]');
actually that example doesn’t make since to most…
try this :)
$wp_embed = new WP_Embed();
$post_embed = $wp_embed->run_shortcode('[embed]your-video-url[/embed]');
for all who get a nasty PHP error after inserting the code in the original blogpost: you need to use single quotes, not the normal ones. Even more if the shortcode contains options.
like:
'[shortcode option="moo"]'Thanks @PipeSmokingMan, that made all the difference, Single Quotes not double.
Thank you PipeSmokingMan!
Thank you Chris!
+1 for this guy
Hi Chris awesome tip! i wanted to ask you something, i have a private shortcode and i wanna use it wrapping a table with all the private content but i cant get it to work, how can i wrap it?
I have this shortcode ” [ ultimate_carouselfree 1/] ” that’s working on pages and posts but I would like to display the video gallery on my theme’s homepage and none of the tricks above works. I literally SEE the code on my page even when I include id=1 at the end :(
Thanks “PipeSmokingMan”
yes this code is trou:
you must use (‘) not (“)
Cheers, very useful.
Definitely watch your single and double quotes here….it got me for sure.
i love uuuu! solve my prob!
Can someone help , I have tried following all of the above steps, or at least I think I have
in my index.php :
the plugin is active and I am even using it within a page.
Ive also tried with “echo” and without, but for some reason it just keeps outputting
“[videoplayer file="http://www.youtube.com/watch?v=ujwYaHwTH0A" /]”
Can someone please tell me what I am missing ?
“do_shortcode(‘[videoplayer file="http://www.youtube.com/watch?v=ujwYaHwTH0A" /]” is what is missing above for the “index.php” section
I can not get the short code to work in the header!!! I want to use a short code i got from the cj wizrd so i can but 5 products in the header but it is not working. I tried what you said but it just all shows up as text like [cjwizard]table +saw,5,2446031+2185748+,120,3000,120,3000,USD,120,,,5,[/cjwizard]
This is the code i used i did ‘ and ” like the guy above said to do
any help would be great
Perfect, thanks so much for posting the solutions :)
@Danny (or anyone else),
In the example above, where are you inserting the custom field?
I’m trying to use this snippet for a facebook like shortcode on a custom template page. If I use the [fblikebutton] on a page it works fine, but if I try to use the php line with [fblikebutton] it just displays my shortcode along side the viewable text…any suggestions?
Thumbs up… Needed it badly.
Ok, I’ve been searching far and wide for answers, and no one seems to be able to help. You seem knowledgeable about these things, so I’m hoping you can sort me out. Sorry if it’s too complex!
I have a wordpress website with an image gallery on one of the pages. I installed a gallery plugin that lays out my images with thumbnails to one side and a “loading area” for a larger image. All I have to do is use the short code [photospace], and to get it to display a specific gallery (they’re divided by media type), just [photospace id=NUMBER]. But, when I hard code this into my page template, I can only use the short code once. If I repeat it to access the other galleries, they don’t function. Is there a reason this is happening or a way to fix it? Thank you!
Its Really helpful!
I have following code which is working partially while embedding in my custom page template. i m unable to resolve the issue since last 2 days
Please help out.
The problem I think may be of closing tag ([/tabs]);
I tried many combinations , but got nothing.
because of this code i solved my problem………Thank you so much !!!!!…..
You’re my hero.
Thanks thanks thanks thanks thanks… so easy as well!! I was fretting about this for ages but that man.
I am trying to load a shortcode in my theme using:
echo do_shortcode(‘[jwplayer param="file="http://actionablebooks.com/wp-content/themes/actionablebooks/js/jwplayer/ABmovie.mp4"]‘);
but it keeps echoing out to the page and not getting the movie any ideas what I am doing wrong?
Hi,
I tried it but it is not working for me. It’s just showing whatever i am passing as a shortcode.
Can anyone help me quickly?
Thanks,
job done! :)
wau! this just made my day
I have been trying to add multiple short code in the same row..
For example three price packages short code in same row.. That too wanted to include in php.,.
Can we do that… Kindly give example.
Is that correct? please help us
Sorry ! missed the code…
How can i check if the output of the shortcode is populated? im using this method and its great when theres a result but for a custom field that has no output ie an image i cant seem to be able to check that using a standard IF/ELSE statement. Can anyone offer any advice on this?
Thx.
Thank you so, so, SO much!!!!!!
You are an amazing man!
THANK YOU SOOOOOOO MUCH!!! THIS IS THE $@!* lol but seriously its awesome!! Thanks again
anyone know how to parse the content from inside enclosed shortcode tags. I need it for a conditional… if function – echo shortcode1, else echo shortcode2… having a helluvatime figuring this one out.
For example something like this:
if ( is_home() ) {
echo do_shortcode(‘[shortcode1]‘)
}
else {
echo do_shortcode(‘[shortcode2]‘)
}
Although I presume you’ve tried this… it seems to obvious ;)
If anyone is breaking their theme while implementing the code :
replace the ” with ‘
Ie. try this instead:
I think it happens when you have apostrophes in the shortcode.
Any enlightenment would be most welcomed?
Sorry the code didn’t appear in the above instead of this:
<?php echo do_shortcode(“[shortcode]“); ?<
Try this:
<?php echo do_shortcode(‘[shortcode]‘); ?<
Ahhrrgg.. didn’t see that one first so I submitted the same snippet to the WordPress section recently :/
Sry Chris for wasting your time :)
btw this snippet is very usefull when you code a theme which uses the WooCommerce plugin and you don’t wanna mess with all these hocks and filters..
Worked like a charm !!! Thank you so much.
I have a PHP file which i added Custom Fields PHP options, and i want to add shortcodes to the end result which is a [member] info…[/member] which basically requires to be logged in to display. How can i get this to show up?
[member]<?php the_field(‘price’); ?>[/member]
Doesnt work…
I’m also trying to wrap some member only content inside a beginning and end shortcode in the template file. Any ideas?
Thanks!
i added this to my functions.php file in my theme >>>
add_shortcode( ‘member’, ‘member_check_shortcode’ );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return ‘Login’;
};
below all the require_once section around line 25 – 30 maybe.
But on my end im trying to make this shortcode work inside another php file = to a listing template.
After a lot of digging in the wordpress and php templates, i found this and this was so easy…….thanks a lot for this small piece of code but still very very useful.
Is there anyway I can insert shortcode into HTML?
How do I use shortcode in a template when there are 2 parts to the shortcode wrapped around some code. For instance creating custom fields and div content that are wrapped in [member] shortcodes [/member] to hide content from non-members
I have no idea what I’ve done (fiddled with some formatting in a page?), but suddenly all the shortcodes have stopped working… I don’t know where to start debugging, help! :)
This worked perfectly! Thanks a ton Chris, you’re a super genius.
I would like to add a short code in the js file, where I modify a js file, but call this short code so it does the functionality necessary! Can this be done?
Chris, you are a genius, keep up the great work bro!
Hello all,
I have been trying for days to get this to work according to the format on the codex [code]
echo do_shortcode('[iscorrect]'.$text_to_be_wrapped_in_shortcode.'[/iscorrect]');
[/code] …
What I need to include in the page template is a set of tabs, and then autofill using another short code. In desperation I have even taken out the text in the hope I can get the tabs to display (ie troubleshoot first, fix later) but no luck. I have even tried another tabs plugin with no luck. The plugin I am using is the WP Rollover-Tab: I am unable to get a response from the developer :(
[code]
[/code]
All I get is this printed (no tabs seen)
[code]
[rollover-tab name="tab1"][/rollover-tab][rollover-tab name="tab2"][/rollover-tab]
[/code]
Part of the plugin syntax is mandatorty (no spaces between brackets
[code]
[/rollover-tab][rollover-tabs]
[/code]
I hope someone already has a fix for this. I read an earlier request for help in the comments for a very similar tabs shortcode problem but no reply to fix it.
This is the code that did not display from my comment
php
echo do_shortcode(‘[rollover-tabs][rollover-tab name="tab1"][/rollover-tab][rollover-tab name="tab2"][/rollover-tab][rollover-tabs]‘);
? >
This syntax does not work anymore:
Updated:
Double quotes ( ” )are replaced with single quote ( ‘ )
Thank you! This snippet saved my butt BIG TIME!
Daniyal,
Can you post the code again please? It does not print out on the screen!
For this plugin I MUST have “” as in
[rollover-tab name="tab1"][shcd cntnt1=text1][/rollover-tab][rollover-tab name="tab2"]
otherwise there are errors
I made a little progress with using the RolloverTab plugin by running all the shortcodes in ONE long line for all 5 tabs (as in …
php
echo do_shortcode (‘[tabs][tab1][content1][/tab][tab2][content2][/tab][/tabs]‘);
?>
but now all the content shows in all the tabs
Any thoughts/suggestions ?
Thank you! Didn’t think this would be possible! I was trying to insert the plug ins code into the main page. Nice one!
Chris…..it’s awsome, thanx a lot man,…..
Does using a shortcode also help for loading efficiency? say you make a shortcode of what appears on more than one page, but not in the header to appear on all pages?
I’m not so sure this is correct? Should’nt it be
and not
Using quotations does not work when adding shortcodes in WordPress 3.0 templates
Thanks man!
thanks! you saved my time!! thanks alot
thanks man, it really helped me to solve a serious issue on a client site.
Hi I have inserted a slider using the shortcode php script provided could any one tell me how I would center the slider that the shortcode is grabbing. I have tried everything.
Site is http://www.thetheatreacademy.com/test
Many thanks in advance
You could add #bannerRotatorParent_18262 to your stylesheet and do
bannerRotatorParent_18262{
margin: 0 auto;
}
that would work I guess…bit messy but would work
So do I just copy AB past that to my style sheet?
Add
bannerRotatorParent_18262{
margin: 0 auto;
}
to your stylesheet. Use a # at the start of bannerRotatorParent_18262 i think this comment area is removing it
that should work
Hi Glen,
Can seem to make that work. have added to css but not working. Can you offer any other advice?
Many Thanks.
Maybe I can help if you contact me matey? @glenwheeler on twitter or onesixtwoltd on skype hope chris doesn’t see this as advertising myself.
Thanks,
Glen
Thanks a lot for the Tip :)
Thank you very much to all of you for your help. I’ve been trying to figure out a solution to this for quite some time.
Wow! That simple? I can’t believe it :) Very useful tips as usual. Thanks for posting it.
Hi, I have problem with this my shortcode is
[ninja-popup ID=1]popup[/ninja-popup],
I tried this:
?php echo do_shortcode('[ninja-popup ID=1]cool[/ninja-popup]'); ?But didn’t work…please how to do it?
Thank you
Googled this, found your post, spot on Chris.
Thanks for this!
:)
Thanks so so much.. It Works for me!!!!!!!!
This falls under the category of code I never try to memorize. I’ve been to this page more times than I can count. Thanks for a great site and community.
Very useful! Thanks!
Another solution though not as easy is to use the short code on a normal page then view that source code and copy it onto your template.
hey nice it works but how can I anchor a shortcode in my header file
You can also execute the shortcode using a custom function with conditional tag and hook it into place in your child themes functions.php file which is a better way to customize your theme rather than edit your parent themes template files.
Example:
function display_slider_header_home() {
if (is_home()) {
echo do_shortcode(‘[easingsliderlite]‘);
}
};
add_action(‘genesis_header’, ‘display_slider_header_home’);
Thanks, Chris!
I’m trying to make a page have tabs without having to put the shortcodes into the tinyMCE.
I want to have this:
can this be done using the methods above. I am having some trouble
works great, I used single quote instead of double quotes and that solved the initial issues i had.
Ok, I hope someone out there can answer a few questions and point me in the right direction. I have a site: http://www.southjerseyhomes.info. —it is very dependent upon a 3rd party idx (real estate listings) plug in that uses short code.
I have been on a mission to decrease page load time. One of the 7 errors I get on Yslow is that i need to:
Grade F on Put CSS at top
There are 7 stylesheets found in the body of the document
http://www.idxre.com/idx/css/carousel_thumbscroll_atlas.css?…
http://www.idxre.com/idx/lib/javascript/livepipe-ui/css/window.css?…
http://www.idxre.com/idx/externalServices/views/css/ihf_atlas_wp620.css?…
http://www.idxre.com/idx/externalServices/views/css/ihf_atlas_wp620_blue.css?…
http://www.idxre.com/idx/css/modalPhotoCarousel.css?…
http://www.idxre.com/idx/externalServices/views/css/ihf_organizer_bar.css?…
http://www.idxre.com/idx/css/ihf_v7_1_blue.css?…
This plug in is a short code item, so it’s move the short code or nothing at all. Each page (about 300 of them have their own short code id). Any ideas how I can pull this off and decrease page load time.
php echo do_shortcode('[soundcloud]'.$album_url.'[/soundcloud]'); ?>few people above trouble with closing to here this might help ^^
thanks Chris! .
Yes! Thanks for this, exactly what I needed for processing shortcodes in custom-made php files.
Amazing.
I know 100 people already said it.
But I needed this so bad.
Worked perfectly.
Thanks CHRIS!!!!
Awesome! Love all of your advise and you have saved me once again :)