How To Safely Add PHP Code To WordPress
The tutorials in this section give you PHP to add to your site. Where you put it decides whether it survives the next update, and whether a typing mistake takes the site down. Never add it to Events Manager's own files: an update replaces them.
There are four reasonable places. For anything you intend to keep, use a plugin of your own.
Compare the options
| Place | Survives theme updates | Survives a theme switch | Can be switched off in wp-admin | Best for |
|---|---|---|---|---|
| Your own plugin | Yes | Yes | Yes | Anything permanent, and anything you'll reuse on another site |
| A must-use plugin | Yes | Yes | No | Code that must always run and that nobody should deactivate |
| A code snippets plugin | Yes | Yes | Yes | Small snippets on a site where you have no file access |
A child theme's functions.php | Yes, if it is a child theme | No | No | Code that only makes sense with that theme |
Create your own plugin
-
Create a folder in
wp-content/plugins/, for examplemy-events-custom. -
Inside it, create a PHP file with the same name and a plugin header:
wp-content/plugins/my-events-custom/my-events-custom.php<?php/*** Plugin Name: My Events Customizations* Description: Site-specific changes to Events Manager.* Version: 1.0.0*/defined( 'ABSPATH' ) || exit;add_action( 'events_manager_loaded', function () {// Add your hooks here.} ); -
Activate it in Plugins then Installed Plugins.
Registering your hooks on events_manager_loaded means your code only runs when Events Manager is active, so deactivating Events Manager doesn't cause errors about missing classes or constants.
Create a must-use plugin
Any PHP file placed directly in wp-content/mu-plugins/ is loaded on every request, before normal plugins, and can't be deactivated from wp-admin. Create the folder if it doesn't exist. On multisite, the file runs for every site in the network.
A must-use plugin needs no header, though adding one makes it show a name under Must-Use on the Plugins screen. Files in subfolders are not loaded automatically.
Because must-use plugins load before Events Manager, wrap your code in the events_manager_loaded action here too.
Use a code snippets plugin
Plugins such as Code Snippets or WPCode store PHP in the database and run it for you. They are convenient when you can't reach the file system, and most of them catch a fatal error on save. The snippets live in the database, so they are not part of your version control or file backups.
Use the theme
Code in a theme's functions.php runs only while that theme is active, and a theme update overwrites the file. If you use the theme, use a child theme so that updates to the parent leave your code alone.
Events Manager also loads one theme file of its own accord: plugins/events-manager/functions.php inside the active theme (the child theme is checked first). It is loaded on init, after Events Manager's classes are available, which makes it a tidy place for Events Manager code that belongs with a theme's template overrides.
Before you paste anything
- Take a backup, or work on a staging copy.
- Keep a way in that doesn't depend on wp-admin, such as SFTP or your host's file manager. If a snippet causes a fatal error, renaming the file or folder switches it off.
- Turn on
WP_DEBUGandWP_DEBUG_LOGinwp-config.phpwhile you work, and readwp-content/debug.logwhen something doesn't behave. - Give your functions a prefix of your own (the tutorials use
my_em_) so their names can't clash with another plugin's. - Don't paste the same snippet in two places. PHP stops with "Cannot redeclare function" when a function is defined twice.
Test it
Add this to your plugin, then load any page of wp-admin as an administrator:
add_action( 'events_manager_loaded', function () {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-info"><p>My Events Customizations is running with Events Manager ' . esc_html( EM_VERSION ) . '.</p></div>';
} );
} );
A notice with the Events Manager version number appears at the top of the screen. Remove the snippet once you have seen it.