KB Plugins

The best Wordpress plugins are free

Print this post

PHP Tutorial for Wordpress Users

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 :

Prerequisites

Disclaimers

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″…


 

27 comments »

1
Chris thinks
at 1:23 pm on December 5, 2007 #

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

2
Thus saith Adam
at 6:20 pm on December 5, 2007 #

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

3
gadget writes
at 8:33 am on January 7, 2008 #

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
Thus saith Adam
at 9:44 am on January 7, 2008 #

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

5
KVS setty writes
at 8:50 am on January 30, 2008 #

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

6
Thus saith Adam
at 8:55 am on January 30, 2008 #

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

7
at 9:23 am on January 30, 2008 #

Hello,
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
KVS setty contributes
at 9:57 am on January 30, 2008 #

Hello,
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
Thus saith Adam
at 10:08 am on January 30, 2008 #

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
Gusto writes
at 2:32 pm on November 18, 2008 #

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 remarks
at 4:23 pm on December 22, 2008 #

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

12
Ah Doe comments
at 12:02 am on February 3, 2009 #

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
Thus saith Adam
at 8:34 am on February 3, 2009 #

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

[link]

14
genyou adds
at 10:18 am on February 3, 2009 #

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
Thus saith Adam
at 10:20 am on February 3, 2009 #

genyou, this is not a wordpress support forum.

16
Slipp proclaims
at 11:50 pm on March 10, 2009 #

Nice php tutorial!

thanks!!!

17
Ah Doe thinks
at 2:35 am on March 12, 2009 #

Hi Adam,

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

18
Suzthembi contributes
at 8:40 am on March 31, 2009 #

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 adds
at 11:12 pm on April 23, 2009 #

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

20
Seth Long writes
at 6:59 pm on May 9, 2009 #

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

21
Suman Sindhu thinks
at 4:32 am on September 22, 2009 #

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
Thus saith Adam
at 7:18 am on September 22, 2009 #

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

23
at 10:41 am on September 27, 2009 #

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

24
Stefan contributes
at 4:20 pm on November 18, 2009 #

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!

Thanks!

25
Stefan remarks
at 6:07 pm on November 18, 2009 #

…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 (http://codex.wordpress.org/Conditional_Tags), and template, tags.

Thanks again!

26
Andrian adds
at 12:15 pm on December 9, 2009 #

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 remarks
at 8:17 pm on January 30, 2010 #

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!

Leave a comment

Comment Guidelines
  • Yes, your comments will be visible to everybody. (Unless you use the private contact form.)
  • Allowed HTML: <a> <b> <blockquote> <cite> <code> <em> <i> <strong>
  • Code: Put code in `backticks` (above your "Tab" key) or it won't display well
  • Gravatars: To override the default image by your comment, use a gravatar
  • Links: If you include more than one link, your comment will go into the spam queue

Please read before commenting: Because I am now employed and not just a student, I provide only minimal support for my plugins. Sorry.

If you have a bug report, feature request, or other general feedback about a plugin, please leave a comment—but do not expect an immediate response. If you are requesting help, though, please check the plugin's documentation thoroughly rather than ask your question as a comment.

Thank you for your understanding.