Inserting PHP code in posts
Video: Inserting PHP code in postsSo far in this chapter, we've examined how to use custom PHP functions on the backend of WordPress. But what if you need to include PHP- generated output on the front end, in a post or page? For that degree of control you'll need to turn to one of the many plug-ins written for that purpose. In this video we will take a look at one of them that turns WordPress Visual Editor into a PHP engine. However, before we can include our PHP code, we need to write it. Dreamweaver is perfect for setting up your initial PHP code and testing it.
- Next steps
Viewers: in countries Watching now:
In this course, author Joseph Lowery shows how to create a robust WordPress-based site using Dreamweaver. The course lays out the essentials of planning a site, explains how to implement custom sidebars, and demonstrates building page templates. The course also explains how to integrate PHP functionality and extend the WordPress database.
- Understanding development environment requirements
- Working with existing WordPress content
- Handling standard pages
- Managing full posts
- Coding a basic template
- Adding a custom sidebar
- Working with WordPress functions
- Inserting PHP code in posts
- Including a recordset
- Outputting data
- Linking to dynamic pages
Inserting PHP code in posts
So far in this chapter, we've examined how to use custom PHP functions on the backend of WordPress. But what if you need to include PHP- generated output on the front end, in a post or page? For that degree of control you'll need to turn to one of the many plug-ins written for that purpose. In this video we will take a look at one of them that turns WordPress Visual Editor into a PHP engine. However, before we can include our PHP code, we need to write it. Dreamweaver is perfect for setting up your initial PHP code and testing it.
Now I have just such a code in the exercise files and I will open it now. It's called php_example.php. And you can find it in the Chapter 4/04_03 folder. Now I have some text here that I'm going to be using in my message. I just included it in my PHP file. But the real workhorse part of this is this custom PHP function that you see here. Let me just walk you through it very quickly.
It sets up a series of variables. The first is theTarget on line 11, and theTarget is set using the php mktime, or make time function. That sets the target as March 31, 2022. Today is set to PHP's basic time function. The difference between the two is set to the target minus today. Then the days, which will calculate the number of days, uses the formula that you see here, using a PHP number format function to format the number when it's returned, where we take theDiff, that is, the number of days, and divide it by the number of seconds, times the number of minutes in an hour, times the number of hours in a day.
Finally, there is a print statement that outputs our texts when Venus tanning sessions will be available, and they will be available on March 31 in 2022. So it would give you an idea of how many days you have to wait before you can take advantage of that. So let's go to Design view here, and I am going to save this file in my PHP site. So I will choose File > Save As, go to the site root, and just save it there. No need to update any links.
Now let's enter into Live view to make sure that our PHP code is working, and there you have it. Venus tanning sessions are a mere 3,717 days away. So we have our code. All right, looking good code-wise. Now let's switch over to WordPress and install our plug-in. So I'll go to Plugins > Add New, and we are going to search for one called exec-php, short for execute PHP.
Once the search results are returned, click Install Now. OK to install it and then activate the plug-in. Now before we can use this plug-in, we need to temporarily switch off the Visual Editor. Unfortunately, you can't just use the HTML tab of the WordPress editor; you to actually disable the Visual Editor for whoever is putting in the code. That is handled through the user profile. So I will go up to the upper-right corner here and click Edit My Profile.
You can also get to this through the Users option. The very first selection is Visual Editor, Disable the visual editor when writing. Let's just select that, and then I am going to scroll down to the bottom and click Update Profiles. We are ready to create our PHP-based posts. Let's get the code from Dreamweaver. I'll switch over to Code view, and I just want a copy the code that's in the body here. Now let's head back to the dashboard, add a new post, put in our title of Venus Launch Date Announced, and we will paste in the code.
Now you'll notice that on the right there are no separate tabs anymore for visual HTML. That's to be expected because of course we disabled that in the profile. Now let's publish this and view the posts. Well, there it is. I've added some CSS to make it stand out, but as you can see, it's working perfectly. Once you've inserted your code, you can always go back to profile and re-enable the Visual Editor. The only time you need to disable it is when you're writing a post that is just PHP, which you can develop and test in Dreamweaver before bringing it into WordPress.
There are currently no FAQs about Dreamweaver and WordPress: Building Sites.