How Do I Work On WordPress PHP Theme Files?

How Do I Work On WordPress PHP Theme Files?

At Boston Web Marketing, many of our custom-built websites are developed on top of the WordPress Content Management System. WordPress powers over 50% of all websites on the internet and is considered the industry standard for hosted static websites built on content management systems.

While WordPress is known for its ease of use to create, read, update, and delete new or legacy content on a website, the fact that the current default editor is built on PHP means there is a fair amount of complexity under the hood. While in most cases you will be able incorporate plugins to add common types of functionality to your website, at some point you may need to edit PHP files via FTP or directly through the built-in theme editor in order to change theme layout files, add schema markup, create custom functionality, or otherwise implement changes for a client. Today, we will do a brief intro on how to work with PHP template files as well as take a look at some common PHP snippets that you may run into while editing content on your WordPress website. In order to do this, first let us talk about the WordPress API.

What is an API?

Nearly every piece of software intended for use by other users will have some form of an Application Programming Interface (API). You can think of an API as the inputs that you are to interact with by design. An API in software is analogous to the set of keys on a piano or the buttons on a mouse — the manufacturer of a piano or computer mouse intends for you to press the buttons on the piano or mouse to create music or perform tasks, without the need to know any specific details on the kind of music you will be playing or the kind of computer you will be plugging your mouse into. Similarly, a piece of software designs an API as a set of inputs that you can use to perform common expected tasks without needing to write every piece of code from scratch, the same way you do not need to figure out how to hold a tiny little hammer to hit the strings inside of a grand piano when you want to play a piece of Chopin or Billy Joel — the keys control the hammers, so you only need to worry about pressing the right keys at the right time.

In the WordPress API, the WordPress community and developers have created functions to allow you to pull content from your WordPress website and design custom functionality for use in themes or plugins. The WordPress API is divided into many smaller APIs for each type of content you will work with — there is a separate Plugin API, Widget API, Settings API, and many more listed on the WordPress. Each of these functions can perform a common task for you to Today we will talk about Template Tags, which are an important component in understanding your theme layout files.

Template Tags, PHP Blocks, and Modular Code

WordPress uses Template Tags in order to display content like page titles, posts, and comments on your website in a modular, reusable way. In your theme files, you will occasionally see functions enclosed inside PHP Blocks, for example:

 

As a refresher, anything you find inside the tags will be replaced with the return value of the PHP functions inside. In this case, the PHP function is a Template Tag that returns the name of the post currently being referenced in the context of the function. There is a corresponding Template Tag in the WordPress API called the_content(), as well as many other Template Tags that you use to get every piece of information that you need from each post so that you can create the layouts that you need.

Most of the Template Tags you will encounter will be inside what WordPress refers to as “The Loop”. “The Loop” is essentially this PHP block:




What this piece of code does is call the WordPress function have_posts() to checks to see if there is another post stored in the database (which you create using the Add New value of the Posts section of your WordPress menu) and then display the current post by calling the_post() function, unless you have run out of posts, in which case the while loop will end. You can then use the_post() function as the context of your other Template Tags.

The reason it is important to build your websites using this kind of code, instead of building many pages and copy/pasting the reusable parts, is the ability to quickly make changes for your clients without having to make an update to every single page containing that information.

For more information on Template Tags and the WordPress API, check the documentation and resources available, or search Google for the function that you are looking for — chances are it will take you to either the WordPress documentation or the Stack Overflow website, where others may have asked the same question that you are currently looking to answer.

For custom website builds and SEO services, contact Boston Web Marketing to see the benefits that we can offer your business.

admin
No Comments

Sorry, the comment form is closed at this time.