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:
- Install and activate the free WPCode plugin from WordPress.org
- Go to Code Snippets → Add Snippet
- Click “Add New Snippet”
- Give your snippet a title (e.g., “Gutenberg Scroll to Top”)
- Select “PHP Snippets” as the code type
- Copy and paste the provided code
- Set “Location” to “Universal”
- Toggle the snippet to “Active”
- 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:
- Create or edit any post in WordPress
- Scroll down in the editor
- Look for the arrow button in the bottom right
- 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.