How to Program a Basic WordPress PHP File
To make sure you understand the basics of PHP in WordPress, including how to start and stop PHP within a file, try your hand at a little sample of 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) — then type <html> and 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 the two tags as the title of the document in the browser title bar.
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 appears in the web browser window.
Type <?php to tell the web browser to start a PHP function and then press Enter.
Type echo ‘<p>Testing my new PHP function</p>’; and then press Enter.
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 ?> (be sure to insert a space before) 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 through 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>
After you write your code, follow these steps to save and upload your file:
Save the file to your local computer as testing.php.
Upload the testing.php file via File Transfer Protocol (FTP) 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.
In this example, yourdomain.com is your actual domain name.
A single line of text displays Testing my new PHP function.
If the testing.php file appears correctly in your browser, congratulations! You programmed PHP to work in a web browser.
If the testing.php file does not appear correctly in your browser, you see some common PHP error messages that indicate what errors exist in your code (usually it gives the error message plus the line number where the error exists in the file).