Disclosure: I may earn affiliate revenue or commissions if you purchase products from links on my website. The prospect of compensation does not influence what I write about or how my posts are structured. The vast majority of articles on my website do not contain any affiliate links.

When my buddy brought me in to develop custom software for his WordPress website, I wished that there was a guide called “PHP for people who don’t feel like learning PHP.” Years later, I still wish that guide existed. For those with programming experience, learning PHP isn’t an insurmountable task, it’s just annoying. I’ve decided to put together some of the features I most frequently use that can be hacked into code without understanding all of the nuances of the language.

Why PHP?

PHP is a serverside scripting language. If you don’t yet know why you can’t just use client-side Javascript for everything, I don’t think you’ll find this guide particularly helpful. PHP gets a lot of hate, but when WordPress was founded circa 2003, PHP made the most sense. It still makes a lot of sense, though there are now tons of alternatives. PHP, at a fundamental level, allows you to do things such as query a database and create templates for pages which can’t be accomplished by simple Javascript.

Print PHP variables using client-side Javascript

When hacking through your production code, you’ll sometimes want to print out your serverside variables. Sure, you can log them serverside, but you may prefer to display them in a web browser or console. On one hand, you can always just `echo` things but you may find it preferable to print things to the console. Here’s how:

echo “<script>console.log( ‘Debug object: ” . $my_variable . “‘ );</script>”;

What this does is inject a javascript script tag wherever you find convenient. You can then find your output in the console.

Setting Session Variables

When designing a payment or signup process in WordPress, which will often require some custom coding, you will find session variables to be a godsend. This being said, too many session variables means you’re a bad developer and you should be ashamed of yourself.

Session variables are variables attached to a user’s session as they browse your website. They are useful because they aren’t explicitly passed from function to function or page to page; they’re just there. This can open the door to some very lazy programming.

To initialize a new session, use `session_start();`. Remember this when creating a login process, as there comes a point at which you’ll want to reinitialize a session and ditch all the old session variables.

To set a session variable, just use `$_SESSION[‘my_variable’]`. This is a key-value map. Once you’ve set the variable you can access it anywhere. As you can imagine, if you overuse session variables, it can make your code very hard to work with and modify as there won’t be a clear way to trace the origin of variables from other files.

To clear your session, use `session unset`. Alternatively, you can use `session destroy`. This SO answer provides more context on the differences.

How to send email

WordPress provides a function called wp_email that you can use to send emails. It’s pretty straightforward. Here’s an example:

$to = array('tim@tjohearn.com');
$subject = 'Someone signed up for something!';
$body = Here are more details . . .<p></p>";
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($to, $subject, $body, $headers);

How to see WordPress errors

By default, error reporting is turned off. When you’re running production code, you don’t want anything resembling a full stack trace of serverside code failure to be displayed to your users. This could be a big security vulnerability. Sometimes, though, you’ll need to do this (hopefully in a prod-copy dev environment!). To see errors, turn on WP_DEBUG in your wp-config.php file.

define( ‘WP_DEBUG’, true );

Make sure to turn it off when you’ve solved the problem. You probably forgot a semicolon.

How to interact with a Database

Chances are you’ll need to do this often. It’s important to have some experience with MySQL databases because PHP can tend to obfuscate what’s actually going on. When it comes time to debug SQL, you can quickly exhaust yourself by blindly trying things. This is the one area that you can’t really hack your way through. Here is the function anyway:

$conn = mysql_connect($servername, $db_username, $db_password);
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}
$sql = 'select * from DB where something=1 limit 1';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if ($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
  // do something
}

I will add to this document as time goes on. Hope this saved you some Googling.

 

PHP for WordPress Crash Course