WordPress, your theme, and your site’s plugins offer a wide range of recipes to extend and enhance core functionality. These methods often rely on a working knowledge of PHP.

This post will help non-developers using WordPress that want to do a bit more to customize their site. After reading this article, you will have a better understanding of what HTML is, what PHP is, how to safely copy and paste recipes into your site, and how to fix things if anything goes wrong.

Banner image for How to Copy and Paste Custom PHP Recipes

What is HTML

HTML is the markup language that structures your page for the browser. A variety of HTML tags form the entire output of your site content, and the browser renders these tags according to how you have structured your page.

Most HTML tags have a start and end tag, surrounded by angle brackets. For example:

<div>
<p>Your content goes here...</p>
</div>

What is PHP

PHP is a scripting language that renders code on the server-side. The result of the PHP scripts are returned to the browser as plain HTML. It’s specifically useful for PMPro because it allows the server to control what the member can see and do on every page of your Membership site.

A block of PHP code has a start and end tag and must return or echo content. For example:

<?php 
	echo "Hi, this is PHP!";
?>

Combining PHP and HTML

Your WordPress site is primarily built using a combination of HTML and PHP, although the new Block-based editor is introducing a heavy bit of JavaScript into the mix as well.

If you look in your site’s /wp-content/themes folder and browse the files, you’ll see nearly every template file is a .php file. When you open the files, you’ll see a heavy mix of both coding languages.

<p><strong>Welcome,</strong> <?php esc_html_e( $current_user->display_name ); ?>.</p>

Copy and Pasting PHP 101

If you were asked or read the instruction to “Copy and paste this ” here’s what you should be aware of.

  1. Never edit core WordPress, plugin or theme files directly.
  2. Functionality and design edits should be made in a helper plugin.
    • Paid Memberships Pro recommends creating a new customizations plugin, and many other plugins recommend the same method.
    • You can use a plugin for minor theme adjustments or, for more advanced customization, you may want to create a child theme.
    • Another popular customization method is to use the Code Snippets plugin by Shea Bunge. This is an open source plugin available in the WordPress repository.
  3. Most code recipes will be PHP-based functions.
  4. An opening PHP tag looks like this: <?php
  5. A closing PHP tag looks like this: ?>
  6. Always make sure to properly “open” and “close” PHP tags. PHP tags are opened and closed when you need to swap between PHP code and HTML.
  7. Make sure you don’t paste your new code recipe inside of another code recipe. Pasting your new code at the bottom of the file, after all other code, is safest.
  8. If you include a new open PHP tag inside of an existing PHP tag, you will get an error. To avoid this, you can either close the existing PHP tag or exclude the new open PHP tag from the code you are copying. For example:

Bad PHP

Bad PHP; Will result in errors
Nested PHP tags will throw an error.

Good PHP

Good PHP - no errors
Close the old tag first.

Better PHP

Better PHP - no errors.
Exclude the open/close tags when pasting into existing PHP code.

Code comments are often a part of a recipe and should be included when copying and pasting PHP. These comments will help you or another developer identify what that code is doing. A code comment looks like this:

// This function will adjust the renewal rate for members in level 1.

The last closing ?> tag in a plugin file is often omitted and it is completely safe and recommended by the official PHP documentation to do this.


About PHP Function Names

PHP function names must be unique. Many of recipes use a function name like my_pmpro_checkout_level. For your site, it would be smart to rename the functions to be even more specific to your site name and what you are using that function to do. For example, if you are using the pmpro_checkout_level filter to modify a level’s pricing for Platinum members, you could write a function name like kims_renewal_discount_for_platinum_members. This will help to ensure that you do not trigger a fatal error, usually of the flavor PHP Fatal Error: Cannot redeclare function.

If you see this error, most likely you have two functions of the same name and you must resolve this.

Some sites prefer to use the same prefix for all of their PHP function names. Let’s say, for example, your business’ is name “Initech”. You could name a function that filters the WordPress the_content hook initech_the_content, and a function that filters the WordPress the_excerpt hook initech_the_excerpt.

When updating a function name in your code, make sure to update the name everywhere the function is used. For example, in this PHP recipe, you would need to update the function name where the function is defined and also where it is passed as the second parameter to the add_filter call beneath the function.

Change PHP function names every time they are used.

About Action and Filter Hooks

WordPress, Paid Memberships Pro, and many of the other plugins running on your WordPress site use Action and Filter Hooks to allow you to customize your site experience or functionality without directly editing the plugin or core WP directly.

You can dive more into the Action and Filter Hooks in Paid Memberships Pro in our reference library here.

WordPress also offers a huge library of Action and Filter hooks you can browse. Our plugin and our recipes leverage many of the core WordPress hooks in these lists.

How to edit your customizations plugin and publish edits to your site.

We have a great guide on how to connect to your web server in this article. In this guide, you’ll learn how to log in via FTP. The WordPress Codex also has a guide on using FTP clients that is a helpful read.

v4.9+ of WordPress includes an improved “Plugin Editor” page under the Plugins menu in your WordPress admin. The editor offers syntax highlighting and improved error checking, which makes it more reliable as compared to past iterations. After saving a change to a plugin file, WordPress will run some checks to make sure that your edits did not result in a fatal error or white screen your site.

WordPress 5.2 plans to have improved fatal error protection to prevent cases where an error introduced through the editor takes down your entire site.

For all these reasons, you may think that editing plugin files in your dashboard is a safe way to go. Still, we do not recommended relying on this editing experience for actual development. If you are using the editor and an error occurs, you’re in a really tough situation. Editing and uploading files via FTP gives you an immediate path to repair the issue.

Read this if you break your site.

If you’re new to all of this, I applaud you for getting this far in the guide. You are starting to get a basic understanding of the code that powers your site. You’re daring enough to say, “Hey, I’m going to give this a shot!” You rock.

Even the most experienced developers will occasionally “white screen” their site. The best way to defend yourself from a white screen is to be prepared for when it, inevitably, happens. Here are few key guides to read before you endeavor to start copying and pasting PHP:

  • How to Set up a Data Backup Process for your WordPress Membership Site
    Before you endeavor to edit code on your site, it is important that you have a site backup process. This will ensure that you always have a restore point.
  • How to enable debugging on your WordPress site.
    In this guide, Travis will show you how to enable WP_DEBUG. This “mode” of WordPress will reveal PHP errors in more detail when and if they are present. With debug mode enabled, you’ll be able to track down the precise line of code that is causing the error.
  • How to deactivate a plugin causing an error
    Case: you’ve pasted code into your customizations plugin, uploaded the file to your web server, and your site is now broken. The guide in this article will instruct you on how to disable the plugin causing the error and restore your site.

Another way to prevent an error like this on your live site is to either develop on a Local Site or a Staging Site.

A local site is a version of your WordPress site that exists solely on your own computer. You can create a pretty close duplicate of your live site in a local environment by enabling all of your live site’s plugins and activating the same theme. Popular tools to create local sites include Local by Flywheel or MAMP. Local by Flywheel is a desktop application that makes it super simple to spin up a new local WordPress site. And here’s a guide from the WordPress Codex on setting up a local WordPress site using MAMP.

We’re going to write up a more robust guide on setting up a staging site because there are huge considerations for publishing a clone of your live membership site on the interwebs. If you are already using a staging site, please make sure you check out the Developer’s Toolkit Add On. This Add On offers several important features to make sure your staging site isn’t cancelling or expiring members, interacting with your connected gateway, or sending emails to members. Our biggest word of warning: do not duplicate your member data and do not keep a staging site connected to your live payment gateway.

Implementing customizations on a staging or local site will give you a very close approximation of what the change will be like for your live site, without any of the risk of interruption to your live site.

Ready to give it a try?

WordPress developers have spent large chunks of their lives exploring all the “what ifs” and “buts” related to writing, copying and pasting PHP. In my opinion, the best way to learn is to get your hands dirty and give it a try.

If you’re a premium member, just open a ticket in our support area for help as you endeavor to try editing PHP for your membership site. We have confidence in you.
Was this article helpful?
YesNo