Starting on Backend WordPress Development • WPShout

[ad_1] 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 a little summary 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, or other similar role. 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: A WordPress LAMP?! An Introduction to WordPress Infrastructure 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 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. (Though in 2022, the amount of PHP you’ll see in themes has just gone down…) 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 HTML 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: WordPress Hooks, Actions, and Filters: What They Do and How They Work 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: <!– file.php –> <html> <?php echo ‘Hi from PHP’; ?> </html> 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 <html> 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 <!– is an HTML comment. Comments are lines in code which shouldn’t do anything, but may help you or another programmer make sense of the program later. In PHP, most comments are broken out with to forward slashes, // comment here, or fenced with /* */ characters, like this: <?php /* Nothing in these lines will show or do anything */ echo ‘Not a comment’; // What’s to the left will run, but this text itself won’t // Neither will this ?> You’ll also want to note that our echo line ends with a semicolon. All lines of PHP will generally end with a { (of which more later) or a semicolon ;. Statements like echo should always end with a semicolon. This is a kind-of-strange convention across many programming languages. Variables, Integers, and Strings, Oh My! 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

Continue reading