Add Back to Top Button

Beginner
✓ Tested
📅 September 20, 2025
⬇️ 0 Downloads
📋 0 Copies

This simple snippet adds a floating “Back to Top” button that appears when users scroll down the page. The button smoothly scrolls back to the top when clicked, improving user experience and navigation.

What does this code do?

The snippet creates a circular floating button with an arrow icon that appears in the bottom-right corner when users scroll down more than 300 pixels. Clicking the button smoothly scrolls back to the top of the page.

Features:

  • Floating circular button with arrow icon
  • Appears automatically when scrolling down
  • Smooth scroll animation to top
  • Hover effects with subtle animation
  • Mobile responsive design
  • No jQuery required – pure JavaScript
  • Works on all pages automatically

Design details:

  • Dark gray background (#333)
  • White arrow icon (↑)
  • Circular shape with shadow
  • Positioned bottom-right corner
  • Smooth fade in/out transitions
  • Hover effect with lift animation

Responsive features:

  • Smaller size on mobile devices
  • Adjusted positioning for mobile
  • Touch-friendly click area
  • Works on all screen sizes

⚠️ Important Warnings

⚠️ Always backup before adding new code
⚠️ Test on different devices and browsers
⚠️ Some themes might override button styles
⚠️ Check for conflicts with existing back-to-top buttons
⚠️ High z-index might interfere with popups or modals
⚠️ Smooth scroll not supported in very old browsers
⚠️ Button might conflict with chat widgets in same position
⚠️ Test with caching plugins enabled
💻 PHP Code
/**
 * Add smooth back to top button to website
 * Simple floating button that appears when scrolling down
 * Prefix: tpsc_ (TP Snippet Collection)
 */

// ============================================
// ADD BACK TO TOP BUTTON HTML
// ============================================

// Add button to footer
add_action('wp_footer', 'tpsc_add_back_to_top_button');
function tpsc_add_back_to_top_button() {
    echo '<div id="tpsc-back-to-top" title="Back to Top">
        <span class="tpsc-arrow">↑</span>
    </div>';
}

// ============================================
// ADD CSS STYLES
// ============================================

// Add CSS for the button
add_action('wp_head', 'tpsc_back_to_top_styles');
function tpsc_back_to_top_styles() {
    echo '<style>
        #tpsc-back-to-top {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 50px;
            height: 50px;
            background-color: #333;
            color: white;
            border-radius: 50%;
            text-align: center;
            line-height: 50px;
            cursor: pointer;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
            z-index: 9999;
            box-shadow: 0 2px 10px rgba(0,0,0,0.3);
        }
        
        #tpsc-back-to-top:hover {
            background-color: #555;
            transform: translateY(-3px);
            box-shadow: 0 4px 15px rgba(0,0,0,0.4);
        }
        
        #tpsc-back-to-top.show {
            opacity: 1;
            visibility: visible;
        }
        
        .tpsc-arrow {
            font-size: 20px;
            font-weight: bold;
        }
        
        /* Mobile responsive */
        @media (max-width: 768px) {
            #tpsc-back-to-top {
                bottom: 20px;
                right: 20px;
                width: 45px;
                height: 45px;
                line-height: 45px;
            }
            
            .tpsc-arrow {
                font-size: 18px;
            }
        }
    </style>';
}

// ============================================
// ADD JAVASCRIPT FUNCTIONALITY
// ============================================

// Add JavaScript for scroll behavior
add_action('wp_footer', 'tpsc_back_to_top_script');
function tpsc_back_to_top_script() {
    echo '<script>
        document.addEventListener("DOMContentLoaded", function() {
            var backToTopButton = document.getElementById("tpsc-back-to-top");
            
            // Show/hide button based on scroll position
            window.addEventListener("scroll", function() {
                if (window.pageYOffset > 300) {
                    backToTopButton.classList.add("show");
                } else {
                    backToTopButton.classList.remove("show");
                }
            });
            
            // Smooth scroll to top when clicked
            backToTopButton.addEventListener("click", function() {
                window.scrollTo({
                    top: 0,
                    behavior: "smooth"
                });
            });
        });
    </script>';
}

📝 Installation Instructions

QUICK START:

Copy the entire code
Paste in functions.php
Visit your website and scroll down
The back to top button will appear automatically!

CUSTOMIZATION OPTIONS:
TO CHANGE BUTTON COLOR:
Find: background-color: #333;
Change to any color:

Blue: #3498db
Green: #27ae60
Red: #e74c3c
Purple: #9b59b6

TO CHANGE BUTTON SIZE:
Find: width: 50px; height: 50px;
Change both values to same number:

Larger: 60px
Smaller: 40px

TO CHANGE POSITION:
Find: bottom: 30px; right: 30px;
Adjust values:

Move left: right: 100px;
Move up: bottom: 100px;
Left side: left: 30px; (remove right)

TO CHANGE WHEN BUTTON APPEARS:
Find: if (window.pageYOffset > 300)
Change 300 to different pixel value:

Earlier: 200 (appears sooner)
Later: 500 (appears later)

TO CHANGE ARROW ICON:
Find:
Replace ↑ with:

Different arrow: ⬆ ▲ ⇧
Text: TOP
Other symbols: ☝ 🔝

TO CHANGE HOVER EFFECT:
Find: transform: translateY(-3px);
Adjust the lift effect:

More lift: translateY(-5px);
Less lift: translateY(-1px);
No lift: remove the line

TO ADD SMOOTH BORDER:
Add this line after border-radius: 50%;:
border: 2px solid #555;
TO CHANGE SCROLL SPEED:
The smooth scroll is handled by browser.
For custom speed, replace:
behavior: "smooth"
With custom animation (advanced).
TESTING:

Visit any page on your site
Scroll down past 300 pixels
Button should fade in
Click button to scroll to top
Test on mobile devices
Check different pages

TROUBLESHOOTING:

If button doesn't appear: Check console for JavaScript errors
If styling is wrong: Clear cache and check CSS
If not working on mobile: Test touch events
If conflicts with theme: Adjust z-index value

📸 Screenshots

💬 Let's Connect and Share!

Got a question about this snippet?

Something not working as expected? Need help customizing it for your specific needs?

💡

Want to request a custom snippet?

Have an idea for functionality you're missing on your site? Tell us what you're looking for!

🤝

Have an awesome snippet you're using?

Share it with us! Our community loves learning and growing together.

No comments yet.

Leave a Comment

Leave a Comment

To leave a comment, please enter your email address. We will send you a verification code to confirm your identity.

Email Verification

We sent a 6-digit verification code to your email address. Please check your inbox and enter the code below:

Write Your Comment

Great! Your email is verified. Now you can write your comment:

Comment Submitted!