8 New and amazing WordPress hacks

by Jean. 26 Comments -

It’s been a while that I haven’t posted a compilation of the best WordPress hacks here on Cats Who Code… But WordPress hacks is one of the most popular topics on CatsWhoCode. This is why I’ve decided to compile 10 new and extremely useful hack for WordPress. Enjoy!

Easily replace WordPress editor font

Don’t like the default font used by WordPress editor? No problem, the following code will allow you to change it. Simply paste it to your theme functions.php file. You can define which font to use on line 5.

add_action( 'admin_head-post.php', 'cwc_fix_html_editor_font' );
add_action( 'admin_head-post-new.php', 'cwc_fix_html_editor_font' );

function cwc_fix_html_editor_font() { ?>

<style type="text/css">#editorcontainer #content, #wp_mce_fullscreen { font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif; }</style>
<?php }

Source: http://devpress.com/blog/fixing-wordpress-3-2s-html-editor-font/

Quick and easy maintenance mode

Sometimes, you need to put your blog on hold while performing some maintenance. Many plugins are allowing you to do so, but here is a simpler solution: Just paste the following snippet into your functions.php file and save it. Your blog is now unavailable to anyone except administrators. Don’t forget to remove the code when you’re done with maintenance!

function cwc_maintenance_mode() {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
        wp_die('Maintenance, please come back soon.');
    }
}
add_action('get_header', 'cwc_maintenance_mode');

Source: http://skyje.com/2011/05/wordpress-code-snippets/

Simpler login url

Would you like to be able to use a simpler url like http://website.com/login to login to your WordPress dashboard? If yes, just read this recipe to learn how to implement it on your own blog.

Open your .htaccess file (located at the root of your WordPress install) and add the following code. Remember to backup your .htaccess file before editing it!

RewriteRule ^login$ http://yoursite.com/wp-login.php [NC,L]

Source: http://www.wprecipes.com/simpler-wordpress-login-url

Disable theme switching

When working with clients, it can be good to keep the control of what they can do to prevent possible problems. For example, disabling theme switching can be a good idea, especially if the site you built heavily rely on the theme. To do so, just paste the code below into the functions.php file of the theme. Once done, the client will not be able to switch themes anymore.

add_action('admin_init', 'cwc_lock_theme');
function cwc_lock_theme() {
	global $submenu, $userdata;
	get_currentuserinfo();
	if ($userdata->ID != 1) {
		unset($submenu['themes.php'][5]);
		unset($submenu['themes.php'][15]);
	}
}

Source: http://sltaylor.co.uk/blog/disabling-wordpress-plugin-deactivation-theme-changing/

Disable RSS feed

By default, WordPress include the popular RSS functionnality, which is great for blogs. But if you’re using your WordPress install as a static site, having RSS feeds may become a bit confusing for your visitors.

This code will totally disable RSS feeds (As well as other formats) from your blog. Just paste the code into functions.php, and you’re done.

function cwc_disable_feed() {
	wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}
add_action('do_feed', 'cwc_disable_feed', 1);
add_action('do_feed_rdf', 'cwc_disable_feed', 1);
add_action('do_feed_rss', 'cwc_disable_feed', 1);
add_action('do_feed_rss2', 'cwc_disable_feed', 1);
add_action('do_feed_atom', 'cwc_disable_feed', 1);

Source: http://wpengineer.com/287/disable-wordpress-feed/

Filter custom post types by author in admin

Here is a function which adds a dropdown select control of users next to existing filters (by default, date). It also works in tandem with the built in author filtering which is available when you click on an author in on admin listing pages (by default on posts and pages).

As usual, the only thing you have to do to implement this code is to paste it into your functions.php file.

function cwc_restrict_manage_authors() {
        if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('your_custom_post_types', 'here'))) {
                wp_dropdown_users(array(
                        'show_option_all'       => 'Show all Authors',
                        'show_option_none'      => false,
                        'name'                  => 'author',
                        'selected'              => !empty($_GET['author']) ? $_GET['author'] : 0,
                        'include_selected'      => false
                ));
        }
}
add_action('restrict_manage_posts', 'cwc_restrict_manage_authors');

Source: http://forrst.com/posts/WordPress_Custom_Post_Types_Filter_by_Author_in-s9p

Add post thumbnails to RSS feed

This very cool piece of code will get the post thumbnail and automatically add it to your RSS feeds. Paste the code into functions.php and save the file. Don’t forget that you need to use a theme that supports post thumbnails for this snippet to work.

function cwc_rss_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) .
        '</p>' . get_the_content();
    }

    return $content;
}
add_filter('the_excerpt_rss', 'cwc_rss_post_thumbnail');
add_filter('the_content_feed', 'cwc_rss_post_thumbnail');

Source: http://snipplr.com/view.php?codeview&id=56180

Remove WordPress admin bar

Introduced in WordPress 3.X, the new “Admin bar” is an useful feature, but if you don’t like it, you can easily remove it. Just paste the following snippet into your functions.php file.

add_filter('show_admin_bar', '__return_false');

Source: http://speckyboy.com/2011/03/01/how-to-remove-the-admin-bar-from-wordpress-3-1/

  • http://www.twitter.com/sherwinraavi Sherwin Raavi

    Loving the simpler login and disable theme switching ideas, thanks =)

  • http://www.safwanrahman.com Safwan Rahman

    This is very useful it helps me a lot!

  • http://andrewnacin.com/ Andrew Nacin

    For disabling the ability to switch themes, you’d be better off actually denying the capability required to do so:

    function nacin_deny_switch_themes( $caps, $cap ) {
    if ( 'switch_themes' )
    $caps[] = 'do_not_allow';
    return $caps;
    }
    add_filter( 'map_meta_cap', 'nacin_deny_switch_themes', 10, 2 );

    That said, if you simply wanted to remove the menu (but keep the page functional, as you’re doing here), use remove_submenu_page(), rather than directly hacking the $menu global.

  • http://amyhendrix.net Amy Hendrix

    Some handy tips here, but I hate to see nearly all of them advise hacking functions.php — most of this stuff would be more appropriate for a plugin. Keep the functional code portable and don’t fill up the theme with stuff that themes aren’t meant to do!

  • http://www.wendycockcroftwebdesign.com Wendy Cockcroft

    Thanks for the tips! Added to my list of WP tutorials.

  • http://hostkabob.com Frank Hagan

    You can also turn off the Admin bar by visiting Admin > Users > Your Profile and un-checking the appropriate checkbox. This is preferable on larger installations where one author, editor or admin might prefer the function. It also doesn’t get overwritten by the next update to your theme.

  • http://www.digitalactivist.net/ Osvaldo

    Thank you very much, WordPress is really an amazing content management system. Can anyone advice on a website/post with more snippets like this?

  • Peter

    Perhaps a better way to disable the admin bar is to simply uncheck the “show admin bar” options in the personal options for your wordpress account (accessed from the dashboard under Users > Your Profile).

  • http://www.francois-paren.com/ francois

    Thanks for the RSS thumbnails ! It’s very useful :)

    • http://www.keithrozario.com Keith

      I was unable to get this to work, I’m using composito from design disease. How can I figure out if my theme supports thumbnails, and which functions.php do I paste it into?

      When I appended the RSS snippet into the functions.php of the theme I use, I get an error everytime I try to upload a picture.

  • http://www.yourwebagency.co.uk/ Edgars

    Thanks for simpler login but about Editor font -I think it’s better to add function add_editor_style(); then create editor-style.css (some themes have it already) and all style can be edited simply through that css file

  • http://a.trendyname.org Chris

    Anyone know how to, instead of disable RSS feeds, hide it to everyone except you so the only way to access it is if you know the URL, I don’t want readers or browsers to find my RSS feed but I’d like to use it for twitter and what nto.

  • http://www.blogging24h.com Trung Nguyen

    Wow, very useful tips for WP blogger, thanks for your collection, Maybe I will try some tips above for my WP blog.

  • http://www.ChiefAlchemist.com Mark “Chief Alchemist” Simchock

    Kudos once again.

  • http://downloadtvcommercials.org Kristina Tindal

    That admin bar is sooo irritating. Thanks to you, I removed it on all my blogs :)

  • http://www.100webhosting.com Jasmine

    Awesome WordPress hacks, I like them a lot! I also think that the admin bar is annoying and thus I have it removed already! Hehe…

  • http://www.synergiaonline.com leo rapirap

    Thanks a lot for sharing! been looking/wondering how to disable theme switching for a week now. Very useful post!

  • http://thirstyaffiliates.com Josh Kohlbach

    Pretty cool, hadn’t seen the majority of these which is nice for a change.

    Adding images to RSS excerpt is very cool.

  • http://www.fourthcup.org Jason Sprenger

    Great post, does anyone know how to enable WordPress to exclude specific RSS feeds like comments? I was thinking it would be useful for those (like myself) who prefer our comments to be private …

  • http://instructmydesign.blogspot.com/ Dinesh Verma

    The maintenance mode trick is cool. By the way what happened to your design.

  • http://www.sparringmind.com Gregory C.

    Awesome resource, will definitely be using a lot of this in new projects!

  • http://insomniatreatment.biz/about/ Bob Carter

    Very useful stuff. Have disabled the admin bar. I use two tabs, one is open for admin and the other to see the website. The admin bar just gets in the way so thanks for the hack.

  • http://portusbhub.com Rahul Tilloo

    Hi Jean

    Thanks for a great share. I am currently try get knowledge on which wordpress services are provided to clients. The theme switching would ceratinly come in handy for me. Thanks for the share.

  • Sam

    i am addicted to this blog.. its amazing.
    but i need a wp hack but i couldn’t find..
    at the ‘tag’ page which shows posts under certain tag:
    - i want to show an unlimited number of posts only on those pages.
    - i want to order the posts in ASC instead of DESC.

  • rudolf

    I’m not sure why, but for me the maintenance mode only works if I use add_action(‘init’, ‘cwc_maintenance_mode’);

  • http://forsberg-marketing.com Jonas

    Really nice post man! Especially the theme switch.. Should disable it to myself – not smart to change theme every now and then. >.>