6 Useful Code Snippets for WordPress

In this article, we would like to present you with six useful code snippets that will enhance your WordPress website.

Please back up your site before you start making changes – especially make sure you have a copy of your original functions.php file.

Members Only Content Shortcode

This shortcode allows you to write a private content in a post. For non-members, the login form will appear. Add the following function to your functions.php file.

function member( $atts, $content = null ) {
       if  ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
              return $content;
              return '';
       } else {
       $yonlendir = get_permalink();
              $form = wp_login_form(array('echo' => false, 'redirect' => $yonlendir ));
              return $form;
add_shortcode( 'mem', 'member' );


[mem] ..your private content.. [/mem]

Only show admin bar to administrators

Paste this code in your theme’s functions.php file to hide the admin bar to all logged in users except admins. Useful for sites that have public registration turned on.


// Snippet code goes here!
if (!current_user_can('administrator')) :


Hide login errors

For security reasons, you might want to hide the login messages that WordPress shows when you’re trying to login with the wrong information. The message that says “Invalid username” helps the hacker in his search for the right username and password.

Put this code in functions.php.

<?php add_filter('login_errors', create_function('$a', "return null;")); ?>

Temporary Maintenance

This function can be added to your functions.php template to temporarily close down your site to visitors. Your website can still be accessed by administrators, allowing you to make the changes you need without worrying about messing up your live website.

To display your website to visitors again, simply delete the function or comment it out.

// Temp Maintenance - with http response 503 (Service Temporarily Unavailable)
// This will only block users who are NOT an administrator from viewing the website.
function wp_maintenance_mode(){
    if(!current_user_can('edit_themes') || !is_user_logged_in()){
        wp_die('Maintenance, please come back soon.', 'Maintenance - please come back soon.', array('response' => '503'));
add_action('get_header', 'wp_maintenance_mode');

How to Promote your RSS feed on every post

Adding this snippet to the functions.php of your wordpress theme will add a link to your rss feed at the bottom of every post. If you are looking to get some new subscribers this is a great way to promote your feed.

function wps_promote_feed($content,$class = "promote") {
    echo $content;
    if (is_single()) {
        <div class="<?php echo $class; ?>">
            <h3>Enjoyed this article?</h3>
            <p>Please consider subscribing to our <a class="feed" href="<?php bloginfo('rss2_url'); ?>" title="Subscribe via RSS">RSS feed!</a></p>

Add welcome message to wp-login

This function inserts custom welcome message below the logo on your wp-admin\ wp-login.php page.

function wpc_login_message( $message ) { 
if ( emptyempty($message) ){ 
 return "<p class="message">Welcome to this site. Please log in to continue</p>"; 
 } else { 
 return $message; 
add_filter( 'login_message', 'wpc_login_message' );
Analytics Made Easy - StatCounter
scroll to top