Doc WordPress WordPress Tricks, Tips & Snippets


Prevent change of positions of admin meta boxes

For some clients I prefer to really lock down the WordPress admin. They get confused if they accidently move one of the many meta boxes in the backend and then they'll call and tell me that the system wouldn't work anymore. So I was gald to find this little snippet which will prevent that any change of the meta boxes will be saved:

/* Prevent Save of backend Meta Box positions */
 add_action('check_ajax_referer', 'prevent_meta_box_order');
function prevent_meta_box_order($action) {
	if ('meta-box-order' == $action ) {

Just put this in your functions.php and you're set!


Load a custom CSS file in WordPress admin

With this little snippet you can load your own css file into the WordPress admin which will allow you to easily hide unneeded items to keep the admin clean and tidy and avoid unnecessary confusion for our clients:

// The function include a css files for backend
function backend_css_style() {
	echo '<link rel="stylesheet" href="' . get_stylesheet_directory_uri() . '/admin.css" type="text/css" media="all" />';
add_action('admin_head', 'backend_css_style');

Just this into your functions.php and also put a "admin.css" file into your child theme folder 🙂


Allow registration of new WordPress users without email address

As you know WordPress requires each user to have a unique email address. This is so that the system can make sure each user is unique using the double opt-in mechanism and also to be able to send him system messages like password reset links etc.

But there are definitely circumstances under which it might be necessary to allow registration of users without email address in the backend. For this I found a great snippet on stackexchange which works perfectly:

// This will suppress empty email errors when submitting the user form
add_action('user_profile_update_errors', 'my_user_profile_update_errors', 10, 3 );
function my_user_profile_update_errors($errors, $update, $user) {

// This will remove javascript required validation for email input
// It will also remove the '(required)' text in the label
// Works for new user, user profile and edit user forms
add_action('user_new_form', 'my_user_new_form', 10, 1);
add_action('show_user_profile', 'my_user_new_form', 10, 1);
add_action('edit_user_profile', 'my_user_new_form', 10, 1);
function my_user_new_form($form_type) {
    <script type="text/javascript">
        // Uncheck send new user email option by default
        <?php if (isset($form_type) && $form_type === 'add-new-user') : ?>
        <?php endif; ?>


This code not only removes WordPress requirement for a user to have ANY email address but also the corresponding form validation and the "required" text in the form fields label!




Strip shortcodes but keep the content within

Sometimes you might need to strip enclosing shortcodes from some content for an excerpt or teaser output. of course WordPress has a useful function for this called strip_shortcodes(), but this function also strips the content in-between the enclosing shortcodes.

If you want to strip the enclosing shortcodes itself but keep the content in-between you can use a regex like this:


$content = '[someshortcode]example text[/someshortcode]';
$result = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $content);

echo $content; // example text


This would strip the shortcodes but leave the enclosed text unchanged.


Fix access problems after domain / site url change

Whenever you need to change the domain under which your WordPress installation is to be found it is necessary to change both the "WordPress Address (URL)" setting and the "Site Address (URL)" setting, which are found in WordPress's backend under "Settings" > "General".

Right after you click "Save" you are logged out (because your session is bound to the domain) and sometimes it is tricky or even impossible to login again, for example if you entered an incorrect domain or something like that. Gladly there are two easy ways to change these crucial settings through either the functions.php (which can be found in your active theme's folder) or the wp-config.php (which can be found in the website's root)!

Method 1: wp-config.php

Add this to your wp-config.php:


Please note: as long as this entry exists in the wp-config.php the values can't be changed anymore via backend, this is like hardcoding the values.

Method 2: functions.php

Add this to your functions.php:


Please note: it is important to remove this entry once you are back in backend and and the site is running normally again!

Et voilà: you should be able to log in again now. If not clear your browser's cache!