It’s the strangest thing – since I’ve begun delving into the world of coding, I’ve heard very little in terms of PHP. It seems that the majority of folks who are attempting to get into this field are more concerned with JavaScript, Python and the plethora of tools that emerge daily. That strikes me as odd, considering how prevalent PHP is on the web. I’ve been working online since the beginning of the century and right behind HTML and CSS, PHP has been where it’s at. To each their own, I suppose.
Treehouse has finally released their latest WordPress course by Zac Gordon, called, “PHP for WordPress.” I’ve been looking forward to this course because, while I’m taking courses in straight up PHP as well, learning PHP specifically as it relates to WordPress is going to be a huge time saver. Since WordPress will be my primary focus moving forward in life and since there are only so many hours in the day, learning advanced PHP probably isn’t in the cards. Getting a grip on what levels of PHP are used by WordPress developers will help immensely.
With that in mind, let’s get started with my notes for Zac’s new course.
PHP Files
If you’ve looked into working with PHP at all, you most likely understand that the files used with PHP, end in .php. Within these files, you can write both HTML and PHP. Some applications use only files that end in .php for every single file they use.
Inside WordPress, there are three primary areas that contain PHP files. They are:
Core Files: If you have no idea what these files are, don’t touch them. These are files that run your installation of WordPress. If you go ahead and edit any of these files and you aren’t a WordPress core developer, you’ll most likely break your install. Much like with any CMS, these types of files run behind the scenes and aren’t meant to be edited by theme developers and the such.
Theme Files: These files are the ones you’ll most likely concern yourself with when developing for WordPress. Theme files control the look and function of the front-end of your WordPress install. They contain HTML and PHP code. In this and later posts, we’ll be talking a lot about theme files in WordPress. If you’d like to see some articles where I’ve already gone into some pretty good detail on this, check out my posts on WordPress.
Plugin Files: Unless you’re a plugin developer, don’t touch these files either. These types of files contain HTML, PHP, CSS and JavaScript. You may want to mess around with these types of files if you’re trying your hand at creating a WordPress plugin or are editing something in someone else’s plugin.
Now that we’ve got that out of the way, the question is, “I want to work on those template files. Where the heck are they?” If you look at an install of WordPress, you’ll see a bunch of files and folders. You can safely ignore mostly everything you see, except for the “wp-content > themes” directory.
If you look inside the “themes” directory, and you’re running a basic install of WordPress, you’ll most likely notice a few default WordPress themes. It’s common for folks to want to modify these themes to make them more suited to their own website, but there are a few words of caution that come with modifying default themes – at times, these themes are updated by the WordPress core developers. If you edit any of these “parent” default themes and the theme is automatically updated, those updates will be written over, meaning all your hard work will be lost. It’s better, and wiser, to create a “child” theme that’s not written over during a theme update.
For example, if you have a theme called, “twentyfifteen,” you shouldn’t edit any files directly in that theme. Instead, you should create a new directory called, “twentyfifteen-child-theme” and copy the file that you’d like to edit, from the original directory, to your new directory. Once it’s there, edit away. WordPress will automatically detect a new file and use it when displaying your website.
Editing Theme PHP Files
When it comes to editing WordPress files, we have a few choices. We can either set up a local server environment on our computer and work from that, work directly from a live server or work on our WordPress files locally and upload them to a live server piece by piece. Let’s go over each option.
If you wanted to go ahead and set up a local environment, you’ve got some choices. They are:
There are more solutions than this, but what I’ve listed here is a good start. Basically, you’d set up a server environment to do your editing and creating from and once everything looks and functions as you’d like, you would upload the entire project to your live server. This is, obviously, the safest way to go about working on a website because of the division between a production environment and a live environment.
The next option you have for editing WordPress PHP files is to work directly from a live server. Once you’ve got WordPress installed, you could either head to the “Appearance > Editor” area in the WordPress admin and have at it. You could, alternatively, use an FTP client to edit the PHP files directly. This would be considered the least safe option there is for editing files because of the lack of the “undo” feature. If you write some code that doesn’t work or code that is way off, you’re going to have a tough time tracking down or reversing your errors.
The last option we have to editing our project is to have a WordPress install live on a server and a backup copy stored locally. This is a mix between the two previous options because you can edit files locally, with all the safety that comes with that, and upload them to a live environment when the files are ready.
Writing Actual PHP
Whichever choice you make, there’s going to come a time when you need to write actual PHP code. I already discussed that PHP files are regular text files that use the .php extension, but I haven’t talked about the other requirement that’s necessary to make your file truly functional PHP. This other requirement is called the “PHP block,” which is essentially a sort of “container” you would write your PHP in.
There are a few rules we need to follow when coding PHP files. The first is that if we’re coding exclusively in PHP (meaning the entire file is PHP code), we can open the PHP block at the beginning of the file and close it at the end. I’ll give an example of what PHP opening and closing tags look like here:
<?php // CODE GOES HERE ?>
The second rule is that if we’re mixing HTML and PHP code in the same file, we’re going to need to open and close PHP blocks multiple times. Here’s an example of that:
<div class="footer-clear"></div> <footer class="row no-max pad"> <p>Copyright <?php echo date('Y'); ?></p> </footer> <?php wp_footer(); ?> </body> </html>
This is some sample code from a footer file in a WordPress theme. What’s important to recognize is the separation of HTML code and PHP code. You can see that it’s PHP code from the opening and closing PHP tags. These are called, “PHP blocks” and may be numerous in number as files get longer.
Remember, if you want to include PHP code in a file, the file has to end in .php. If it ends in anything else, it won’t work. Also, any PHP code needs to be encapsulated inside a PHP block. If it’s not, you’re dynamic PHP code will display as regular text.
Some Basic PHP Syntax
In order to get your PHP functioning correctly, there is some basic syntax that needs to be adhered to. I’ve discussed this in one of my posts on PHP, so if you’d like to read about that, please do so here. Towards the bottom of that post, I get into syntax. If you’d like to read about general PHP development, please take a look at my PHP category.
Leave a Reply