17 WordPress Functions.php File Hacks

Last Updated on September 12, 2023 by 54 Comments

17 WordPress Functions.php File Hacks
Blog / Tips & Tricks / 17 WordPress Functions.php File Hacks

Out of all your WordPress core files, few are as versatile as functions.php. If you know how to interact with it, this file will enable you to add all sorts of functionalityย to your site with just a few lines of code.

In this article, weโ€™ll tell you everything you need to know about the WordPress functions file and how to use it to your advantage. Weโ€™ll discussย what it does, why you should use it, and when not to. Plus, weโ€™ll introduce you to 17 useful file hacks to extend your siteโ€™s functionality without having to install a plugin.

Letโ€™s get this show on the road!

An Introduction to the WordPress Functions File

The functions.php fileย is one of WordPressโ€™ core files. In other words, itโ€™s one that helps toย power the entire platform, and itโ€™s behind many of the features on your site.

A child theme's functions.php file.

You can also use the functions.php file as a makeshift way to implement new features to your website, such as adding new widgets to your dashboard or custom image sizes for your installation. Itโ€™s the perfect alternative to WordPress plugins for simple or temporary features.

Itโ€™s also worth noting thereโ€™s a โ€˜parentโ€™ functions.php file that influences your entire site, but each theme comes with its own child version (much like your style.css file). This effectively enables you to implement functions only for specific themes without affecting your site globally.

17 File Hacks for Your WordPress Functions.php File

As we mentioned earlier, each theme comes withย its own functions.php file. If youโ€™re going to make extensive modifications to yours, the best course of action is to set up a child theme so your changes donโ€™t disappear during updates. Youโ€™ll also want to backup your site before making any changes, just in case things go wrong and you need toย roll back your changes.

Moving on, youโ€™ll need to access your functions.php file using an FTP (File Transfer Protocol) clientย โ€“ such as FileZillaย โ€“ toย navigate to the wp-content/themes/ directory. Inside, youโ€™ll find a folder for each of your themes, all of which should have their own functions.php files within:

The Divi functions.php file.

Just right-click on it and pick the option that says Edit, which will open your default text editor. With that out of the way, letโ€™s get on to business!

1. Add a New Admin User to WordPress

There are some occasions where you could get locked out of your admin account, and be unable to either change your password or access your site. With this tweak, youโ€™ll have an additional way of regaining access via FTP.

As with all of these hacks and snippets, simplyย copy the following code, paste it atย the end of your themeโ€™sย functions.php file, and save the changes.

function wpb_admin_account(){
$user = 'username';
$pass = 'password';
$email = '[email protected]';
if ( !username_exists( $user ) ย && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','wpb_admin_account');

Youโ€™ll need to replace the values where it says username, password, and email with your own, and youโ€™re all set!

2. Display the Total Number of Registered Users

If youโ€™ve ever wanted to brag to the world about how many users your site has, this tweak enables you to add a simple count to your front end. This hack alsoย has uses beyond just bragging rights. For example, people might be more willing to trust your site if they know it has a strong user base. Hereโ€™s the snippet:

function wpb_user_count() {
$usercount = count_users();
$result = $usercount['total_users'];
return $result;
}
add_shortcode('user_count', 'wpb_user_count');

This is a bit advanced, as weโ€™ve actually registered the [user_count]ย custom shortcode to display the total registered users on your site. While this can be aย complex topic, our implementation is simple!

3. Remove Your Dashboardโ€™s โ€˜Welcome Panelโ€™

Your welcome panel is the section of your dashboard that appears as soon as you log into WordPress, and it usually contains a few tips and checklists to get you started. With this code snippet, youโ€™ll be able to removeย it permanently:

remove_action('welcome_panel', 'wp_welcome_panel');

Sure, the welcome panel isnโ€™t invasive at all, but it can get a bit old if youโ€™ve been using the platform for a while and donโ€™t need the tips anymore. Plus, since all it takes is a line of code, thereโ€™s literally no downside to this hack.

4. Hide Your WordPress Version Number

As you probably know, depending on theย theme youโ€™re using, WordPress tends to display its version number in your siteโ€™s footer. Itโ€™s a tiny detail that you can remove easily with this code:

function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');

If youโ€™re running an old version of WordPress (which of course, you shouldnโ€™t be), this tweak can come in handy to make you a less tempting target.

5. Modify Your Dashboard Footer

Your WordPress dashboard probably includes a bit of text atย the footer, often stating a credit of some kind (usually WordPress itself). Itโ€™s something you may have become blind to, but this hackย does offer you a good opportunity to customize or brand your site with a simple hack. Letโ€™s check out the corresponding code:

function remove_footer_admin () {
echo 'Hello world!';
}
add_filter('admin_footer_text', 'remove_footer_admin');

For this hack, you should replace theย Hello world! textย with your own, including any links youโ€™d like to include.

6. Add a Simple Text Widget to Your Dashboard

If you decided to clear up some room on your dashboard by removing the welcome widget, you might want to replace it with one of your own. Using this code, youโ€™ll be able to set up a simple text widget:

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_widget', 'More Information', 'custom_dashboard_information');
}
function custom_dashboard_information() {
echo 'If you need help making changes to your site, you can always contact me via email at...';
}

This snippet will add a widget called More Information to your dashboard, including the text within the custom_dashboard_information function. For this example, we chose to create a contact widget of sorts, which you can use to white label client websites and remind them to stay in touch if needed.

7. Add Custom Image Sizes to Your Installations

WordPress enables you to resize your images to specific dimensions after uploading them, and provides some presets by default. With this code, youโ€™ll be able to add new presets to avoid having to set custom dimensions:

add_image_size( 'sidebar-thumbnail', 120, 120 );

Keep in mind you can add as many new sizes as you want, and they can come in particularly handy if thereโ€™s a specific resolution you use throughout your site.

8. Add Featured Images to Your RSS Feed

A lot of people donโ€™t know this, but WordPress generates RSS feeds for your sites automatically. If you intend on using yours, youโ€™ll want to set up a featured image for your posts, which you can do with this code:

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '

' . get_the_post_thumbnail($post->ID) .
'

' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

Featured images can go a long way towards making your RSS feed look more attractive, which is crucial for any type of content that relies on click-throughs.

9. Exclude Categories From Your RSS Feed

One of the best things about WordPress RSS feedโ€˜s feature is how customizable it is. With this code, youโ€™ll be able to hide specific categories from your feed, so you can pick and choose which content is added:

function exclude_category($query) {
if ( $query->is_feed ) {
$query->set('cat', -2, -3');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');

Keep in mind WordPress identifies categories according to ID numbers (i.e. -2 and -3) in the code above. So youโ€™ll need to identify the IDs for the categories you want to remove before implementing this hack.

10. Disable Your RSS Feed

Although its a great WordPress feature,ย if you arenโ€™t interested in using RSS at all, you can disable itย altogether using this code snippet:

function fb_disable_feed() {
wp_die( __('Sorry, we don't use RSS!') );
}
add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);

Youโ€™ll notice the code includes a little message for users whoย attempt to access your feed after youโ€™ve disabled it. Thereโ€™s no downside to keeping it active, but if youโ€™re sure youโ€™re not going to use it, this enables you to turn it off altogether and potentially save a cycle or two.

11. Hide Your WordPress Login Error Details

Login errors that are too detailed can give undesirablesย the information they need to help gain access to your site. With this code, WordPress wonโ€™t specify where you made an error when attempting to log in:

function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

By using this hack, it makes it harder for attackers to attempt to gain access to your site, while real users should have no problem figuring out where theyโ€™ve made a mistake. A win-win all around!

12. Force Users to Login With Their Usernames

A lot of people tend to sign up for every service using the same email, which means that attackers can attempt to gain access just by knowing their email addresses. With this code, youโ€™ll force users to log in using usernames exclusively:

remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );

To be fair, usernames also tend to get reused a lot, but this small tweak can still help improve your security and minimize attacks.

13. Change the Length of Your Post Excerpts

WordPress blog indexes usually display brief excerpts of your posts to give readers an idea of what theyโ€™re in for. If youโ€™ve ever wanted to change the default length of these excerpts, hereโ€™s an opportunity to do so.

functionnew_excerpt_length($length) {
return 75;
}
add_filter('excerpt_length', 'new_excerpt_length');

The default length is 55 but in the code above, we raised it to 75 to give us a little more wiggle room.

14. Enable WordPress to Upload Any File Type You Want

By default, WordPress only enables you to upload a very limited range of file types, including common media formats such asย .png and .jpg. With this code, youโ€™ll be able to force your installation to allow more types:

function my_myme_types($mime_types){
$mime_types['svg'] = 'image/svg+xml';
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

Take .svg, for example, which is a file type weโ€™ve discussed in the past. With this snippet, youโ€™ll be able to add support for it without the need for a plugin.

15. Disable Automatic Image Linking

Out of the box, WordPress will add links to each image you include in your content that pointsย to its full version. By using this code, youโ€™ll disable this functionality:

function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);

Keep in mind, though, you can still access this feature by editing each individual image โ€“ all this does is prevent it from working automatically.

16. Add a Widget Ready Area

WordPress already includes multiple widget-ready areas for you to use, but you can always add new ones using a little functions.php magic. As with our earlier shortcodes hack, youโ€™re beginning to get into the realm of developer here. However, this is a simple implementation that could help you immensely:

function custom_sidebars() {
	$args = array(
		'id' => 'custom_sidebar',
		'name' => __( 'Custom Widget', 'text_domain' ),
		'description' => __( 'A custom widget, 'text_domain' ),
		'before_title' => '

<h3 class="widget-title">',
		'after_title' => '</h3>

',
		'before_widget' => '

<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>

',
	);
	register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );

Keep in mind thisย code merely โ€˜registersโ€™ your new widget area. Youโ€™ll still need to call it for specific theme sections, such as your footer, by editing their respective files.

17. Disable the WordPress Search Feature

Almost every site needs a search feature to help users find their way around. However, if youโ€™re going to build a single-page site, you mayย notย need it. Hereโ€™s the code that will enable you to turn if off:

function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

Before disabling this feature, you need to be 100% sure you wonโ€™t need it since it can heavily impact your siteโ€™s usability. Of course, you can always remove the code to reinstate search on your site, but if youโ€™re not sure, err on the side of caution and donโ€™t turn it off!

Conclusion

Your functions.php file is one of the most powerful assets in your WordPress toolbox. Not only does it enable you to add all sort of features with a few lines of code, but itโ€™s also critical for your site to function (see what we did there?).

If youโ€™re a Divi user, the functions.php file can help you add several complementary features to your site without having to set up additional plugins, which is always a win.

Do you have any questions about these WordPress functions file hacks? If so, ask away in the comments section below!

Article thumbnail image by MchlSkhrv / shutterstock.com

Divi

Want To Build Better WordPress Websites? Start Here! ๐Ÿ‘‡

Take the first step towards a better website.

Get Started
Divi
Premade Layouts

Check Out These Related Posts

Splice Video Editor: An Overview and Review

Splice Video Editor: An Overview and Review

Updated on March 10, 2023 in Tips & Tricks

Video is a valuable form of content for social media. Unfortunately, creating quality videos is usually a long process that involves moving mobile footage to a desktop app for editing. However, mobile editing is on the rise. Apps such as Splice Video Editor make it possible to efficiently create...

View Full Post
How to Use Font Awesome On Your WordPress Website

How to Use Font Awesome On Your WordPress Website

Updated on September 16, 2022 in Tips & Tricks

When given the choice between using a vector icon or a static image, it’s a good idea to go with the vector. They’re small and fast to load, and they can scale to any size without a loss of resolution. Font Awesome is a superb library of vector icons that you can use on your websites,...

View Full Post

54 Comments

  1. Hi John, care to enlighten us as to what these are:

    “If youโ€™re a Divi user, the functions.php file can help you add several complementary features to your site without having to set up additional plugins, which is always a win.”

  2. Thank you for the hack for divi theme. Can you provide the code for the Related post in the post content. We do not need a plugin to that or add it with the divi – Like extra themes. I already asked in forum.

  3. Nice bit of information but in your 5 number hack you use echo statement which is not recommended it should be used as return “Hello, World!”; rather than echo “Hello, World!”

    • Hi Imran, thanks for your input! ๐Ÿ™‚

  4. How about writing a decent educational material/tutorial/ebook on this topic instead of providing “Hacks”?

    Codesnippets taken out of context, without comments or explanation are like asking for disaster. Plus it provides very little educational value. And if somebody is stuck, it is basically impossible to figure out what went wrong.

  5. Concerning login error logs, I prefer to do something like this

    add_filter(‘login_errors’, create_function(‘$a’, “return ‘Bad thing has just happened’;”));

    That shows there is an error on login, but not sure what it is.

    • Hi Pat, thanks for the tip. ๐Ÿ™‚

  6. For #1, shouldn’t it be: $user->set_role( ‘administrator’ );

    (-> instead of ->)

    • Thanks for letting us know, Oliver. ๐Ÿ™‚

    • OK, the special character apparently “looks” like the correct higher bracket, so it’s not very obvious what I tried to say here…

      In the post above, it was ANDgt; (instead of “and”, there’s a “&”).

  7. Hi John,
    thanks a lot for this article. I have a question: I need to create a fixed top bar in a website for public users. Do you suggest to develope it with css or to create a widget with functions.php file? Is there another way you suggest to do it?

    Thank you

    • Hi Maria! Either option could work; it depends on what you’re most comfortable with.

  8. This is the most useful information about functions file. It increased our knowledge about WordPress files.

    • Thanks for your comment! ๐Ÿ™‚

  9. I believe 13 is missing a space after ‘function’.

    functionnew_excerpt_length($length) {

    should be

    function new_excerpt_length($length) {

    • Hi David, thanks for pointing that out. ๐Ÿ™‚

      • Rather than just saying thanks – would be better if you updated the post to fix the typo – as non-coders will be copy/pasting this – and will find it hard to debug. You have a responsibility to correct such errors in a timely fashion…

        RB

  10. You are the best
    Thank you so much

    • You’re welcome Eddy! Thanks for your comment. ๐Ÿ™‚

  11. wow! thank you elegant themes team! this is what we’re talking about, wise (very useful) & updated (very timely) blog posts :)!

    • You’re welcome! We’re glad you found this post so helpful. ๐Ÿ™‚

  12. Thanks a lot John, very important when we do not want to depend on plug-ins.

    • You’re welcome, Alex! ๐Ÿ™‚

  13. Great inputs about customizing WordPress site using functions.php, is it possible to customize header section of my travel website using this.

    • Thanks Roger! It depends on what you specifically want to do. Try posting your goals in the forum (https://www.elegantthemes.com/forum/) to see if other users have some useful ideas about how to achieve them.

  14. Thanks John. I use few of them once in a while. It is great to have a handy cheat-sheet with all of hacks at one place.

    • No problem. ๐Ÿ™‚ We’re glad you found this post so useful!

  15. Thanks for these hacks – really useful.

    I’ve used this plugin – My Custom Functions – sometimes to add custom actions to my website:

    https://wordpress.org/plugins/my-custom-functions/

    It means that they are not theme specific and I don’t loose the functionality when I change themes.

    Thanks for your great blog – I read it every day!

    • You’re welcome Neal; we’re glad to hear it! Thanks for the plugin suggestion. ๐Ÿ™‚

  16. Hey John,

    Wondering if I introduce three or four of your choices shared above, if they should be separated via any specific code or may they simply reside, one above the other with simple hard return between them?

    Number 13 – 16 are all of interest to me.

    • They’re all self-contained hacks, so you should be able to easily add more than one to your functions file.

  17. Hi,

    I’m a “life-long” member here.
    Post arrive in my email daily.
    I’ve a new WordPress.org site I’ve been working on of weeks. You’ve been very helpful as I read most of what you send me.

    Here’s the site in question:
    http://www.rosslandingboggybayou.com

    Please note that while I was reading the lastest mailing from you about php “hacks”, I decided to do a test. It’s not been good.

    Using ftp, I downloaded my “functions.php” file from the Divi-Child theme.
    It is sparse. I used a test code:

    in an attempt see what works best for including a “comment” along with the new codes you’ve written about. I thought that the realm of testing was a good idea but as you’ll see if you visit the site mentioned above, the text is written into the top, left corner of the page and my footer design has dropped down to an enormous size below the final footer text! Yikes!

    Here is what I wanted to place in the bottom of the “functions.php” file:

    functionnew_excerpt_length($length) {
    return 75;
    }
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

    …but I wanted to find out if I should use opening and closing elements – hence, the “TESTING”!

    Have you any ideas? I’ve backed up all files prior to this test so I have the original site on my home computer. Could I simply upload the “functions.php” file from the Divi-Child back up?

    • Hi Fredrick! It’s hard to say without knowing your exact setup. We’d suggest posting in the forums (https://www.elegantthemes.com/forum/) to see if another user can help.

  18. Great, thank you for handy tips! In this occasion I must ask urgent question. Is there any “hack” to change the way Extra theme displays the WooCommecre single product’s tabs? I mean it does it vertically while the Divi horizontally and this is what I need to achieve!

    • Hi Ada; thanks for your comment! It’s hard to say without knowing your exact setup. Try posting in the forums (https://www.elegantthemes.com/forum/) to get help from other users.

  19. Awesome list John! Easily digestible little codebites, I love it! I will be using a few of these on my next project!

    • Thanks Dom! Good luck with your project. ๐Ÿ™‚

  20. Cheat #11 doesn’t work for me…

    I paste it at the end of my child themeโ€™s functions.php file
    Why it doesn’t work ?

    Thanks

      • Is there a limit on the length of the login error message?

    • Try the parent theme function.php. You’ll have to add every time the the parent theme updates.

  21. Thanks for this info.

    A number of these issues I already address using existing plugins or security plugins, however I have always been trying to further my WP skills set by understanding what is happening on a code base level.

    • You’re welcome, Will! Coding takes a little time and patience to learn, but it helps you understand how your site works much more clearly.

  22. Great article! Learned few hacks that I didn’t know myself.

    It’s also possible to customize WordPress login page in functions.php instead of installing a plugin that does that. Can be useful when developing a site for a client. Here’s an example if anyone’s interested https://gist.github.com/anonymous/9e40eee19d5bcfa164667644b0799da1

    • Hi Alex, thanks for the tip!

  23. Hi, thanks a lot, some of this features I use already, the other are now on my WP-ssnippets-Whatch-List.
    One little tipp only: I use to put such snippets into a little plugin. It’s even more easy to add a plugin to wordPress-instaltion than a child-theme.
    I mostly use the Init-Hook for such cool things. You also can integrate more of this features in one plugin.

    • You’re welcome! Thanks for the suggestions. ๐Ÿ™‚

  24. Thank you John, these are awesome. How would I add searchable, linked tags to the bottom of all my posts?

  25. Wow, Great post John!

    Most of the things i knew already, but some of them not.
    There are a lot off hacks possible in the unctions.php

    • Hi Jeroen, thanks! ๐Ÿ™‚

  26. I love this blog! It’s my first go-to place when I have a question, especially about DIVI. Thanks for today’s useful tips. I’ll be using them.

    • You’re welcome, Hope! We’re flattered. ๐Ÿ™‚

Leave A Reply

Comments are reviewed and must adhere to our comments policy.

Get Started With Divi