WordPress Hacks and Tips #1


Wordpress

For many years, I resisted moving over to WordPress. The truth is, I have a tendency to be a bit of a control freak- liking control over every aspect of the code that powers the websites I build! However, with WordPress becoming more and more powerful and adopted by so many round the globe as well as the existence of fantastic starter themes such as Roots, it came the time to bite the bullet.

I thought it would be useful to document some of the things I have learnt along the way, and this is the first in what I hope will be a number of short posts with some tips and hacks that I have learnt. I hope you find them useful.

1) Shortcodes

Shortcodes are really useful. They enable you to insert extra functionality in posts and pages from a plugin. It could be a Google map or an audio player. I have come across some great plugins, but sometimes I want to hard code a shortcode into the theme. For example, the rather fantastic Event Organiser plugin by Stephen Harris allows you to insert a Google map of the venue for an event. In the documentation they give you a list of shortcodes one of which is one to insert a Google Map. I could get my hands dirty and work out the code to achieve this, but this is WordPress and there is a shortcut- the do_shortcode() command. To use it you just put the short code in and there you go. For example:

<?php echo do_shortcode('[eo_venue_map height="300px" width="600px"]');?>

I came across this from the CSS Tricks website. Thanks!

2) Hiding the WordPress Admin Bar

I really like the WordPress Admin Bar, but there are times when it would be better to disable it. For example, if you have subscribers accessing members only content, you don’t really want them accessing the admin area, and to be honest, you want to hide the fact that you are using WordPress.

There are a number of options. Firstly, you can hide the links to WordPress (the first option in the admin bar). Just add this to your css file:

#wp-admin-bar-wp-logo {display:none;} /* remove WordPress bit of admin bar */

This won’t actually remove this from your code, but it will at least remove it from your display using css.

Joost de Valk, in this post, goes one further and details how you can remove the admin bar completely by adding code to your theme’s functions.php file:

/* Disable the Admin Bar. */
add_filter( 'show_admin_bar', '__return_false' );
function yoast_hide_admin_bar_settings() {
?>
<style type="text/css">
.show-admin-bar {
display: none;
}
</style>
<?php
}
function yoast_disable_admin_bar() {
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'admin_print_scripts-profile.php',
'yoast_hide_admin_bar_settings' );
}
add_action( 'init', 'yoast_disable_admin_bar' , 9 );

This disables it for all users, which might not be quite what you want. In my case, I wanted to disable it for all users except administrators and editors. You can do this by adding the following check:

if(!current_user_can('editor') AND !current_user_can('administrator')){

So, the above becomes:

function yoast_hide_admin_bar_settings() {
?>
<style type="text/css">
.show-admin-bar {
display: none;
}
</style>
<?php
}
function yoast_disable_admin_bar() {
if(!current_user_can('editor') AND !current_user_can('administrator')) {
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'admin_print_scripts-profile.php',
'yoast_hide_admin_bar_settings' );
}
}
add_action( 'init', 'yoast_disable_admin_bar' , 9 );

I also use the auto hide admin bar plugin for editors and admins so that the admin bar is hidden unless you move the mouse up to the top of the page at which point it gracefully appears.

3) Redirect to Home Page after Login and Hide the Admin Area

Once you’ve removed the admin bar for non-admins, there’s no point directing them to the dashboard. So, how do you redirect visitors to the home page after logging in? You need to add this code to your functions.php file:

add_action("admin_init","redirect_fromdashboard");
function redirect_fromdashboard()
{
if(!current_user_can('editor') AND !current_user_can('administrator'))
{
header( 'Location: /' ) ;
}
}

There is also Peter’s Login Redirect plugin which I’ve used which does a similar thing.

4) Redirect to Home Page after Logging Out

I wanted to redirect the visitor to the home page once they logged out (so they don’t get greeted with the WP login form). A really simple plugin that achieves this was developed by Daan Kortenbach and can be found here.

5) Customise Login Screen

There is an awesome article on how to customise the WordPress login screen without affecting core files which I highly recommend reading. I decided on using a simple option and am using the popular plugin Theme My Login. It allows you to match the login and logout screen to match the design of your website. The problem, as the author  pointed out, is that you can still access the WordPress login by entering the normal wp-login.php page in the address bar. However, with a bit of code added to your functions.php file we can redirect people to the correct url:

if ( !is_user_logged_in() && strpos($_SERVER["REQUEST_URI"],"wp-login.php" )) {header( 'Location: /login/' );}

All this does is check that the user is not logged in and then checks if they are trying to access wp-login.php, and if so, it will redirect them to the login page. Make sure you change /login/ in the code above to the page on your site that you are using for logging in with Theme My Login

Turbo Boost your Social Media with the Seriously Social Newsletter!

You will receive an email no more than once a week.


About Ian Anderson Gray

Ian is a digital marketer, social media consultant, web developer & Partner of Select Performers. In real life he is a husband, dad, geek and runs a monthly tech meet up called Cheadle Geeks and a regular coworkers event called Cheadle Jelly. Find out more


Comments- Let me know what you think!