WordPress is not PHP

There have many concerns and question about the template system used in WordPress. Mostly the concerns are with n00bs and the complications of PHP. “I want to change this and that, but don’t know PHP”. I hear this a lot. But the thing is, you don’t need to know PHP to change the Templates in WordPress.

The Template System

Lets’ start with “the Loop” as we call it. Probably the most confusing part of the Template for a non-programmer. The Loop looks like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 
     //Template Section 1: post content Template Tags go here
 
<?php endwhile; else: ?>;
 
    //Template Section2: no posts found stuff here.
 
<?php endif; ?>

It may look complicated, but it’s actually quite simple. In Template Section 1, all the posts information will be displayed. Within that section we will put all the posts Template Tags, and our HTML to format them. In Template Section 2, we will display a message telling the user no posts were found, if we find none to meet their criteria.

Template Section 1: The Posts

We’ll start with outputting the post tile. Simply add in the Template Tag <?php the_title() ?>. That will output the title of the post.

Now let’s output the date of the post. Simply add the date’s Template Tag <?php the_date() ?>. That outputs the date of the post.

We will also, of coarse, need the content of the actual post. Simply, again, add the Template Tag <?php the_content() ?>.

Now let’s put it all together with some HTML:

<div class="post">
    <h2> <?php the_title() ?> </h2>
    <p> <?php the_date() ?> </p>
 
    <?php the_content() ?>
</div>

And there’s our simple template. Now let’s add some more.

First let’s add a link to the post, permalink as it’s called, on the title. We get the actual URL, something like http://mysite.com/archives/2004/09/post-title/, with the Template Tag <?php the_permalink() ?>. We will need to put that in our <a href=””> HTML tag.

We also want to add in links to each page of our post, next page, previous page, etc.. We simply call another Template Tag, <?php link_pages('<p>','</p>') ?>. Now here’s another confusion people have. We’ve added in, what programmers would call, arguments to our Template Tag. That is, we’ve added in '<p>', '</p>' to the brackets of the Template Tag. In this case we are just saying, if there are pages to link, output the page links with a <p> at the start, and a </p> at the end. Hence, enclosing it all in HTML paragraph tags. Most Template Tags have different arguments you can pass to them, causing them to format their output differently.

Now let’s add in our new tags.

<div class=”post”>
  <h2> <a href=”<?php the_permalink() ?>”> <?php the_title() ?> </a> </h2>
  <p> <?php the_date() ?> </p>
 
  <?php the_content() ?>
 
  <?php link_pages('<p>','</p>') ?>
</div>

Now we have a fully functional Posts section, Template Section 1. Let’s move on to Template Section 2.

Template Section 2: No Posts Found

For our Template Section 2, the no post found section, all we need to do is put in a message to tell the user nothing was found, to meet their criteria. Whether it was a search or a mis-typed URL. So let’s use the following simple HTML:

<p>Sorry, no posts could be found to match your criteria.</p>

Now let’s put it all together.

Complete Template

Putting all we learned above together, we get a nice simple Loop Template.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 
    <div class=”post”>
        <h2> <a href=”<?php the_permalink() ?>”> <?php the_title() ?> </a> </h2>
        <p> <?php the_date() ?> </p>
 
        <?php the_content() ?>
 
        <?php link_pages('<p>','</p>') ?>
    </div>
 
<?php endwhile; else: ?>
 
    <p>Sorry, no posts could be found to match your criteria.</p>
 
<?php endif; ?>

As you can see, we have done no PHP coding, just some simple HTML with WordPress Template Tags added in.

Obviously this is not a complete template, as we have no header and footer containing all the <html><head><body> tags. But with some simple HTML and more Template Tags, and no PHP, we can easily add all that is needed for a complete HTML document. That’s right, an HTML document, not a PHP script.

As we can see WordPress is not PHP. WordPress is powered by PHP, but uses a simple template system, easily modified by even the most novice of users. None of that complicated Perl code like MT has … Yes, believe it or not, MT is also powered by a complicated programming language. The only difference is marketing. MT has been marketed as having simple templates with no programming skills needed. However, somehow, and I don’t know why, WordPress has gotten a rap of being complicated and only for the hardcore programmers out there. But as we can see, using the Template Tags is extremely easy and requires no knowledge of PHP at all.

Changing the format and style of your Template is as easy as using a little HTML, CSS and Template Tags. Just like other Weblog systems out there.

However, as with any software, there is room for improvement.

Improving Templates

One of the problems with the WordPress Template System, in my opinion, is the default Template, or theme, itself. Specifically the sidebar, with all the is_*() functions. We shouldn’t expect a novice WordPress user to understand what these functions mean. I would suggest removing all the if(is_*()) statements in place of a simple sidebar with just the essentials. Say, Search, Pages, Archives, Categories, Links, Meta information and take out all the complicated if statements and includes.

Another problem I see with the default Template is the CSS in the header. I think all CSS should be contained in the style sheets themselves. Again remove the if statements. When a user wants to modify the CSS it seems logical to have just the simple CSS in one place, to easily modify.

Another problem I see is with some of the Template Tags. Such as <?php bloginfo() ?>. Instead of having one function to display some necessary information, like blog name, description, url, etc., why not have specific tags for the highly used information. Like say, <?php blog_name() ?>, <?php blog_description() ?>, <?php blog_url() ?>, etc., to maintain consistency. For some of the less used information, charset, version, etc., the <?php bloginfo() ?> tag would be fine.

And some of the other advanced Template Tags in use in the default Templates, like <?php list_cats(0, '', 'name', 'asc', '', 1, 0, 1, 1, 1, 1, 0,'','','','','') ?>, should be simplified to not contain all those arguments. Maybe use <?php wp_list_cats() ?> where all those settings would be set in the admin area.

It would also be nice to try to simplify the loop, although I can’t see any way of doing that now, to use as little actual PHP, like if else statements, as possible. Also take out the else, for when no posts are found, and always use the 404 template file.

The point I’m trying making is to take the PHP out of the Templates, as much as possible, so users don’t get confused and think they need PHP skills to modify the Templates.

Those are just my thoughts on the matter.

If you need any help with your templates, or would like to learn more about the monkey, I’d be glad to help out. Just drop me a line.

WordPress is not PHP was written by

Comments

The opinions expressed in comments are entirely the responsibility of the various contributors. While I will do everything within reason to ensure that they are not defamatory, I accept no liability for them or the content of links included in them.

(#24171)

Good Post. A lot of information some of which I put to use matching my company’s website to our blog. Thanks!

(#1831)

Mega, mega kudos. Required reading for people new to WordPress.

(#1832)

Its about time someone posted something like this. If you ask me, theme developers need to stop their bitching and sit down for a minute and look at what they’re doing before saying “ohgodidontknowhtisicandoitbetterstartbitching”

(#1833)

Just wanted to say thank you!

That sweet tutorial really helped me start working on my wordpress site. Thanks!

Gorgeous site by the way… 8)

(#1835)

M.P.R.D

(#1836)

I dunno… I think you’re greatly underestimating how intimidating this stuff is.

Those aren’t “template tags”, after all; they’re function calls with pointy brackets around them, and thus bear only the most trivial resemblance to something more familiar, like HTML.

Now, I’ll grant you, if you’ve got a user who doesn’t even grasp HTML, WP’s templates may be no more complicated that anything else. But is such a user going to monkeying with her templates in the first place?

(#1837)

Roger, you’re right, technically there are function calls. But technically a stop sign is a piece of sheet metal with paint on it, yet it’s a stop sign. In this case those function calls are Template Tags.

If we look at any template system, technically all the template tags are function calls. They just get replaced with the content using some regex. So whether we’re using {$the_title} or , they are both template tags. The fact that the later is actual PHP is simply superficial. It’s a template tag that just looks different than others, but effectively is the same.

I’ll I’m saying is that let’s stop thinking of it as PHP, and start thinking in terms of templates. It’s just an HTML document with some WP tags in it. There is no PHP coding required. Thinking like that, and helping people out by using generic "template tag" terminology, instead of using programming terminology, will help reduce the amount intimidation.

(#1838)

Awesoem write up Matt, This would have been incredibly useful when I started using wordpress because I just could not figure out how it mixed in the content with the formatting. Its come a loooong way since then hahahah

(#1839)

Unleashing the power of WordPress

It’s interesting what happens when one metaphorically “looks under the hood” at the code of WP.

I’ve discovered that WP has one fatal flaw in it’s presentation to the world. It’s billed as a blog, when in actual fac…

(#1840)

Bravo, really well explained. You’ve done a great job. :D

(#1841)

Remember that PHP was originally designed as a template system for use with HTML. Only later it advanced to a full blown programming language.

What I like in WP is that you can start in a very simple way, only using some of the template tags, but if you really need it, you can use the full power of PHP to do some magic without learning another easy (but also proprietary) language.

(#1842)

Matt,

I think your dismissal of MT templates’ perceived ease-of-use as pure “marketing” smoke and mirrors is incorrect. MT indeed has a full separate and well-documented template “language”. An MT user could go his entire life, creating and customizing templates, without ever encountering Perl or even knowing Perl is under the hood.

Yes, MT templates have a syntax of their own that may need to be picked up, but it’s like HTML: angle brackets and attributes. (example: ) Not hard for HTML authors to see the similarity between this and html tags. And that’s why it’s easier to learn.

As mentioned earlier, the WordPress template tags are function calls. For a novice user who’s never programmed before, the syntax is completely alien. Open and close parens, commas between arguments, what an ‘argument’ even is, if statements.

(#1843)

Nice article, I’ll keep it in mind to redirect here n00bies asking for support about templates. Time permitting you could extend it to a more complete series of tutorials, don’t you think ? hahahah

(#1844)

Thank you Matt, i have a question about the navigation system in your site, how did you assigned an id for archives link? i tried to do that in wordpress but i don’t know how, so i hope you can write something about it.

And sorry for my poor english hahahah

(#1845)

Cloudy Layer Over the Blogosphere

Studying the Codex, from the very first: “What is a Blog?”, and muddling through the cloudy issue of Pingbacks and Trackbacks, I’m making notes as I go, hoping that I can understand some of these things….

(#1849)

I suppose I am one of the n00bs, you speak of. Learned html then CSS several years ago. I know enough to have all my pages validate properly, yet have no ability explain what part of the language is what (ex: selector, element, property etc.).. I just know how to make it work. ..So I decide to use WP.. Oi vey, find it very confusing to change the template. Those “template tags” are making my head swim! I appreciate your suggetion to think of them as HTMl with WP Temp Tags… After many searchs to attempt to understand how to change my layouts, this has been the most benifitial. Still struggling to understand what I have to do to make it all work, but your words have at least given me the confidence that I might yet still be able to “get it”.

(#1850)

If you haven’t already, how about throwing this little tutorial up on the Codex?

(#1853)

Meh, I still think that WP should use something like Smarty. Separating template logic from programming logic is much nicer.

(#1854)

Great work Matt, this will be very beneficial to lot’s of new users. 8)

(#1861)

If WordPress Templates are not PHP, then why use the confusing <?php ... ?> tag convention?

Surely <? the_title(); ?> would be less confusing for new users.

(#1862)

My biggest problem with Wordpress when I installed it was the insane range of places from which to get information about doing the most simple things with the templates. I’ll follow instructions and use a template system, regardless of syntax, but please please please don’t make me check pages and pages of Codex, all named for people who know what they’re looking for, a support forum, some old documentation, a plugin library, another plugin library, lots of individual homepages about plugins, articles like this… All just to work out how to get something that may or may not require a plugin but comes out of the box in MT. And none of the documents in these places consistently say which version of WP they refer to, so you can spend ages barking up the wrong tree. At least with MT, which I used to use, there’s one page for how templates work. Full stop. As it happens, I know php and am committed to the idea of open source and a robust architecture, but the frustration of the documentation really made me think twice about it. And now the disclaimer: I like WP, I use WP, I gripe because I care.

(#1863)

This is great, and I wholeheartedly support all your suggestions for simplifying the template system further. I’d add that given the amount of PHP in the default template, the ‘classic’ theme is nearly always a better model for people wanting to build their own templates; it may not be as full-featured, but that makes it a lot easier to reverse-engineer.

(#1866)

Well, it still is PHP. I understand what you are doing. But, honestly, I would have chosen a different title. Maybe, something that’s more like, “you don’t need to know PHP to modify a Wordpress template.”

Hrm … any thought?

(#1872)

Mark, the reason we don’t use the short opening tags (without the ?php) is that they don’t work as reliably cross-platform

(#1875)

Jeff, I didn’t mean to say MT template were not easy, cause they are very easy. I was just pointing out that eventhough the syntax is, as you say, alien to some, it is still easy to use.

Jemima, I agree with you, the documentation is all over the place right now. But there are many great people working on making that better.

Shayne, That would prolly have been a better title, but I like the sound of mine better.

Anonymous, the navigation is using PhotoMatt’s Intelligent Menus Script. It’s quite lovely.

(#1878)

Learn to write templates for WordPress

One of the major complaints with the two b2/cafelog derived blogging packages (b2evolution and WordPress) is that the template system is much harder to use than the MovableType template system. While there are some inaccuracies with the article which d…

(#1879)

I’ve supported several people trying to make sense of their templates and I have to disagree with you: WordPress “template tags” are PHP functions, plain and simple.

The two biggest causes of frustration: 1. passing arguments to WP functions requires knowledge of PHP. Do you use single quotes? Double quotes? Include the parameter name? The average HTML/CSS coder isn’t going to know this right off the bat. 2. too many WP functions output their own HTML tags, leaving users struggling to understand what the end result will look like.

As a final note, you really shouldn’t start off your article referring to new users as “n00bs” — it’s demeaning and deters your target audience.

(#1884)

i think its still PHP! :P

(#1885)

@jerome,

  1. You’re right they are PHP functions, but they are used as template tags. It does take time to learn, but it’s not PHP. Well, technically it is, but it’s also the way the WP Template Tags work. So I would say it’s not learning PHP, it’s learning the WP Template syntax. That’s why I say it’s not PHP.
  2. I agree completely. They can cause problems in outputting their own HTML. But most info they output is semantically proper. Not all, but most.

"n00bs" I don’t see as demeaning. I know I’m certainly a n00b in many many things. But yes, some may find it demeaning, bad choice on my part. Thanks for the input.

@b, I don’t. :D

(#1888)

Is there a “complete” manual for Wordpress”? I read tutotial here and ther. I could not find a more systematic “howto”. Am I missing something?

(#1896)

This Tutorial so Incridible for me, I learn something new from this Post

Thanks :P

(#1897)

This is awqesome. Beautifully written, very stylishly presented, and putting really useful but technical info in front of the end user in an accessible way. You have a gift my friend.

The real joy is that you have articulated clearly what many of us in our stuttering way have been trying to say. For that I thank you. It is not surprising that all the usual suspects hahahah are here in a supportive way. You deserve it.

(#1902)

Great article, terrible title. I suggest changing the title to “WordPress is not Programming”.

(#1914)

links for 2005-04-29

Picking Designer Homes Off the Rack | Business & Economics | Deutsche Welle | World Famous designers are doing pre-fab plans. That italian one with the pool looks incredible. (tags: modernhomes ecofriendly prefab architecture) easter pig roast A frien…

(#1969)

Please write more tutorials. You have a talent for it, and I would gratfully appreciate someone with the moxy to do it with panache. Please pickup where Podz left off, with your presentation skills ou would most definately become famous for it. Thank you for your help. Kudos!!!!

(#1977)

Für Wordpress braucht es keine PHP-Kenntniss

Das behauptet zumindest der ursprüngliche Autor dieses Templates … das ich übrigens an irgendeiner Stelle für den Opera unbrauchbar gemacht habe … Nun gut, wird auch wieder gerichtet, sobald ich den Übeltäter habe. Aber zum Templ…

(#1989)

Goddammit, this is exactly what I needed. The way people talk about (insert scary voice here) the loop makes it seem like some kind of monster that lives in a lair. But when you break it down like that, it seems much more simple. Thanks, at least now I partly understand what powers my blog!

(#2057)

Thanks for the write-up. Now if you could explain how CSS fits in, using equally simple terms and clear color-coded examples, I’d be most appreciative! I’ve figured out how to move and place “chunks” of data on my site inside HTML (these template tags), and even how to write the arguments properly (whoo hoo I guess I did some PHP). But now I’ve got CSS div span class scattered around, that I don’t know where to start, or even what program to open it with for making changes (Notepad? Firefox extension?).

(#2060)

I thought it was PHP till I really started editing some of the actual php files that run WP. It was then I realized what PHP was. codex.wordpress.org is key. Thank you Matt R. for this post.

(#2065)

Please put this on the codex. I’ve been trying understand that for 2 days. 2 long days. Great article. thanks.

Is there a model anywhere? what connects to what… what the hell are all those folders…

(#2066)

FINE! I’ll just delete your sig. boooo!

(#2070)

It seems that you know alot about wordpress. What is the php line of code that makes list. I want to put links to other site on my sidebar however I don’t know the code to make the list expand. I have seem them in other wordpress sites. Please email me back a.s.a.p. Thank You, Quy

(#2157)

Very straight to the point and it answered my doubts!! hahahah

(#2162)

HOW TO: Customize Wordpress Part 1…

There are two main ways that I can see to go about customising themes in WP. I’m not really a theme user, as I tend to build from scratch, so it’s difficult for me to say which way would be easier for everyone. One way is to customise the functionali…

(#2191)

Phew, this article encouraged me to get going. All I want to do is using the blogging functionality of WP as a better news script on small webpage of otherwise static pages. After all that confusion it looks like it is actually not hard to do it. Just design the webpage in Dreamweaver as always and add the WP tags where they are needed. No need to implement the whole site navigation and static sites in WP. :mrgreen:

(#2206)

People are getting hung up on semantics. Anything can be construed as anything. Ok we get that it’s actually PHP but it’s PHP templates so we can just say templates. And like Matt said, it’s easier for beginners to understand that way.

I don’t really like the lack of docs on the themes that come with WP. I can get around on my own but I dont really have that much time and would rather read a code map or something.

Useful article though…well done.

(#2247)

I couldn’t figure out how to install MT and I gave up before even trying very much. I tried Wordpress and I was hooked. It keeps getting better all the time. This tutorial is great. I have been monkeying around with template tags, for me it is fun to do. On the codex you can find all of the template tags, you can do copy and paste and just change the variables and you are set, in a way it is easier than doing html.

Thanks for helping me see things in a little easier light.

(#2253)

very nicely written… will give it a try

(#2265)

I ran across The Holy Grail of Layout and want to use it to create a WordPress theme. I can handle the CSS but I’m really frustrated by the lack of a foundation of template pages to begin the customization. I can see that starting with the Default theme or the Classic theme will eventually lead to what I want but my question is: Isn’t there a set of foundation pages for developing a new theme? The Default theme is a double column within a single column and the Classic theme is the same. At many of the CSS sample sites, there are one column layouts, two column layouts, etc. Is there anything like that for WordPress? If so, where? Thanks, by the way, this was a marvelous article! (I have used PHP for some of my web sites so it does not scare me but your theme of it is NOT PHP was intriguing and you point was VERY well made.)

(#2270)

Great article. Thank you very much.

(#2295)

Thanks so much for the good advice !

(#2302)

I don’t know who’s intelligence got insulted by this awesome tutorial but I think you did a great job. The WordPress Codex is next to useless when it comes to us “noobee” or whatever it is you people call us.

I understand “The Loop” a hell of a lot better now after reading this than I ever did reading the Codex over and over and over. Thank you for taking the time to put this together.

(#2311)

Hi dude, I really wish to thank you for your knowledge and how you explain very well all the things…

I’m quite new in php, I’m a dj but for my site I use WP. The problem is that I wish to modify my template, split the design, or for explain better have more pages into one page.

Btw, your explain to how work WP was very useful and interesting and I’m writing to you to thank you for your great work. Best wishes and greetz…

Alan

(#2315)

Thanks man, you just helped me to integrate worpress in my website layout. :lol:

(#2470)

I get this stuff all the time.. almost every serious sofware has similar template system and all people look is extension..

(#2633)

I need some help with templates in Pages. I have been using WP as a CMS and would like particular pages and their children/child pages to use certain templates. The thing is that I’d like to find a way to set the templates at one go and not set them for each page. Is that possible? How? Thanks hahahah

(#2636)

I don’t understand. Wordpress is PHP and will be, and it’s just great to have the templates in PHP compare to other CMS/Blogs around there. I aint catching this.

Related Posts