Building a Custom Back-to-Top Button in WordPress Without Plugins

Improving user experience is crucial for engaging website visitors. A custom back-to-top button streamlines navigation by allowing users to effortlessly return to the top of a page with a single click. This tutorial guides you through creating this button for your WordPress site without relying on plugins.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Creating the HTML Structure in WordPress with wp_footer

To begin, we’ll use the wp_footer action hook in the functions.php file to insert the HTML structure for the back-to-top button just before the closing </body> tag. This ensures optimal loading and performance.

function add_back_to_top_button() {
    echo '<div id="backToTopBtn"><span>&uarr;</span></div>';

add_action('wp_footer', 'add_back_to_top_button');
Code language: PHP (php)

Implementing Smooth Scrolling with jQuery

The JavaScript/jQuery code, placed in a separate back-to-top.js file, creates a smooth scrolling effect. It fades in the button when users scroll down and fades it out when they are at the top. On clicking the button, it smoothly scrolls back to the top of the page.

jQuery(document).ready(function($) {
    $(window).scroll(function() {
        if ($(this).scrollTop() > 100) {
        } else {

    $('#backToTopBtn').click(function() {
        $('html, body').animate({ scrollTop: 0 }, 800);
        return false;
Code language: JavaScript (javascript)

Alternatively, if your theme already has a custom JavaScript file, you can add this code there to minimize HTTP requests. If you prefer not to create a new JS file, you can include this code directly in your theme’s header.php file or use the wp_footer hook in your functions.php.

Styling the Back-to-Top Button

The CSS code styles the button for a polished appearance, making it fixed at the bottom-right corner. It includes hover effects for a visually appealing interaction.

Similarly, if your theme already includes a custom CSS file, consider adding this styling code there to keep your styles organized. Alternatively, you can include this code directly in your theme’s style.css file.

#backToTopBtn {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 20px;
    background-color: #0073e6;
    color: #fff;
    border: none;
    border-radius: 50%;
    padding: 10px;
    cursor: pointer;
    font-size: 20px;

#backToTopBtn:hover {
    background-color: #005bb5;
Code language: CSS (css)

Testing and Troubleshooting

Thoroughly test the button’s functionality on various pages. Troubleshoot any styling issues and verify its compatibility across different browsers.

That’s it! You’ve successfully crafted a custom back-to-top button for your WordPress site without relying on plugins. This simple addition significantly improves user navigation and contributes to an enhanced overall user experience.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

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