If you have a WordPress website and have already learned HTML and CSS and you’re ready to take the next step into learning PHP so you can customize your site more, this is the tutorial for you. When I was first learning PHP, I looked all over to try to find a book that was specific to WordPress, so I could learn what I needed to know quickly and be able to use it right away. I never found that book, so I have now decided to write it myself. PHP is a huge subject, so I plan to break up this tutorial over the next few weeks to make it easier to digest. Please be sure to see the other tutorials as well.
DISCLAIMER: This is by no means a complete PHP tutorial. It is only the very basics that you need to know to enable you to make changes on your WordPress site as soon as possible. And by all means, you should continue your PHP training after this. Also, WordPress is changing constantly. What works today may not work or be the standard tomorrow. Please be sure to backup your work constantly. I can not express enough the need to backup your work any time you work with PHP. A misplaced comma, extra period, even an extra space can take down your whole site. I like to use the version method adding a new number to each revision of my files. That helps me keep track of changes and go back to the file that works at any time.
Okay, so let’s begin at the beginning.
A PHP script starts with
That lets the server know that the code is changing from regular HTML. If you are creating only PHP, that's all you will need at the beginning of the file, but if you're going to be using PHP with HTML, you will need to add ?> at the end of your PHP code to signify the closing of the php information. Also, any file that you create needs to end in .php. For example, functions.php. Also, all PHP statements are case sensitive, so watch those capitals! I recommend you don't use them unless necessary. So, to review- files using PHP should start with
and end with
?>You can then use HTML and then add more PHP by adding the
again.
However, there is no?>at the end of WordPress php files. Only use this ending if you will be adding other non-php code after it.
Pay attention to quote marks.
You will notice that single quotes and double quotes are often used interchangeably in php, but it is important to note their relationship to each other. If you create a statement with only one set of quotes, it doesn't matter if you choose single or double quotes, but I do recommend that you choose your favorite and stick with it for consistency. Consistency is very important in coding. For me, I prefer double quotes, since that is what you use in HTML. But, if you are using a statement that requires more than one set of quotes, you need to be aware that the quotes begin and end that section of the code. In order to use that second set of quotes, you need to use the other kind of quotes so you don't end section of the code prematurely.
For exampleecho "Click here";Notice that the url has single quotes.
Also, if you need to use an apostrophe, you will need to use the backslash like this:$myVar='How\s it going?';Each code line in PHP must end with a semicolon.
The semicolon is a separator and is used to distinguish one set of instructions from another.
Comments
Comments notations are the same as in CSS. So you can begin the line with two forward slash marks like this:
//This is a PHP comment lineor you can use the forward slash with the asterisk like this:
/* This is a PHP comment block */Vocabulary
- there's no way around it, you are going to need to learn some.
Let's start with one of the basics:echo - simply used to display something, so the following code with simply display on the screen as "Hello World!"
The WordPress Codex lays this out nicely for you here:
codex.wordpress.org/Function_Reference
Luckily, like most vocabulary, these functions are designed with root words like "get", "is" and "remove" that help us understand exactly what these functions do.Please note that w3schools.com has a great overall reference here:
See all the tutorials here: https://wpdecoder.com/topics/PHP-for-Wordpress
Sayeed says
A nice starting guide about php and wordpress.