11 Cool Things You Can Do With The WordPress Functions.php File

Cool Things You Can Do With The WordPress Functions.php File

The functions.php file is one of the most important files that a WordPress website has for webmasters. It allows them to directly change some of the website’s features and functionalities instead of requiring them to use third-party plugins or custom-tailored themes. Even with its great potential, this file often goes under-appreciated by newer WordPress users.

This is why we dedicated this article to explaining what the WordPress functions.php file is and to sharing some of the cool things you can do with it. To give you an idea of what those are, or to skip ahead to any one of them, you can check out the list below:

A Brief Explanation of the WordPress functions.PHP File

The functions.php file is one of the core files of every WordPress theme, both parent and child. It allows theme developers and webmasters alike to define additional features or functionalities that a theme could have. Meaning, this file can serve as a repository of custom code snippets that improve your website. Any custom code snippets you choose to add should be inserted at the end of the file, which you can edit with an FTP client and the text editor of your choice. However, there are two things you should keep in mind.

The first thing is that the functions.php file is directly tied to the WordPress theme within which it’s located. As such, any code snippets that you insert into the file will no longer work if you choose to deactivate its theme. Therefore, if you deactivate your current theme and choose to use another, you will need to copy/paste any custom code snippets from your previous functions.php file to the new one.

The second thing is that, as one of the core files of WordPress themes, the functions.php file will get overwritten on every theme update. Therefore, if you insert a code snippet into the functions.php file of your parent theme, you risk losing it during the next theme update. To avoid that, webmasters usually opt to include their code snippets inside the functions.php file of a child theme.

While a child theme will keep your modifications to the functions.php file safe from updates, it won’t be much help if you choose to switch themes. The best way to cover both eventualities is to insert your custom code snippets inside a site-specific plugin instead of the functions.php file. That way, the snippets aren’t tied to a theme, but to your plugin, and they will work as long as the plugin is active. With that being said, the choice is yours whether you’ll use a site-specific plugin or add the snippets inside the functions.php of your child theme.

Some of the Things You Can Do with the WordPress functions.PHP File

Now that you have a clearer picture of what the functions.php file serves, we’ll be taking a look at how you can modify it with code snippets and create some additional features and functionalities for your site. We prepared 11 different code snippets that you can browse through and choose from.

Before implementing any of the codes shown below, we strongly suggest you make a backup of your website, as a safety precaution. Furthermore, as we mentioned earlier, any snippets you decide to add to your functions.php file should be inserted via FTP. If you aren’t familiar with it, we recommend going through our article on the use of FTP, as well. With that being said, let us proceed.

Adding a Dynamic Copyright Notice in the Footer

Including a copyright notice in your site’s footer will help you fight against copyright infringement. But, even though these notices can be easily made, they are rarely dynamic. This means you would need to regularly update them by hand. However, you can save yourself the trouble by creating dynamic copyright notices that are updated automatically. Below, you can see one example of a code snippet that will help you do this.

function custom_copyright_text( $atts, $content = null ) {
$default_atts = array(
"year_from" => ''
$params = shortcode_atts( $default_atts, $atts );
$html = '<div class="copyright">Copyright &copy; ';
if ( ! empty( $params['year_from'] ) ) {
$html .= $params['year_from'] . ' - ';
$html .= date( 'Y' ) . ' ';
if ( ! empty( get_bloginfo( 'name' ) ) ) {
$html .= '<span class = "site-title">' . get_bloginfo( 'name' ) . '</span>' . ' ';
if ( ! empty( $content ) ) {
$html .= '<span class="content-after">' . esc_html( $content ) . '</span>';
$html .= "</div>";
return $html;
add_shortcode( 'copyright_text', 'custom_copyright_text' );

This code represents a shortcode named copyright_text that enables the creation of a dynamic copyright notice. The shortcode output shows a Copyright label, followed by a copyright symbol. After that, the current year will be shown. Alternatively, you can opt to show a span of years, which will require you to insert the starting year.

Finally, the output will also show any text you choose to insert. However, to get that output, you need to know how to properly “call” a shortcode in one of the footer widget areas. For a more detailed explanation on how to do that, as well as this shortcode and its use, you can browse our article on creating dynamic copyright date notices.

Updating WordPress URLs

If you have recently changed domains or switched from HTTP to HTTPS, updating your WordPress URLs is one of the most important steps you need to take. Additionally, updating your WordPress URLs is an important troubleshooting step when dealing with login issues. Luckily, there are several methods you can use to do this, including adding code to the functions.php file.

To update your WordPress URLs via your functions.php file, you will need to add the following code to the end of the file.

update_option( 'siteurl', '' );
update_option( 'home', '' );

This code simply updates your WordPress and Site addresses with the values you insert. To clarify, the WordPress address represents the location of your WordPress core files, while the Site address represents the URL users type in to access your website.

Therefore, when using the code shown above, make sure to replace the part with your actual website URL. Also, if your website URL contains www, make sure to include it.

Please note, this code snippet is one of those that need to be used only once and then removed. Therefore, after adding the code in your functions.php file and checking the results, you should erase it. Otherwise, it could harm your website. For more on this subject, you can take a look at our article on changing WordPress URLs.

Redirecting Users after Login

Redirecting users after they log in is quite a useful way of streamlining their experience, especially if they are new to your site. Some of the reasons why you might want to do this are redirecting customers to the shop page, subscribers to the announcement page, or listing agents to an alternative dashboard. All of these can be achieved using custom code. Below, you can find an example of how a redirection code could look.

function custom_login_redirect( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) || in_array( 'author', $user->roles ) ) {
$redirect_to = admin_url();
} else if ( in_array( 'customer', $user->roles ) || in_array( 'shop_manager', $user->roles ) ) {
$redirect_to = home_url( '/shop' );
} else {
$redirect_to = home_url();
return $redirect_to;
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );

This code sorts user roles into three categories and redirects them accordingly. Those with the admin, editor, and author user role are redirected to the default dashboard, customers and shop managers are redirected to the Shop page (the page URL is your-website-url/shop), while all other users are redirected to the homepage. This is a very specific after login redirection that you can use or adapt to your website. For more on the subject, check out our article on redirecting users after login.

Qode Themes: Top Picks
Bridge New Banner

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme

A Genuinely Multi-Concept Theme

Startit WordPress Theme

Fresh Startup Business Theme

Adding a New Admin User

Every WordPress user, admin included, has lost access to the WordPress dashboard at least once. Most often, that happens because of a forgotten password. And, while it is relatively easy to recover your password using the recovery email, knowing alternative means how to do so can prove handy.

Instances where a different approach to password recovery might be needed include accidentally deleting your admin account, being hacked, or not being able to recover the password as the recovery email isn’t yours. In these cases, simply adding a new admin user is the easiest way of getting back your access to the WordPress dashboard.

You can use your functions.php file and the code snippet below to create a new admin user for your site.

function qode_add_new_admin_account() {
$user = 'insert-username-here';
$password = 'insert-password-here';
$email = 'insert-email-here';
if ( ! username_exists( $user ) && ! email_exists( $email ) ) {
$user_id = wp_create_user( $user, $password, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
add_action( 'init', 'qode_add_new_admin_account' );

The code is quite straightforward—it creates a new user with administrator privileges, which will have the username, password, and email you insert attached to it. Needless to say, you should replace the dummy data (insert-username-here, insert-password-here, and insert-email-here) with actual user information. The only condition that the code has is that the username and email you insert can’t belong to any of the existing users on your website. Make sure to keep that in mind when adding the appropriate information to the code.

After inserting the code into your functions.php file, navigate to your login page and try to log in. When you’ve logged in, make sure to remove the code you added from your functions.php file, as it only needs to be used once. To learn more about creating admin users, as well as see an alternative way to do it, you can go through our article on adding users via phpMyAdmin.

Adding Additional Image Sizes

Every time you upload an image to your Media Library, WordPress generates several default variations of that image. On top of that, WordPress allows you to add custom image sizes that work better with the orientation and dimensions of the images you upload. While this functionality should be used sparingly, as too many custom image sizes can slow down your website, it’s still very useful to have.

By inserting code into your functions.php file, you can add more custom image sizes in WordPress.

function register_custom_image_sizes() {
if ( ! current_theme_supports( 'post-thumbnails' ) ) {
add_theme_support( 'post-thumbnails' );
add_image_size( 'custom-small-square', 450, 450, true );
add_image_size( 'custom-landscape', 1000, 600 );
add_action( 'after_setup_theme', 'register_custom_image_sizes' );

This code can be divided into two parts—adding support for post thumbnails and registering two new custom image sizes using the add_image_size() function. The first part is a prerequisite, and we included it in case that your current theme hasn’t. In the second part, we have added two custom image sizes with the IDs titled custom-small-square and custom-landscape, respectively. The attributes we used for the former entail image dimensions of 450×450 pixels, and that the resulting image will represent a centrally cropped version of the original. As for the latter, the attributes we used mean that the original image will be scaled down until it fits at least one of the given dimensions (1000px for the width or 600px for the height).

When using this code, make sure to adapt it to your needs by changing all the parameters of the add_image_size() function accordingly. As this is a broad subject, we suggest reviewing our article on adding custom image sizes for a more in-depth guide.

Adding Custom Fonts

You might find yourself in a situation where you need a specific or custom font, but your current theme doesn’t have it. The right custom font or custom font combination can significantly improve the look of your website and its user experience. This is why a lot of webmasters opt to use a font that plays into their brand.

However, finding the right font to use is only half the problem. After choosing your custom font, you still need to properly add it to your website. While some users prefer plugins that solve this issue, there’s a way to include custom fonts using code and your functions.php file. We will show you how to properly enqueue custom font stylesheets on your website, so you can use your chosen font. You can find an example of the code below.

function qode_add_custom_fonts() {
wp_enqueue_style( 'qode-add-custom-font', 'custom-font-link', false );
add_action( 'wp_enqueue_scripts', 'qode_add_custom_fonts' );

This code represents a function we created called qode_add_custom_fonts(). It’s hooked onto the wp_enqueue_scripts hook, which is used for enqueueing scripts and styles that appear on the website’s frontend.

The content of the function is a single line of code that enqueues the font stylesheet using the wp_enqueue_style() function. In our example, we used the following parameters: qode-add-custom-font as the handle, custom-font-link as the URL of the font’s stylesheet, and false as the dependency (meaning the font stylesheet won’t require other stylesheets to be loaded first). Since we haven’t used them, all the remaining parameters take on their default values—the stylesheet version is the same as the currently installed WordPress version, and the stylesheet code is intended for all media types.

In most cases, you will only need to edit the code structure slightly. That includes making sure that any handle you use is unique. Also, you will need to replace the custom-font-link with a valid stylesheet URL. If your custom font is contained within a separate file, you will need to upload it via FTP to your current theme directory and replace the custom-font-link part with the URL of the uploaded file. However, if you are using Google fonts you only need to replace the custom-font-link part. We’ll explain how to locate a Google font URL below.

To locate the stylesheet URL of your chosen Google font, find the font on Google Fonts first. Then, after selecting all the styles you need, examine the Use on the web section in the options on the right. The appropriate URL is the value of the href attribute of the link tag, as shown in the screenshot below.

Adding custom fonts

In our example, the custom-font-link part should be replaced with;600&display=swap to properly enqueue the font stylesheet.

Afterward, you should use CSS code to apply the new font to different elements of your website. Here’s how that CSS code could look for the font that we used in our example.

h1 {
font-family: 'Dancing Script', cursive;

You can place the CSS you create into the Appearance > Customize > Additional CSS.

While this concludes the section on adding custom fonts via the functions.php file, enqueueing is an advanced WordPress subject. Therefore, we invite you to read through our article on enqueueing scripts and stylesheets for a more detailed explanation and examples of use. Also, you can find some suggestions on choosing the right font, as well as instructions on alternative ways to add fonts, in our article on adding custom WordPress fonts.

Adding Google Analytics

Web analytics provide the essential insights for building marketing strategies or improving your website as a whole. The code used for web analytics gathers data across a multitude of metrics and helps you understand the demographics of your site visitors better. The most common web analytics service nowadays is Google Analytics, which can be added to your website using several methods.

We’ll show how to add Google Analytics to your site by inserting a short code snippet to your functions.php file.

function qode_add_google_analytics() { ?>
// Paste your Google Analytics tracking code here
<?php }
add_action( 'wp_head', 'qode_add_google_analytics' );

The code represents a custom function called qode_add_google_analytics() that is hooked onto the wp_head action hook. This code is very simple and requires only one modification—replacing the dummy information with your own. Specifically, you need to set your Google Analytics code in place of the line containing // Paste your Google Analytics tracking code here.

As the wp_head() function is located within theelement, it means this code will be executed as if it was placed directly between the openingand closingtag, the place where the Google Analytics code should be added. Using this method (hooking the code onto the appropriate hook) to add Google Analytics code is the recommended approach; it’s preferable to manually editing the header.php template file.

Allowing Additional Upload File Types

For security reasons, WordPress allows only a limited number of file types to be uploaded directly via the dashboard. And depending on your server configuration, these restrictions could be even stricter. If you try to upload a file type that is not permitted to your website, you will see an error message similar to the one shown below.

Allowing additional upload file types

One way of avoiding this is by adding a snippet of code to your functions.php file, which will expand the list of file types allowed for upload. You can see an example of a code snippet that would do this below.

function additional_upload_file_types($mime_types){
$mime_types['svg'] = 'image/svg+xml';
$mime_types['psd'] = 'image/vnd.adobe.photoshop';
return $mime_types;
add_filter('upload_mimes', 'additional_upload_file_types', 1, 1);

This code allows you to upload .svg and .psd files within the WordPress admin dashboard. You can create similar lines of code to allow other non-permitted file types.

Bear in mind that the file upload process could also be restricted by your hosting provider. If that’s the case, you will need to contact them and request to be allowed to upload certain file types.

There’s also the option of allowing unfiltered uploads, which you should keep as a last resort. This is done by adding


in your wp-config.php file, above the /* That’s all, stop editing! Happy blogging. */ comment line.

This line of code allows administrators to upload any file type, but it poses a huge security risk. If you decide to use it, we recommend you do so briefly and that you remove the line of code as soon as you upload the files.

Narrowing the Search Scope

If your website is separated into several sections (for example, blog and shop), it’s a good idea to have separate search results for each. To help your visitors find what they’re looking for and to narrow the search scope, you can exclude certain post types as potential search results. This can be achieved quite easily by adding an appropriate code snippet inside your WordPress functions.php file. You can find an example below.

function qode_set_search_scope( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page' ) );
return $query;
add_filter( 'pre_get_posts', 'qode_set_search_scope' );

By using this code, you will ensure that only pages and posts appear in the search results. Knowing that, you can edit the code snippet above according to your needs. For example, you can change the allowed post types or add new ones that are registered on your website.

Creating Custom Widget Areas

One of the many advantages of WordPress is the potential to customize almost any aspect of your website. This can be done through both custom code and the use of plugins. In this section, we will talk about customizing your site by creating custom widget areas, which are often needed to achieve a certain page look.

The process of creating a custom widget area can be broken down into two parts—registering the widget area and adding the code that displays it inside the appropriate template file. We will include examples for both and explain them below.

function register_custom_widget_area() {
'id' => 'new-widget-area',
'name' => esc_html__( 'My new widget area', 'theme-domain' ),
'description' => esc_html__( 'A new widget area made for testing purposes', 'theme-domain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="widget-title-holder"><h3 class="widget-title">',
'after_title' => '</h3></div>'
add_action( 'widgets_init', 'register_custom_widget_area' );

This code registers a custom widget area titled My new widget area with the description A new widget area made for testing purposes shown underneath. The unique ID of this widget area is the new-widget-area, and it will later be used to reference it.

As for the remaining parameters, the before_widget and after_widget parameters represent the HTML structure that will wrap the content of any widget inserted inside the widget area. And the before_title and after_title represent the HTML structure that wraps the widget area title.

The widget area is registered using the register_sidebar() function, using the parameters mentioned above. Apart from them, this function accepts additional parameters which we didn’t use, but you can see them on the function’s documentation page. Once you know all the parameters this function accepts, you can register your custom widget area by editing the code we provided accordingly.

After registering the custom widget area, you will need to add the code that displays it in a proper place on your website. An example of that code is given below.

<?php if ( is_active_sidebar( 'new-widget-area' ) ) : ?>
<div id="secondary-sidebar" class="new-widget-area">
<?php dynamic_sidebar( 'new-widget-area' ); ?>
<?php endif; ?>

The code is rather simple—it uses the is_active_sidebar() function to check whether you added any widgets to the area with the ID new-widget-area. If you added content inside the new-widget-area, it will display it using the dynamic_sidebar() function.

To use the piece of code intended for displaying your custom widget area, you will need to add it to an appropriate template file. The most frequently used are header.php, footer.php, or sidebar.php. As we covered this code in great detail in our article on adding custom widget areas, we suggest reviewing it for more information.

For an added layer of customization, you can include your custom widget areas within custom page templates. To learn more about that, you can read our article on creating custom page templates.

Creating Custom Widgets

To accompany the creation of custom widget areas, we have decided to cover how to create custom widgets as the last item on this list. As widgets, depending on their purpose, can vary in complexity quite a lot, we will show you how the structure of a basic widget should look like. Generally, it should look like the structure below.

class My_Widget extends WP_Widget {
function __construct() {
// Some code here
public function widget( $args, $instance ) {
// Some code here
public function form( $instance ) {
// Some code here
public function update( $new_instance, $old_instance ) {
// Some code here
function name_of_the_register_function() {
register_widget( 'My_Widget' );
add_action( 'widgets_init', 'name_of_the_register_function' );

This code represents the custom widget called My_Widget, which is registered using the register_widget() function. The My_Widget is a class object that extends the WP_Widget class given by the Widget API. Our My_Widget class has four methods: __construct(), widget(), form() and update(). The __construct() method creates the widget, the widget() method displays the output of the widget, the form() displays the widget options in the backend, and the update() method is responsible for properly updating the options selected within the widget. As this is a very advanced topic, we suggest reading more about it in our article on creating custom WordPress widgets to get detailed information.

Final Thoughts

The WordPress functions.php file can be a versatile tool for a creative webmaster. We shared a selection of neat tricks you can accomplish with this file, but this list is by no means exhaustive. We urge you to explore and try some of these things out on your own. There’s a range of things you can do. You can use small code snippets to troubleshoot certain WordPress errors like adding a new admin user or updating WordPress URLs. You can also improve existing functionalities—add new upload file types or image sizes, implement new features such as redirects after login. Or create custom widget areas, widgets, or shortcodes of various kinds.

WordPress’s functions.php file allows users to modify or create a number of features and functionalities. By now, we hope you have become convinced of the usefulness of this file and the flexibility it provides. As there is only so much we can cover in a single article, we invite you to take a peek at some of our other tutorials for more helpful code snippets and tricks.

Post your comment