PHP in wordpress

January 23, 2010 at 5:41 am Leave a comment


What is PHP?
When you view a webpage, the server (i.e. the site you are viewing) sends a bunch of HTML to your computer. Your browser (Firefox, Safari, Internet Explorer, whatever) then turns this HTML source into the pretty stuff you see in your browser.
With pure HTML, every visitor to a website will see the same thing, no matter what. For example, if I make an HTML page with this content
How do I insert PHP into an HTML page?
PHP and HTML are completely distinct languages. You need to help your server tell them apart so that it knows which parts to process as PHP. Always open PHP code with . You can do this as many times as you want in your document.
Everything between must use correct PHP syntax, or else you’ll get one of those dreaded “parse errors” that can be very difficult to figure out when you’re new to PHP. So let’s talk a little about correct syntax.

First, let me explain how to insert comments. You know that something like in an HTML document gets ignored because it’s a comment. There are few ways to do the same thing in PHP. Here are the two you’ll see used in themes most frequently, the // syntax and the /* … */ syntax:

Bad examples that will cause a “parse error” in PHP:

The last example really throws people off. PHP sees that the string starts with ‘, so when it sees the ‘ after How, it thinks that’s the end of the string, so it can’t figure out what to do with s it going. The result: A fatal parse error. There are two ways to fix it:

To display the contents of a (string or integer) variable on your webpage, you just echo it:

To append something to an existing variable, use a dot. There are two ways to do this:

Conditional Statements in PHP
Templates and plugins make frequent use of a handful of statements in PHP. The difference between statements and functions is obscure, so I won’t try to explain any more than this: Statements are “special” functions that tell PHP to check a condition before proceeding or to repeat a block of code multiple times. The statements you’ll see the most are if, elseif, else, for, foreach, while.
If you write a plugin, you’ll probably need all these statements. But most WordPress users need only to know how to use the conditional statements, so that’s all I’ll write about for now.
if, elseif, else are used for conditional statements. These tell PHP to execute a block of code only if something is true. There are two “correct” ways to write these. In general:

These two syntaxes can be expanded as necessary using else and elseif, as shown:

You can use as many elseif statements as necessary, or none at all. If you want to display different HTML depending on a conditional statement, you can simply close the PHP tags between if and elseif. This example uses WordPress’s is_home() and is_archive() functions:

Main Page

Archives Page

Welcome to my blog!!

Variables in Conditional Statements
Now it’s time to combine the two preceding sections. You learned above that you can assign $myVar the value Howdy! like so:

But what if you want to test whether $myVar already has the value Howdy!? To check a variables value, use two == instead of one =, like this:

The first example is correct. The second will not work as expected. In the second example, the if is testing whether or not PHP is successfully able to assign the value of Howdy to $myVar, which will always be true.
Functions in PHP
Most of the PHP you see in your WP templates will be neat little functions. I’m not going to talk about how to create a function here; if you want to do that, you should check out a more advanced tutorial. But I will talk about how to use existing functions.
There are two kinds of functions. There are functions that RETURN a value, and there are functions that PRINT (or echo) a value. The distinction is crucial. Let’s talk about each briefly.
You can use functions that return a value in conditional statements. For example, is_home() is a function that returns TRUE if you are on your blog’s main page, FALSE otherwise. If you want your template’s header.php file to do something differently on your blog’s main page, but not elsewhere, you would use this code:

… html for the main page only …

Note that is_home() by itself won’t cause anything to appear on your site. If I inserted this code into my theme files, it would not cause any noticeable difference in the HTML output:

That’s because is_home() doesn’t print any HTML, it just returns a value.
By contrast, let’s look at a function that does the opposite–that is, a function that echoes but does not return anything. WP’s the_content() prints HTML, but it does not return a value. You already know that placing into a theme file causes the post (or page) content to appear. However, it would be meaningless to try to use the_content() in a conditional statement. The conditional statement shown below will always evaluate to false, since the_content() has no return value:

Fortunately, many of WordPress’s template “tags” (which are better called “functions,” not “tags”) have two varieties–one that returns, one that echoes. The preceding conditional statement would work as expected if we used get_the_content() (which returns) instead of the_content() (which echoes). See the difference?

WordPress Web Hosting
There are hundreds of thousands of web hosts out there, the vast majority of which meet the WordPress minimum requirements, and choosing one from the crowd can be a chore. Just like flowers need the right environment to grow, WordPress works best when it’s in a rich hosting environment.
We’ve dealt with more hosts than you can imagine; in our opinion, the hosts below represent some of the best and brightest of the hosting world. If you do decide to go with one of the hosts below and click through from this page, some will donate a portion of your fee back—so you can have a great host and support WordPress at the same time.
WordPress’s minimum requirements

To run WordPress your host just needs a couple of things:
PHP version 4.3 or greater
MySQL version 4.1.2 or greater

That’s really it. We recommend Apache or Nginx as the most robust and featureful server for running WordPress, but any server that supports PHP and MySQL will do. That said, we can’t test every possible environment and each of the hosts on our hosting page supports the above and more with no problems.

Blue Host
“WordPress Auto-Install — new WordPress updates are made available within 24 hours! Unlimited disk storage, Unlimited monthly data transfer, host unlimited domains on 1 account, 1 free domain name for as long as you host with BlueHost, 2500 POP / WebMail addresses, 50 MySQL databases, free control panel, free Fantastico, free SimpleScripts, 24/7 network monitoring, 99% uptime guarantee, mirrored storage backups, no hidden fees, voted best support in 2004, 2005, 2006, 2007, and 2008!”

Entry filed under: php in wordpress.

Javascript will work within WordPress and Using Javascript in WordPress How a WordPress Plugin Works

Leave a comment

Trackback this post  |  Subscribe to the comments via RSS Feed


Blog Stats

  • 8,739 hits

RSS Enabled.in

  • An error has occurred; the feed is probably down. Try again later.