Doc WordPress WordPress Tricks, Tips & Snippets

19Sep/180

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 ) {
		die('-1');
	}
}

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

1Aug/180

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 🙂

8May/180

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) {
    $errors->remove('empty_email');
}

// 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">
        jQuery('#email').closest('tr').removeClass('form-required').find('.description').remove();
        // Uncheck send new user email option by default
        <?php if (isset($form_type) && $form_type === 'add-new-user') : ?>
            jQuery('#send_user_notification').removeAttr('checked');
        <?php endif; ?>
    </script>
    <?php
}

(Source: https://wordpress.stackexchange.com/a/258137)

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!

 

 

26Mar/170

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:

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

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:

update_option('siteurl','http://example.com');
update_option('home','http://example.com');

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!

 

12Oct/150

Disable automatic redirect to site url

By default WordPress redirects incoming links automatically to the proper URL based on the site url, e.g. it would redirect incorrect incoming "www." URLs automatically to the correct form, as set in the site url setting. For example: when a requested URL is "http://yourdomain.com/whatever/" but the site-url is set as "http://www.yourdomain.com" WordPress will automatically redirect the visitor to "http://www.yourdomain.com/whatever/".

This is a good thing as search engines consider www. and non-www. as different URLs and this would otherwise lead to duplicate content. But should you for whatever reason wish to disable this behaviour you can easily do so by just adding this snippet to your theme's function.php:


remove_action('template_redirect', 'redirect_canonical');

Please note: this will also disable canonical redirects!