Welcome, visitor! Log in

KB Plugins blog

The best Wordpress plugins are free

PHP Tutorial for WordPress Users – PHP 101

Welcome to PHP 101, to the quickest, simplest PHP tutorial around, and the only PHP tutorial written specifically for the average WordPress user. I cover only the minimal amount of PHP necessary to competently modify your themes (and maybe even to modify plugins).

What this is

My tutorial is only a few pages long; you can read it in 20 minutes. It covers basic PHP syntax. It tells you how to assign a value to a variable, how to check that value, and how to set up a PHP if ... else ... statement. From my experience, that's all most WordPress users need (and want) to know. If you want more details later, try PHP.net's tutorial .

After you have mastered the basics, you can visit one of the subpages linked below to learn several little tricks that folks ask about frequently in the wp.org support forums :



Most tutorials are written in inaccessible, overly technical language. I'm going to write as clearly and non-technically as possible. So please don't complain if you notice me being a little less than precise.

Table of contents

As you click through the pagination links below, we'll address the following topics:

  1. Introduction (this page)
  2. What is PHP? PHP vs HTML
  3. How do I insert PHP into a web page?
  4. Variables
  5. Conditional statements (if, elseif, etc)
  6. Variables in conditional statements
  7. Functions, and the difference between echo and return
  8. Conclusion

Ready to get started? Click on "page 2" below. (Look below the ad.)

Tutorial navigation

To navigate through this tutorial, look for the pagination links immediately below this text. Some folks miss those links. Just thought I'd clear that up.

Pages: 1 2 3 4 5 6 7 8


  1. Posted December 5, 2007 at 1:23 pm | Permalink

    Can this be used to display a different chunk of sidebar content depending on what page number you are on?

  2. Posted December 5, 2007 at 6:20 pm | Permalink

    Yes, PHP can be used for that. See conditional tags.

  3. Posted January 7, 2008 at 8:33 am | Permalink

    I know I need to add the PHP file to the same directory as the page containing the PHP script but which FTP directory does WordPress store the posts in pls?

  4. Posted January 7, 2008 at 9:44 am | Permalink

    @gadget: For general WP support, please use the WP support forums. The short answer: WP uses MySQL, not PHP, to store data.

  5. Posted January 30, 2008 at 8:50 am | Permalink

    If the directory I am accessing from the site contains both index.php and index.htm ,What happens?

  6. Posted January 30, 2008 at 8:55 am | Permalink

    That’s not really a PHP question. It depends on your server configuration. But why ask? Just try it and see what happens.

  7. Posted January 30, 2008 at 9:23 am | Permalink

    If I use single quote to assign a string to a variable in the PHP,Can I define another variable with a double quote? I mean, can I mix and match single and double quotes according to my convince in the same PHP snippet or should I stick to only one type of delimiters throughout the snippet?
    Thanks a lot for the quick reply.

  8. Posted January 30, 2008 at 9:57 am | Permalink

    Nice tutorial! simple,easy, neat, to the point.And the best part is it inspired me to learn a new web language.Thanks a lot

  9. Posted January 30, 2008 at 10:08 am | Permalink

    Thanks. Regarding your question about quotes, you can switch back and forth all you want. But again… why ask? Just try it and see what happens. That’s the way I learned PHP.

  10. Posted November 18, 2008 at 2:32 pm | Permalink

    Hey thanks for the help I am still not getting it to work but at least I understand a bit better now. Very clean and easy to read tutorial.

    What I want to see happen is if somebody click about us the header changes and so does the sidebar to match the about us content ect. I will get there this was a good start, thinking it might just be easier to copy new directories onto my server lol.

  11. J Mann
    Posted December 22, 2008 at 4:23 pm | Permalink

    Excellent tutorial. Just wanted to say thanks for taking the time to write it! :)

  12. Posted February 3, 2009 at 12:02 am | Permalink

    Hi Adam,

    Thank you for your wonderful tutorial, it’s the best and the most relevant article that I have been looking for all this while!

    However, may I know how do you insert all these textboxes? It’s so cool and I hope I can know more. Any recommendations?

  13. Posted February 3, 2009 at 8:34 am | Permalink

    Ah Doe: I think you’re looking for this:


  14. genyou
    Posted February 3, 2009 at 10:18 am | Permalink

    I have download a template and run it in local host by using xampp, but when i try to get it the index.php, fatal error was occurred.

    Fatal error: Call to undefined function language_attributes() in C:\xampp\htdocs\gear\index.php on line 2

    I have try to do with php be4, but i never try to do WP template. Do i need any install extra stuff or kind of plugin or what to make WP source code can work in PHP.

    I did try something else like get_header(); is_home(); bloginfo();
    The same error message will appear, really hope that you can gv me some advice. Thanks very much.

  15. Posted February 3, 2009 at 10:20 am | Permalink

    genyou, this is not a wordpress support forum.

  16. Slipp
    Posted March 10, 2009 at 11:50 pm | Permalink

    Nice php tutorial!


  17. Posted March 12, 2009 at 2:35 am | Permalink

    Hi Adam,

    Thanks for looking out for me. I really appreciate it.

  18. Suzthembi
    Posted March 31, 2009 at 8:40 am | Permalink

    Was looking and looking for a quick quide on PHP with wordpress and found your pages very very helpful.

    Thanks for putting this together.

  19. cursed
    Posted April 23, 2009 at 11:12 pm | Permalink

    hello..how can i manipulate the custom fields and make its contents appear instead of the archive list?thanks

  20. Posted May 9, 2009 at 6:59 pm | Permalink

    I found your website very helpful. Thanks for posting the info.

  21. Suman Sindhu
    Posted September 22, 2009 at 4:32 am | Permalink

    I am learning wordpress and want to change some functions. I must know PHP for that. In how many day of practice, I will be able to do PHP coding at my own.

  22. Posted September 22, 2009 at 7:18 am | Permalink

    Suman Sindhu: Hours, not days. Why not just read the tutorial?

  23. Posted September 27, 2009 at 10:41 am | Permalink

    Very helpful tutorial on PHP for WordPress users.
    Thanks for sharing.

  24. Posted November 18, 2009 at 4:20 pm | Permalink

    Danke Adam!
    Piecing together my first WP blog (integrating it into my site) I needed this…
    Smooth transitions into learning curves (specifically php here) are often sorely lacking, so thanks for filling that gap.
    I’ve got some more feedback (re return and print values) but I’ve got to go pick up my daughter now… more later!


  25. Posted November 18, 2009 at 6:07 pm | Permalink

    …so smooth sailing but was momentarily thrown by the return and print values example
    <?php is_home(); ?> returns a value, but
    <?php the_content(); ?> has no return value

    Which prompted me to look into it further and discovered the difference between conditional ([link]), and template, tags.

    Thanks again!

  26. Posted December 9, 2009 at 12:15 pm | Permalink

    I understand the conditional tag if (is_home())
    I don’t know how can i do to return true in case of paged content in index.php. More specific for index i need true, and for the rest of the pages (page/2 , page/3 , page/4 , etc)=> false.
    Something like a sticky post.
    Can you help me?

  27. john harris
    Posted January 30, 2010 at 8:17 pm | Permalink

    From your example…

    // method #1
    $myVar1 = ‘Hey dude. ‘;
    $myVar1 .= ‘How are you?; // $myVar1 now holds “Hey dude…

    Appears to be a typo in line 3: no trailing apostrophe.
    You are a good teacher!

  28. Posted April 15, 2010 at 1:08 pm | Permalink

    Andrian: Wrong place. Go to wp.org and look up conditional tags.

    John: Thanks, fixed.

  29. Chris
    Posted May 9, 2010 at 10:12 pm | Permalink

    I am trying to enter a PHP script into a wordpress page but whe i enter it wordpress makes part of it disipear. Please help

    this is the code that i am trying to use. I have tried to download plugins that will allow me to enter a php code in a page but i cant get it to work.

    $row = 1;
    if (($handle = fopen("[link];, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    for ($c=0; $c < $num; $c++) {
    echo $data[$c] . "\n”;

  30. Posted May 10, 2010 at 8:13 am | Permalink

    Chris, this isn’t a general WordPress support forum. That’s over at wordpress.org.

  31. Jim
    Posted May 27, 2010 at 2:52 am | Permalink

    Great instruction, thanks.

    But… I copied and pasted the time code on my blog page and saved it. When I visited the site, the code did not render. When I returned to design mode, the php tags had been changed to comment tags. What did I do wrong?

  32. Posted May 27, 2010 at 8:10 am | Permalink

    Jim: WordPress won’t execute PHP that you write into the editor. It strips it out unless you use a plugin that runs PHP found in blog posts. Those plugins exist (for example), but they’re usually not a good idea. PHP should go in your plugins and in your theme files.

  33. Wordpress Developer
    Posted June 18, 2010 at 4:20 am | Permalink

    Nice article for wordpress users who wish to do customization on their own.
    But if you wish to just change the design or theme , you need to learn designing stuff too (HTML/CSS) etc,..

  34. Posted June 18, 2010 at 7:37 am | Permalink

    Yes, that’s why the article starts by saying that HTML and CSS are prerequisites.

  35. Posted September 22, 2010 at 2:59 pm | Permalink

    I was told to enter this code
    ECHO date(“M-d-Y”);
    to a page and it would display the date if I had php. Well the date did not display when I view the page. Then I added this in front <?php and it still did not work.

    I'm running a wordpress blog through bluehost. So I have to already have php set up. I see the code in my wordpress theme.
    What am I missing?

  36. Agatha
    Posted September 30, 2010 at 7:28 am | Permalink

    Excellent introduction to PHP, thanks :) Now to attempt to put it into practice…

  37. Posted October 25, 2010 at 6:06 am | Permalink

    I see this was posted some time ago, I gather the information hadn’t changed. Somehow I wish we (some of us) hadn’t gotten away from DOS. I swear that would help me now. I’m having the hardest time installing a premium theme and it shouldn’t be that difficult (I think).

    I’m at work now and will have to bookmark this for later, I will let you know what I was able to grasp.

    Thanks, I love the simple tutorials!

  38. Posted August 15, 2011 at 3:06 pm | Permalink

    Thanks. I require to design my website. I have to learn more, to modify the features based on my needs.

    Do you know any PHP Advanced tutorial like this?


  39. Posted October 9, 2011 at 4:33 am | Permalink

    Thank you very much. This tutorial was so clear to me. I hope you write an extension to this too.

  40. Aman
    Posted December 20, 2011 at 8:44 pm | Permalink

    Excellent tutorial. Thanks for presenting all the basics at one place.