How to put Shortcode in PHP on WordPress

Apr 20, 2016 | WordPress Wednesday

Kori explains how to put shortcode in PHP on WordPress.

Full Video Transcript

Hi you guys! Welcome to another WordPress Wednesday. My name is Kori Ashton here at WebTegrity in San Antonio, Texas.

Today we’re going to look at putting shortcode inside of PHP- inside of your WordPress dashboard. What does it look like to try to put a slideshow on your homepage if the author hasn’t just allowed you to do something like that? What if you want it fullwidth up in your header and really make a beautiful presentation of a slideshow? What if you’re trying to put some sort of a calendar somewhere and it just won’t render correctly on a page?

I’ve got a solution for you of how to get it into your PHP files. However, (quick side note) as you know in the past as I’ve shared with you, if you do anything inside of your PHP files (of course) you want to be working on a child theme.

If this is already a little advanced, don’t worry about it. I’ve got a couple of other videos in the description box below that can walk you through the idea of how to set up a child theme very easily.

Also, how to go into your PHP files without messing anything up. I’ve also got another video in the description box below that’ll help you put shortcodes inside of your widget boxes. That’s not what this is about. We are looking at how to put shortcode inside of PHP files specifically.

Let’s get to it.

Of course a shortcode looks just like this. It’s inside of brackets and it’s some sort of function calling for a plug-in (more than likely)- a plug-in that you’ve put into your WordPress website.

How do I get this little nerd code to work inside of a PHP file?

Well, you could (if you wanted to) on a theme try to go just go into the page itself and paste the shortcode. Of course it works like that. If you’re trying to put something (maybe) up in your header and the author hasn’t given you an easy way to do that, there is a way to get there.

Of course, you want to be working (again) on a child theme in order to get to these PHP files. For the sake of this tutorial, I’m just going to run right in here and jump into my header. Now remember, that a header runs the whole top part of your website and it (of course) repeats on every single page-every single post if that’s the way the author has coded it (which is more than likely.) If you’re going to put something in your header it will repeat on every single page- every single post. If that is not what you’re trying to do and you just want it to be on your homepage, you’ll want to look for a different PHP file. You’ll want to look for one (maybe) called frontpage.php or perhaps one called index.php. Again, completely dependent upon your theme. If this is going to get a little confusing to you and you can’t get this tutorial to work, you might want to look at your theme’s documentation or go talk to your theme author in their support forum and see if they can help you get this shortcode where you want it to sit and appear.

What I’m going to give you right now though is the the PHP code that you put that little shortcode inside of and it turns it into your awesome slideshow or calendar -w hatever it is you’re trying to render.

All right.

<?php echo do_shortcode(“[insert_your_shortcode_here]”); ?>

Let’s say I wanted to put one full width up here in my header. Of course I’m going to go back to my header.php file (that’s where I’m at) and I already have my slideshow plug-in in place here the “Fluid Responsive Slideshow.” This is a free slideshow. If you’re looking for something more fancy than just images turning, you might want to look at the Rev Slider. We’ve done a lot of tutorials on that. I’ll put the link to that in the description box below. It’s a premium slideshow so you get a lot of options. If you’re just looking to do a couple of slides rotating back and forth, this is really a pretty great plug-in that we found for free. This of course (once you’ve created it) is the shortcode that you would want to use to embed.

But, if I go here to my header (and I know this could look a little intimidating to you) what you want to do is look at an end tag here that says </header.> This won’t mess anything up as long as you’re meticulous right here. This is another reason why you need to have a backup in place before you’re doing this.

All right.

Be sure you’ve already clicked backup and you know you have a backup in place. If you want to you can copy this whole entire PHP file and put it in a text document or something before you start tinkering here so you know we don’t mess up anything. This could break your whole website. You need to be cautious as you’re going through here. Be very meticulous.

I’m going to show you again, just before I’m going to drop in there and paste in my shortcode
right here, grab my shortcode for my slideshow, come in here and just before

I’m going to put in that little shortcode. If I click update and jump back over here and click refresh, it’s not going to work. It’s very difficult to see because it’s black text sitting on a black background but my text is sitting right here and my shortcode just sits there. It’s not working correctly. That’s because shortcode kind of by default does not want work on it’s own inside of a PHP file. You have to put it inside of a PHP call.

So, here’s that information right here. This is a PHP line that tells your browser to render our shortcode. Here’s what we’re going to grab. We’re going to grab this whole thing. I’ll put this in the description box below so you can just copy and paste it. We’re going to come back here. We’re going to paste that in. We’re going to take our shortcode that we actually do want to render and we’re going to back out this one that says insert your shortcode here. I’m going to delete that and paste it in here. If I’m going to quickly just press pause for a second, rewind this and walk through the steps.

It’s super simple. All we’re doing is pasting in a bit of PHP and we’re going to replace a shortcode in there asking WordPress to render our slideshow. Let me jump out of the way here. I’ll click refresh. Now because we put it up in our header, our slideshow works beautifully. But, of course it’s going to be on every single page. So if I go over to results it’s going to be in our header. You can zip through all of our different slides. If I go back to the homepage (of course) it’s going to be there. Again, it’s going to be on every single page because I put it in my header file.

If I only wanted that to work inside of my homepage, I can just grab that little bit of PHP and go to my front-page now. What you’re looking for is kind of the start of your content on that page. I’m just going to drop it in there. You can tinker around and play as long as you have a backup in place of your website and you know how to restore it. You can always tinker around inside here and play. I’ve placed this now on my front-page and now if I go into any other page, it won’t be there.

That’s how you take shortcode and put it inside of PHP. Again, please be sure to be using a child theme if you’re doing something like this because the second you update your theme- this will disappear completely and you’re going to have to go do this again. If you’re using a child theme, this PHP file will always overwrite that parent PHP file.

Have fun with this. You can tinker around as long as you want (as long as you have a backup in place and you know how to restore your website. There’s all sorts of extra resources for you in the description box below.

Have fun. If you have questions put them in the comments and we’ll do our best to answer any questions that you have.

Have a great WordPress Wednesday.

Bye, y’all!