The Theme Customization API has become a great thing in WordPress and gives much power to the user. But some settings should never change, like colors for corporate identity. To keep them from accidental change or loss you can override their value no matter what. Let me show you how.Read the more up to date and famous one afterwards!
Everything cool and wrong about upcoming WordPress 4.7Open in new tab for later
Theme support for header and background
First things first – the basics. With the Theme Customization API you can create this neat little dialog. It allows the user to change colors of his page with ease.
The image shows the “custom-background” support from the twentysixteen theme.
Lets zoom out a bit from this thing. The possibility to change the background has been added using the “add_theme_support” function. Besides that we can see a section called “custom-header”. Of course this is responsible for the header design. Another important part of the website, that can be changed easily.
Now that we know of the two parts, imagine how simple it is to do something wrong there. Or maybe you like to suppress changes so that the final website is a bit more stable. Lets take a quick look what settings can be made by the user and what their defaults are.
Default values for customer-background
You can determine how the background should look like. The defaults for the “custom-background” are:
$defaults = array( 'default-image' => '', 'default-repeat' => 'repeat', 'default-position-x' => 'left', 'default-attachment' => 'scroll', 'default-color' => '', 'wp-head-callback' => '_custom_background_cb', 'admin-head-callback' => '', 'admin-preview-callback' => '', );
Default values for customer-header
Settings for the header can be …
$defaults = array( 'default-image' => '', 'random-default' => false, 'width' => 0, 'height' => 0, 'flex-height' => false, 'flex-width' => false, 'default-text-color' => '', 'header-text' => true, 'uploads' => true, 'wp-head-callback' => '', 'admin-head-callback' => '', 'admin-preview-callback' => '', );
Those are a lot settings but only a few of them can be made immutable.
Force layout with constants
To assert that one of the settings above is always correct (in manner of the CI) you can force theme settings by using constants. I will show one simple example and then give you a list of possibilities that work just the same.
Imagine the major color of the company is blue and will always be. As seen above the theme has the setting “default-text-color”. Every admin can change it unless you do something like this:
define( 'BACKGROUND_COLOR', '#00f' );
Such a great color! This single line in your “wp-config.php” makes it impossible for the user to change the default text color. Almost any setting from above has a constant, that overrides the user-input. For the “custom-header” the overriding constants are:
default-color => BACKGROUND_COLOR default-image => BACKGROUND_IMAGE
And for the header the enforcing constants are:
default-image => HEADER_IMAGE width => HEADER_IMAGE_WIDTH height => HEADER_IMAGE_HEIGHT flex-height => true, if HEADER_IMAGE_WIDTH is NULL flex-width => true, if HEADER_IMAGE_HEIGHT is NULL default-text-color => HEADER_TEXTCOLOR header-text => ! NO_HEADER_TEXT (inverted)
On the one side you limit the possibilities for the user and he might be upset about it. Changing colors, logos and other things might be a thing he likes to do. But then think about how often the corporate identity changes or you accidentally click a button. This is a thought that leads to the conclusion, that a user or admin shouldn’t need that control and could get a more stable WordPress. Just decide for yourself how few or much your customer should do.
I would set this constant whenever the corporate design is given. Having unstable applications makes me sleep fitfully. So I better do my thing and lock it down.