Welcome to the WordPress Codex, the online manual for WordPress and a living repository for WordPress information and documentation. The minimum PHP version that is compatible with WordPress is PHP 5.2.6. Although your host may offer this as an option, it's not necessarily the best or safest. Other PHP versions compatible with WordPress are PHP 5.3 – 5.6 and PHP 7 and higher. WordPress recommends using PHP 7.4. WordPress (WP, WordPress.org) is a free and open-source content management system (CMS) written in PHP and paired with a MySQL or MariaDB database. Features include a plugin architecture and a template system, referred to within WordPress as Themes. PHP Compatibility and WordPress Versions. WordPress aims to support new versions of PHP PHP The web scripting language in which WordPress is primarily architected. WordPress requires PHP 5.6.20 or higher on the day they are released as much as possible.
WordPress 5.2 is targeted for release at the end of this month, and with it comes an update to the minimum required version of PHP. WordPress will now require a minimum of PHP 5.6.20.
Beginning in WordPress 5.1, users running PHP versions below 5.6 have had a notification in their dashboard that includes information to help them update PHP. Since then, the WordPress stats have shown an increase in users on more recent versions of PHP.
Why You Should Update PHP
If your site is running on an unsupported version of PHP, the WordPress updater will not offer WordPress 5.2 to your site. If you attempt to update WordPress manually, that update will fail. To continue using the latest features of WordPress you must update to a newer version of PHP.
When updating to a new version of PHP, WordPress encourages updating to its recommended version, PHP 7.3. The PHP internals team has done a great job making its most recent version the fastest version of PHP yet. This means that updating will improve the speed of your site, both for you and your visitors.
This performance increase also means fewer servers are needed to host websites. Updating PHP isn't just good for your site, it also means less energy is needed for the 1-in-3 sites that use WordPress, so it's good for the planet.
How to Update PHP
If you need help updating to a new version of PHP, detailed documentation is available. This includes sample communication to send to your host for them to assist you. Many hosting companies have published information on how to update PHP that is specific for them.
5.6 now, but soon 7+
This is the first increase in PHP required version for WordPress since 2010, but may not be the only increase in 2019. The WordPress core team will monitor the adoption of the most recent versions of PHP with an eye towards making PHP 7+ the minimum version towards the end of the year.
WordPress, the content management system the internet loves. You can use it for years without needing to tackle PHP, but eventually you're finding yourself needing it. You go to Bing and search 'php for beginners' and you find yourself here. The journey to learn PHP for WordPress development is long, but let's start!
We'll kick off this WordPress coding tutorial with some understanding of PHP's role in WordPress, and then start to build up from there.
Using PHP in WordPress: Useful for Everyone, Necessary for Developers
You don't really need to ever write PHP code as a WordPress user, WordPress business owner, etc. A minority of the people who use WordPress on a daily basis even know what PHP is, never mind know how to write code in it. But WordPress developers, WordPress developers must use PHP. But I'm getting ahead of myself…
WordPress Runs atop PHP on the Server
Before we jump fully into our PHP for beginners tutorial, some background:
Web servers, it turns out, are just computers. And those computers need to have underlying layers they can run on. For most WordPress sites, that breaks down to be:
- Linux (the operating system, like Mac OS or Windows),
- Apache (the web server, thing your browser talks to),
- MySQL (the database, where posts live),
- and PHP (which coordinates with the database, OS, and files to build web pages).
I wrote a lot more about this in our 'WordPress LAMP' article:
PHP is a Programming Language
So, hopefully the above made you aware that PHP is something that WordPress uses under the hood. It's a programming language, and the language that WordPress server-side code is written in. (In the web browser of both administers and visitors, WordPress often also involves languages called HTML, CSS, and JavaScript).
PHP was one of the first and most popular languages that people have ever used to build HTML documents (aka 'web pages.') Its popularity is a little more complex than I want to cover here, but I did write a 'Why PHP' article over on Thoughtful Code for those who are interested in that.
In short: PHP is a logic-programming language which you can use to control which HTML a page shows, either in WordPress or outside of it. The files that make up both WordPress themes and plugins are mostly using PHP to build the pages that you see when you visit a WordPress site in your web browser.
Learn PHP for WordPress and You'll be Able to Modify Themes, Make Plugins
As we just covered, both WordPress plugins and themes use a lot of PHP. Essentially, everything in a plugin is enabled by the PHP code you write. For a WordPress theme, some functionality is coming from WordPress PHP and some will be in PHP you write into your theme template files. You'll generally need less PHP expertise to make good themes than good plugins for WordPress, but it's an important skill in either case.
While we won't get into this much in this introductory PHP WordPress tutorial, for those wondering, the basic way that WordPress plugins work is with WordPress hooks: actions and filters. If you already understand what PHP function, variable, and strings are, you can jump right into that with this guide:
A Beginner's PHP Tutorial for WordPress
Alright, now that we've got that WordPress stuff out of the way, we can start in earnest on our short PHP programming for beginners tutorial. We'll focus on a few core things: what PHP looks like, what things you must understand to make any sense of PHP, and what next steps make sense.
PHP 101: Where We Start on a PHP Tutorial for Beginners
So, PHP as we mentioned above started as a way to create more dynamic HTML. As such, you'll know you're writing PHP, and not HTML, in a .php
file because it'll be boxed in by what are most commonly called 'PHP tags.' Those PHP tags are things that fence off PHP from your HTML, and vice-versa. Although there is still some interaction.
Here's an example:
What would loading file.php
up from your web server show you in your web browser? It'll show the words, 'Hi from PHP'. (The word echo
in PHP essentially lets something exit PHP-interaction-land and show on the page. What's more, if you viewed the page source in that browser, you'd also see that the opening and closing tags come through. Where not controlled, all HTML from a PHP file just shows in your browser.
Last note: that first line, which starts
We just showed your first PHP data type: the string. A 'string' is a common programming-language term for a sequence of characters. In our specific case above, our string was the sequence of characters 'Hi from PHP'. In PHP, a string can be differentiated from other words (the ones that are just the program itself) via either single-quotes or double-quotes. So both of these are equal:
What will the above do? It'll show 'Hi WPShout Reader!' in your web browser. How? Through the use of a variable. What's a variable?! Great question. A variable is a stored value. This term is common across most programming languages including PHP. Variables can be many things: strings, integers (i.e. 1, 2, 99, 12931298), floating-point numbers (0.123, 4.39, 6.928348723074082370974092384), things called arrays or objects, and whole lot more.
In PHP, all variable names will start with a dollar sign ($
). They'll then have at least one character after that dollar sign, which is unique. If you, as we do above, set (using the equal sign) a variable twice (or more), it will always have the later-set value. You can use this to 'vary' the value of a variable. (The above code does not.)
You'll also note that in the last code sample we didn't have a 'closing PHP tag' ?>
. This is because those aren't strictly necessary at the end of a .php
file. They're sometimes used, but if you're not going back to 'HTML land', it's a best practice that you don't use closing PHP tags at the end of files.
Functions: Where You Start to Learn PHP for WordPress Development
Another thing we must cover to complete our little PHP 101, WordPress PHP tutorial, is functions. Functions in PHP are essentially another name for 'stored procedures.' A stored procedure can be something as simple as an adder, which adds two value together, or a 'show-er' function that just shows a specific string.
A basic add function in PHP would be defined like this:
The word function
tells PHP that the next thing it should see will be the name of the function. Then there are parenthesis with what are called 'arguments' or 'parameters.' A function can have no parameters, which would look like function no_arguments() {}
.
Finally, most functions will return something, though they don't have to. In PHP, a function returns one (1) thing. In our case, our function returns a number. It's important to know that a return value isn't shown in your browser by default. Instead it can be shown with echo
, saved to a variable like $value
, of have further operations performed on it.
That's why in the code above HTML wouldn't show anything from our PHP code until we did our echo $value
, which shows the value stored in the $value
variable, which for this specific code will be 4.
Basic Conditionals (If, Then, Else)
Another thing you'll get a lot of value from when learning PHP for WordPress is coming to terms with conditional statements. If you're able to live a day in the modern world, you understand conditional logic. And one of the fun and surprising things about learning PHP for most beginners in the pleasant shock of how 'normal' conditional logic in PHP (and many other languages) is like normal thinking. The syntax is a bit like this:
In the above PHP code, which will happen?
- The parameter-less
execute_function
function is called and executed, or - the string
'Too small'
will be printed into the HTML?
If you guessed that the function will be called (#1), you may have forgotten that >
is a 'greater than' sign, and that 5 is not 'greater than' 5. I think about the above code as saying 'If $value
is bigger than five, call function execute_function
. Otherwise, echo 'Too small'
'.
endif
, endwhile
and other Conditional Syntaxes
You should also note that in the above code sample our logic was bound by 'curly braces,' {}
. This is common in PHP, and many other languages. These curly brace are a unique and useful delimiter of the difference between different conditions.
In PHP, because you're often mixing it in with HTML, you'll sometime logically have a dangling closing curly brace . These are annoyingly hard to understand. So especially in HTML-heavy situations (like WordPress theme template files), many people like to use a more verbose kind of condition. So the following lines are just like the above:
There are a few big changed here. The most important one is that we've switched from curly braces to colons :
and an endif;
. These work the same, but one makes the closing of which tag quite clear. Dangling curly braces are a bad time, especially when you've shifted from HTML to PHP and back again. This is common in WordPress theme files, because of what are called WordPress conditional tags.
We'll kick off this WordPress coding tutorial with some understanding of PHP's role in WordPress, and then start to build up from there.
Using PHP in WordPress: Useful for Everyone, Necessary for Developers
You don't really need to ever write PHP code as a WordPress user, WordPress business owner, etc. A minority of the people who use WordPress on a daily basis even know what PHP is, never mind know how to write code in it. But WordPress developers, WordPress developers must use PHP. But I'm getting ahead of myself…
WordPress Runs atop PHP on the Server
Before we jump fully into our PHP for beginners tutorial, some background:
Web servers, it turns out, are just computers. And those computers need to have underlying layers they can run on. For most WordPress sites, that breaks down to be:
- Linux (the operating system, like Mac OS or Windows),
- Apache (the web server, thing your browser talks to),
- MySQL (the database, where posts live),
- and PHP (which coordinates with the database, OS, and files to build web pages).
I wrote a lot more about this in our 'WordPress LAMP' article:
PHP is a Programming Language
So, hopefully the above made you aware that PHP is something that WordPress uses under the hood. It's a programming language, and the language that WordPress server-side code is written in. (In the web browser of both administers and visitors, WordPress often also involves languages called HTML, CSS, and JavaScript).
PHP was one of the first and most popular languages that people have ever used to build HTML documents (aka 'web pages.') Its popularity is a little more complex than I want to cover here, but I did write a 'Why PHP' article over on Thoughtful Code for those who are interested in that.
In short: PHP is a logic-programming language which you can use to control which HTML a page shows, either in WordPress or outside of it. The files that make up both WordPress themes and plugins are mostly using PHP to build the pages that you see when you visit a WordPress site in your web browser.
Learn PHP for WordPress and You'll be Able to Modify Themes, Make Plugins
As we just covered, both WordPress plugins and themes use a lot of PHP. Essentially, everything in a plugin is enabled by the PHP code you write. For a WordPress theme, some functionality is coming from WordPress PHP and some will be in PHP you write into your theme template files. You'll generally need less PHP expertise to make good themes than good plugins for WordPress, but it's an important skill in either case.
While we won't get into this much in this introductory PHP WordPress tutorial, for those wondering, the basic way that WordPress plugins work is with WordPress hooks: actions and filters. If you already understand what PHP function, variable, and strings are, you can jump right into that with this guide:
A Beginner's PHP Tutorial for WordPress
Alright, now that we've got that WordPress stuff out of the way, we can start in earnest on our short PHP programming for beginners tutorial. We'll focus on a few core things: what PHP looks like, what things you must understand to make any sense of PHP, and what next steps make sense.
PHP 101: Where We Start on a PHP Tutorial for Beginners
So, PHP as we mentioned above started as a way to create more dynamic HTML. As such, you'll know you're writing PHP, and not HTML, in a .php
file because it'll be boxed in by what are most commonly called 'PHP tags.' Those PHP tags are things that fence off PHP from your HTML, and vice-versa. Although there is still some interaction.
Here's an example:
What would loading file.php
up from your web server show you in your web browser? It'll show the words, 'Hi from PHP'. (The word echo
in PHP essentially lets something exit PHP-interaction-land and show on the page. What's more, if you viewed the page source in that browser, you'd also see that the opening and closing tags come through. Where not controlled, all HTML from a PHP file just shows in your browser.
Last note: that first line, which starts