10 Best Code Snippets for Your functions.php File (Functions File)

Over the period of time, Bloggersignal has shared a lot of code snippets that help improve your WordPress site. Most of the code snippets shared here or almost on any other blog should be added to the functions.php file unless otherwise specified.

In case you don’t know, functions.php file is nothing but your theme’s functions file.

To make things a tad bit more awesome, here are some of the best code snippets for your functions file.

Best Code Snippets for Your functions.php File

Also, do note that the all the code snippets shared here can also be added to the Site-Specific plugin. That way, even if you change the theme, the settings or code will stay intact.

Note: before editing WordPress files, make sure that you backup your WordPress site. This helps you to revert back if anything bad happens.

1. Enable Hidden All Settings Menu

code-snippets-for-wordpress-functions-file-all-settings

Do you know that WordPress has a hidden settings menu that displays almost all the advanced settings in a single page?

Just add the below code in your functions file and you will see a new “All Settings” sub-menu in the Settings menu. However, be cautious while messing around on this new settings page. Any miss configurations can break your site.

//Show Hidden All Settings Menu in WordPress
   function all_settings_link() {
    add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
   }
   add_action('admin_menu', 'all_settings_link');

2. Show Update Notifications Only to Admin Users

code-snippets-for-wordpress-functions-file-wordpress-update-notification-hide

By default, the update notifications in WordPress will appear to all users on your WordPress website. If you are running a multi-author blog or if you have multiple users registered on your WordPress website then it is a good idea to restrict the update notifications.

So, add the below code to your functions file and the update notification will only appear to admin users. If a user doesn’t have admin rights then they won’t see the update notifications.

//Show Update Notifications Only to Admin Users
global $user_login;
get_currentuserinfo();

if (!current_user_can('update_plugins'))
	{ // checks to see if current user can update plugins
	add_action('init', create_function('$a', "remove_action( 'init', 'wp_version_check' );") , 2);
	add_filter('pre_option_update_core', create_function('$a', "return null;"));
	}

3. Add Spam and Delete Links to Comments on the Frontend

Generally, if you want to mark comments as spam or to delete certain comments you need to open the Comments section in your WordPress dashboard.

However, by adding the below code in the functions file, you can mark a comment as spam or delete the comment directly from the front end. A pretty handy enhancement for the admins. Of course, you can always disable comments globally, if you really want to.

//Add Spam and Delete Links to Comments (Frontend)
function delete_comment_link($id)
       {
	if (current_user_can('edit_post'))
	   {
	    echo '| <a href="' . admin_url("comment.php?action=cdc&c=$id") . '">del</a> ';
	    echo '| <a href="' . admin_url("comment.php?action=cdc&dt=spam&c=$id") . '">spam</a>';
	   }
       }

After adding the above code, open the comments.php file in your theme’s folder, find edit_comment_link() declaration and then add the below code just after it.

Note: if you are using a child theme then you might want to copy the comments.php file from the parent theme.

delete_comment_link(get_comment_ID());

Also read: how to completely block comment spam in WordPress

4. Enable Shortcodes in Widgets

As you can tell from the sub-heading, by adding the below code in your functions.php file, you can use shortcodes in widgets. Of course, if you ever want to, you can always remove unused shortcodes in WordPress.

// Enable Shortcodes in Widgeds
if ( !is_admin() ){
    add_filter('widget_text', 'do_shortcode', 11);
}

5. Change ‘Howdy’ Welcome Text

code-snippets-for-wordpress-functions-file-wordpress-welcome-message

If you look at the top right corner of your WordPress dashboard, you will see a welcome message something like “Howdy, Vamsi.”

This is good and nothing wrong with it. But if you want to customize the ‘Howdy’ text, simply add the below code and you are good to go. By default, the below code changes “Howdy” to “Welcome.”

However, you can replace the “Welcome” text in the code below with whatever text you want by editing the code. Pretty helpful if you want to the Welcome text to be in your native language.

//Change 'Howdy' Welcome Text
add_filter('gettext', 'change_howdy', 10, 3);

function change_howdy($translated, $text, $domain)
	{
	if (!is_admin() || 'default' != $domain) return $translated;
	if (false !== strpos($translated, 'Howdy')) return str_replace('Howdy', 'Welcome', $translated);
	return $translated;
	}

6. Disable URL Auto-linking in WordPress Comments

code-snippets-for-wordpress-functions-file-url-auto-linking

More often than not, some commenters on your website may link to other sites in their comments. This is nothing bad as long as the links posted are really useful and not spam.

However, the thing is, even if a commenter simply pastes a text link (like http://example.com), WordPress will automatically convert them to hyperlinks. So, if you ever want to, use the below code to disable URL auto-linking in WordPress comments.

//Disable URL Auto-Linking in WordPress Comments
remove_filter( 'comment_text', 'make_clickable', 9 );

7. Stay Logged in for a Long Period of Time

code-snippets-for-wordpress-functions-file-wordpress-login-form

By default, if you select the “Remember me” option while logging in to your WordPress dashboard, it will let you be logged in for 14 days. By using the below code snippet, you can change the default duration from 14 days to one year. That way, you can stay logged in for a longer period of time in WordPress.

That being said, if you ever want to, here is how to log out from active WordPress sessions remotely.

// Stay logged in for longer periods
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in' );
function keep_me_logged_in( $expirein ) {
    return 31556926; // 1 year in seconds
}

8. Remove Private and Protected Prefix for Said Posts

Whenever you create a post that is private or protected, WordPress will add “Private” and “Protected” prefixes to those article titles. If you don’t want these prefixes to be added then add the below code in your functions file. This will remove the private and protected prefixes from the article titles.

In case you are wondering, all we are doing with the below code is that we are replacing Private and Protected text with empty strings.

//Remove Private and Protected Prefixes
function the_title_trim($title)
	{
	$title = attribute_escape($title);
	$findthese = array(
		'#Protected:#',
		'#Private:#'
	);
	$replacewith = array(
		'',//replace "Protected:" with empty string
		'' //replace "Private:" with empty string

	);
	$title = preg_replace($findthese, $replacewith, $title);
	return $title;
	}

add_filter('the_title', 'the_title_trim');

9. Change “Reply” Text in WordPress Comments

code-snippets-for-wordpress-functions-file-change-wp-comment-reply-text-modified

Using the below code, you can change the default “Reply” text in the comments section. By default, the “Reply” text will be replaced with “Respond.” If you want to, you can customize the “Respond” text in the below code.

//Change Reply Text in WordPress Comments
function bs_modified_comment_reply_text( $link ) {
 $link = str_replace( 'Reply', 'Respond', $link );
 return $link;
 }
add_filter( 'comment_reply_link', 'bs_modified_comment_reply_text' );

10. Add Your Own Custom Gravatar Image

code-snippets-for-wordpress-functions-file-add-custom-gravatar-custom-gravatar-in-action

The default Gravatars in WordPress are good but not great. Using the below code, you can set your own gravatar, like your site logo or something.

If you want to customize it even more then set users first letters as their Gravatars.

Note: the below code assumes that your custom gravatar image in the “images” folder in your current theme directory. If not then change the path accordingly.

//Add Custom Gravatar Image
function custom_gravatar($avatar_defaults) {
    $logo = get_bloginfo('template_directory') . '/images/custom_gravatar.jpg'; //change path if needed.
    $avatar_defaults[$logo] = get_bloginfo('name');
    return $avatar_defaults;
} 
add_filter( 'avatar_defaults', 'custom_gravatar' );

Bonus: Add Confirmation Dialog Box When Click ‘Publish’ Button

code-snippets-for-wordpress-functions-file-confirm-message-before-publishing-add-code

How many times have clicked on that “Publish” button accidentally? I know, I been there and done that. It is pretty frustrating and embarrassing at times.

With the below code added to your functions file, whenever you click on the “Publish” button, you will receive a confirmation box where you have to confirm the action.

Pretty simple but really useful.

//Add Confirmation Box Before Publishing
function add_publish_confirmation(){ 
    $confirmation_message = "Are you sure you want to publish this post?"; //Your custom message
 
    echo '<script type="text/javascript">';
    echo '<!-- var publish = document.getElementById("publish");'; 
    echo 'if (publish !== null){';
    echo 'publish.onclick = function(){ return confirm("'.$confirmation_message.'"); };'; 
    echo '}'; 
    echo '// --></script>'; 
} 
add_action('admin_footer', 'add_publish_confirmation');

Conclusion

That’s all for now and hopefully that helps. Do comment below to sharing your thoughts and experiences about using the above code snippets on your site or to share your own code snippets that you added to your functions file.

3 Comments

  1. Wow! These snippets are pretty handy! I could definitely use the ability to delete span comments on the front end. It would be quicker and easier to do it that way. Also, adding shortcodes in widgets adds another layer of functionality that will take your site one step further. Thanks for these cool snippets!

  2. Thanks for a brilliant post.

    I’m always hitting the publish button when I’m only really drafting post outlines, the Confirmation Dialog Box is going to prove really helpful.

    I will also be disabling the URL auto-linking in comments.

    Bloggersignal just got added to “Must Read” bookmark folder.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.