How to Add a Back-to-Top Button in WordPress Without a Plugin


In this tutorial, you will learn How to Add a Back-to-Top Button in WordPress Without a Plugin step by step. Improve user experience.


Published on: June 18, 2023

How to Add a Back-to-Top Button in WordPress Without a Plugin

Unlock the Power of WordPress 6: Build Powerful Websites (Even Without Coding!)

WordPress 6 just dropped! Learn everything you need to know to create stunning websites with the latest features. No coding required!

Sign Up Now!

Welcome to this tutorial on How to Add a Back-to-Top Button in WordPress Without a Plugin.

A back-to-top button is a handy feature that allows users to effortlessly navigate back to the top of a page with a single click.

By following this step-by-step guide, you’ll be able to implement a back-to-top button using built-in WordPress functionality, improving user experience and navigation on your website.

Let’s get started!

Why Add a Back-to-Top Button in WordPress?

Before we dive into the tutorial, let’s understand why adding a back-to-top button is beneficial for your WordPress website:

  1. Enhanced user experience: A back-to-top button provides a convenient way for users to return to the top of the page without scrolling manually. It improves navigation, especially on long pages, and saves users time and effort.
  2. Improved engagement: By offering an easy way to return to the top, a back-to-top button encourages users to explore more content on your website, potentially increasing engagement and reducing bounce rates.
  3. Mobile-friendliness: With the growing number of mobile users, a back-to-top button ensures a smooth scrolling experience on smaller screens, where reaching the top can be more challenging.

Now that we understand the advantages, let’s proceed with the step-by-step process of adding a back-to-top button to your WordPress site without relying on a plugin.

Step 1: Create a Child Theme (For Safety)

To ensure the safety and maintainability of your changes, it’s highly recommended to create a child theme.

By creating a child theme, you can make customizations without modifying the original theme files.

Follow the steps outlined in our previous tutorial to create and activate a child theme specific to your WordPress installation.

Step 2: Add the Back-to-Top Button Markup

  1. Connect to your WordPress installation using FTP or a file manager.
  2. Navigate to the child theme directory (wp-content/themes/my-theme-child).
  3. Locate and open the header.php file in a text editor.

Now, let’s add the back-to-top button markup using WordPress functions.

  1. Within the <body> section of the header.php file, add the following code:
    <?php if (is_single() || is_page()) : ?>
      <a href="#" class="back-to-top">Back to Top</a>
    <?php endif; ?>
  1. Save the changes to the header.php file.

Step 3: Add the Back-to-Top Button Styling

  1. Open the WordPress editor and navigate to “Appearance” > “Customize.”
  2. Click on “Additional CSS” to access the Customizer’s CSS editor.

Now, let’s add the back-to-top button styling.

  1. Add the following CSS code to the editor:
    .back-to-top {
      display: none;
      position: fixed;
      bottom: 20px;
      right: 20px;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      font-size: 16px;
      background-color: #000;
      color: #fff;
      border-radius: 50%;
      z-index: 9999;
    }
    
    .back-to-top.show {
      display: block;
    }
  1. Click “Publish” to save the changes.

Step 4: Add JavaScript Functionality

  1. Connect to your WordPress installation using FTP or a file manager.
  2. Navigate to the child theme directory (wp-content/themes/my-theme-child).
  3. Create a new directory called js.
  4. Inside the js directory, create a new file called back-to-top.js.
  5. Open the back-to-top.js file in a text editor and add the following JavaScript code:
    (function($) {
      $(document).ready(function() {
        $(window).scroll(function() {
          if ($(this).scrollTop() > 200) {
            $('.back-to-top').addClass('show');
          } else {
            $('.back-to-top').removeClass('show');
          }
        });
    
        $('.back-to-top').click(function(e) {
          e.preventDefault();
          $('html, body').animate({ scrollTop: 0 }, '300');
        });
      });
    })(jQuery);
  1. Save the changes to the back-to-top.js file.

Step 5: Enqueue the JavaScript File

  1. Connect to your WordPress installation using FTP or a file manager.
  2. Navigate to the child theme directory (wp-content/themes/my-theme-child).
  3. Open the functions.php file in a text editor.

Now, let’s enqueue the JavaScript file.

  1. Add the following code to the functions.php file:
    function enqueue_back_to_top_script() {
      wp_enqueue_script(
        'back-to-top-script',
        get_stylesheet_directory_uri() . '/js/back-to-top.js',
        array('jquery'),
        '1.0',
        true
      );
    }
    add_action('wp_enqueue_scripts', 'enqueue_back_to_top_script');
  1. Save the changes to the functions.php file.

Step 6: Preview and Publish

Preview your website to see the back-to-top button in action. As you scroll down the page, the button will appear after reaching a certain scroll threshold. Clicking on the button will smoothly scroll the page back to the top.

It’s all done.

Congratulations! You’ve successfully added a back-to-top button to your WordPress website without relying on a plugin.

By using WordPress functions, HTML markup, CSS styling, and JavaScript, you’ve enhanced the user experience and navigation of your website.

Remember to maintain your child theme and regularly update the JavaScript and CSS files as needed.

Feel free to customize the button’s appearance and positioning according to your website’s design preferences.

If you have any questions or need further assistance, please leave a comment below.

Happy scrolling with the back-to-top button!

Unlock the Power of WordPress 6: Build Powerful Websites (Even Without Coding!)

WordPress 6 just dropped! Learn everything you need to know to create stunning websites with the latest features. No coding required!

Sign Up Now!

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.