How to Add a Scroll to Top Button in WordPress Gutenberg Editor?

Are you tired of endless scrolling in the WordPress block editor when working on long posts? Learn how to add a convenient “Scroll to Top” button to your Gutenberg editor in just a few minutes. This tutorial covers two easy implementation methods, perfect for both beginners and experienced users.

What Does This Feature Do?

The “Scroll to Top” button appears when you scroll down in the Gutenberg editor, allowing you to instantly jump back to the top of your post with a single click. This feature:

  • Saves time when editing long posts
  • Reduces mouse wheel fatigue
  • Improves your content creation workflow
  • Works seamlessly with the Gutenberg interface

(see the screenshot below)

Method 1: Using the WPCode Plugin (Recommended)

This is the easiest and safest method for beginners. Here’s why:

  • No direct file editing is required
  • Changes survive theme updates
  • Easy to enable/disable
  • No coding knowledge needed

Steps to Implement:

  1. Install and activate the free WPCode plugin from WordPress.org
  2. Go to Code Snippets → Add Snippet
  3. Click “Add New Snippet”
  4. Give your snippet a title (e.g., “Gutenberg Scroll to Top”)
  5. Select “PHP Snippets” as the code type
  6. Copy and paste the provided code
  7. Set “Location” to “Universal”
  8. Toggle the snippet to “Active”
  9. Click “Save Snippet”
// Add a Scroll to Top Button in WordPress Gutenberg Editor
// Add button HTML and styles to admin head
function add_scroll_button_styles() {
    // Only on post editor screens
    global $pagenow;
    if (!in_array($pagenow, ['post.php', 'post-new.php'])) {
        return;
    }
    ?>
    <style>
        body.block-editor-page .editor-scroll-to-top {
            position: sticky !important;
            bottom: 30px !important;
            right: 30px !important;
            float: right;
            margin-top: -20px; /* Adjust this value if needed */
            z-index: 99999999;
            width: 40px;
            height: 40px;
            background: #2a2a2a;
            border-radius: 4px;
            cursor: pointer;
            display: none;
            align-items: center;
            justify-content: center;
            border: none;
            padding: 0;
            transform: translateY(-100%);
        }

        body.block-editor-page .editor-scroll-to-top .dashicons {
            color: white;
            font-size: 24px;
            width: 24px;
            height: 24px;
        }

        body.block-editor-page .editor-scroll-to-top:hover {
            background: #444;
        }

        .scroll-button-wrapper {
            position: sticky !important;
            bottom: 0 !important;
            right: 0 !important;
            height: 0;
            width: 100%;
            pointer-events: none;
        }

        .scroll-button-wrapper .editor-scroll-to-top {
            pointer-events: all;
        }
    </style>
    <?php
}
add_action('admin_head', 'add_scroll_button_styles');

// Add button HTML and JavaScript
function add_scroll_button_script() {
    global $pagenow;
    if (!in_array($pagenow, ['post.php', 'post-new.php'])) {
        return;
    }
    ?>
    <script>
    window.addEventListener('load', function() {
        setTimeout(function() {
            const editor = document.querySelector('.interface-interface-skeleton__content');
            if (!editor) return;

            // Create wrapper div for proper positioning
            const wrapper = document.createElement('div');
            wrapper.className = 'scroll-button-wrapper';
            
            // Create the button
            const button = document.createElement('div');
            button.className = 'editor-scroll-to-top';
            button.innerHTML = '<span class="dashicons dashicons-arrow-up-alt2"></span>';
            
            // Add button to wrapper
            wrapper.appendChild(button);
            
            // Add wrapper to editor
            editor.appendChild(wrapper);

            // Show/hide button based on scroll position
            function toggleButton() {
                if (editor.scrollTop > 300) {
                    button.style.display = 'flex';
                } else {
                    button.style.display = 'none';
                }
            }

            // Initial check
            toggleButton();

            // Add scroll listener
            editor.addEventListener('scroll', toggleButton);

            // Scroll to top when clicked
            button.addEventListener('click', function() {
                editor.scrollTo({
                    top: 0,
                    behavior: 'smooth'
                });
            });
        }, 1000);
    });
    </script>
    <?php
}
add_action('admin_footer', 'add_scroll_button_script');

Method 2: Adding to functions.php (Advanced Users)

While you can add this code to your child theme’s functions.php file, we don’t recommend this approach because:

  • Theme updates might cause conflicts
  • Switching themes will remove the functionality
  • Requires FTP access and file editing
  • There is a higher risk of breaking your site

Why Choose WPCode Over Child Theme Method?

Safety First: WPCode includes syntax error checking to prevent site crashes

Easy Management:

  • Enable/disable features with one click
  • No need to edit theme files
  • Organize all your custom code in one place

Portability:

  • Your customizations stay even if you change themes
  • Easy to backup and transfer between sites
  • No need to recreate child themes

Debugging:

  • Better error reporting
  • Easy to troubleshoot issues
  • Quick to disable if something goes wrong

Testing Your New Feature

After implementation:

  1. Create or edit any post in WordPress
  2. Scroll down in the editor
  3. Look for the arrow button in the bottom right
  4. Click it to smoothly return to the top

Troubleshooting Tips

If the button doesn’t appear:

  • Clear your browser cache
  • Make sure you’re using the latest WordPress version
  • Check if your theme is compatible with Gutenberg
  • Verify the code is properly activated

Conclusion

Adding a scroll to top button to your Gutenberg editor is a simple way to improve your content creation workflow. While there are multiple ways to implement this feature, using the WPCode plugin offers the best balance of ease and reliability for most users.

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 134