Customizing Primer with hooks
Primer has contains many built-in actions and filters that allow you to fine-tune the behavior of the theme.
You should never edit theme files directly, otherwise your customizations will be lost when the theme receives updates.
For that reason, it is important that code-level customizations to Primer (and/or its child themes) be done inside a must-use plugin.
Must-use plugins (MU Plugins) are installed in a special directory inside the
wp-content
directory and are automatically activated on your WordPress site. Must-use plugins do not show in the default list of "Active" on the Plugins page of the WordPress Dashboard, and they can only be deactivated by removing the plugin file from themu-plugins
directory.
- Use SFTP or SSH to access the filesystem of your WordPress install.
- Inside the
wp-content
directory of your WordPress install, create a new directory namedmu-plugins
-
Set file permissions on the
mu-plugins
directory to0755
. - Save a new file on your computer named
my-customizations.php
and open it in favorite code editor. - Place all the necessary PHP code for your hooks into the file and save it.
- Upload the
my-customizations.php
file to your web server inside the newly-createdwp-content/mu-plugins
directory. - Set file permissions on the
my-customizations.php
file to0644
.
<?php
/**
* Plugin Name: My Customizations
* Description: Theme customizations and overrides.
* Author: Your Name
*/
// Turn off the author credit in the site footer
add_filter( 'primer_author_credit', '__return_false' );
In the example above, we added a filter for primer_author_credit
(which returns true
by default) to now return a value of false
, thus removing the credit from the site footer.
In your WordPress Dashboard, navigate to Plugins > Must-Use and you will see your new plugin listed here. Notice that you cannot Deactivate or Delete a must-use plugin. Your plugin will be automatically loaded with WordPress before themes or plugins are loaded, and will not be affected by other plugin, theme, or core updates.