Using PHP to Change WordPress Templates Tags
Before you play around with template tags in your WordPress templates or plugin functions, you need to understand what makes up a template tag and why, as well as the correct syntax, or function, for a template tag as it relates to PHP. WordPress is based in PHP to pull information from the MySQL database. This quick primer walks you through the setup of a basic page using PHP.
Every tag begins with the function to start PHP and ends with a function to stop it. In the middle of those two commands lives the request to the database that tells WordPress to grab the data and display it.
A typical template tag, or function, looks like this:
<?php get_info(); ?>
This example tells WordPress to do three things:
Start PHP: <?php
Use PHP to get information from the MySQL database and deliver it to your blog: get_info();
Stop PHP: ?>
In this case, get_info() represents the tag function, which grabs information from the database to deliver it to your blog. The information retrieved depends on what tag function appears between the two PHP commands.
Every PHP command you start requires a stop command. For every <?php, you must include the closing ?> command somewhere later in the code. PHP commands structured improperly cause ugly errors on your site, and they’ve been known to send programmers, developers, and hosting providers into loud screaming fits.
You find a lot of starting and stopping of PHP throughout the WordPress templates and functions. The process seems as though it would be resource intensive, if not exhaustive, but it really isn’t.
Always, always make sure that the PHP start and stop commands are separated from the function with a single space. You must have a space after <?php and a space before ?> — if not, the PHP function code doesn’t work. So make sure that the code looks like this: <?php get_info(); ?> — not like this: <?phpget_info();?>
To test some PHP code, follow these steps to create a simple HTML web page with an embedded PHP function:
Open a new, blank file in your default text editor — Notepad (Windows) or TextMate (Mac) — type <html>, and then press Enter.
The <html> tag tells the web browser that this is an HTML document and should be read as a web page.
Type <head> and then press Enter.
The <head> HTML tag contains elements that tell the web browser about the document; this information is read by the browser but hidden from the web page visitor.
Type <title>This is a Simple PHP Page</title> and then press Enter.
The <title> HTML tag tells the browser to display the text between two tags as the title of the document in the browser title bar. (Note: All HTML tags need to be opened and then closed. In this case the <title> tag opens the command, and the </title> tag closes it and tells the web browser that you’re finished dealing with the title).
Type </head> to close the <head> tag from Step 2 and then press Enter.
Type <body> to define the body of the web page and then press Enter.
Anything that appears after this tag displays in the web browser window.
Type <?php to tell the web browser to start a PHP function and then press the spacebar.
Type echo '<p>Testing my new PHP function</p>'; and then press the spacebar.
This is the function that you want PHP to execute on your web page. This particular function echoes the text Testing my new PHP function and displays it on your website.
Type ?> to tell the web browser to end the PHP function and then press Enter.
Type </body> to close the <body> HTML tag from Step 5 and then press Enter.
This tells the web browser that you’re done with the body of the web page.
Type </html> to close the <html> tag from Step 1 and then press Enter.
This tells the web browser that you’re at the end of the HTML document.
When you're done with Steps 1–10, double-check that the code in your text editor looks like this:
<html> <head> <title>This is a Simple PHP Page</title> </head> <body> <?php echo '<p>Testing my new PHP function</p>'; ?> </body> </html>
Save the file to your local computer as testing.php.
Upload the testing.php file.
Via File Transfer Protocol, upload testing.php to the root directory of your web server.
Open a web browser and type the address (http://yourdomain.com/testing.php) in the web browser’s address bar (where yourdomain is your actual domain name).
A single line of text displays: Testing my new PHP function.
If the testing.php file displays correctly in your browser, congratulations! You programmed PHP to work in a web browser!
If the testing.php file doesn’t display correctly in your browser, a PHP error message gives you an indication of the errors in your code. (Usually included with the error message is the line number where the error exists in the file.)