Stop the Scroll! v2.0 – Float the Admin Notice Above the Editor in Code Snippets Pro

Hey Look Note Floats

This snippet enhances Code Snippets Pro ❤️ by making admin notices float above the editor and automatically dismiss after a configurable time period. It keeps important messages visible while you’re editing, then cleanly removes them to prevent interface clutter.

Once upon a time, when you hit ‘Save’ on a snippet inside the Code Snippets Pro ❤️ code editor, the entire page would refresh.

It got the job done, but it was bothersome because you couldn’t undo or see what went wrong, etc.

So the clever developers of Code Snippets Pro ❤️ made a wonderful change where it would save your code without! refreshing the page, and then an admin notice would display at the very top of the page that would indicate if your snippet was saved correctly or if there was an error, etc.

It got the job done, but it was bothersome because if you were working on a large block of code and were scrolled down far enough, the notice would be hidden and you might never… “notice” …if something had gone wrong. And you would keep working and changing stuff and writing awesome snips and and and annnnnd… then you would scroll up and realize there was an error. But you had no clue when it happened or how many times you needed to press Undo! Undo! Undo!

So the clever developers of Code Snippets Pro ❤️ did a great job quickly solving this by ensuring the page would scroll the notice into view anytime a notice appeared.

It got the job done, but it was bothersome, to me, at least. I kept losing my place of where I was working.

So this snippet keeps notices visible without interrupting your work by floating them at the top of your window.

After a few seconds, they’ll smoothly fade away – keeping you informed without cluttering your workspace.

It gets the job done, and isn’t so bothersome.

What it does…

This code snippet adds a little bit of CSS to the admin notice in the Code Snippets Pro ❤️ plugin for WordPress. If you’re on the Add New Snippet or Edit Snippet page, it makes the admin notice float at the top of your window on top of the code snippet editor so that when you save the page doesn’t scroll away. This ensures that the notice is always visible and you don’t lose your place, even when working on a large block of code and scrolled far down the page.

Here’s What You Can Customize

Basic Customization

Look for these constants at the top of the class definition:

  • DEFAULT_DISMISS_DELAY – How long notices stay visible (default: 5000 milliseconds)
  • DEFAULT_NOTICE_OFFSET – Distance from top of viewport (default: 40 pixels)

Advanced Customization

Advanced users can modify these values using the WordPress options API with the option name floating_notice_settings. Add this code to another snippet or your theme’s functions.php:

// Example: Change settings via WordPress options API
$options = get_option('floating_notice_settings', []);
$options['dismiss_delay'] = 3000;  // 3 seconds
$options['notice_offset'] = 60;    // 60px from top
update_option('floating_notice_settings', $options);

Using the options API allows settings to persist across snippet updates.

Why it does it…

By default, the admin notice in Code Snippets Pro ❤️ appears at the top of the page when a snippet is saved or when there is an error. And to make sure you see it, the page scrolls to the notice anytime a notice appears. If you are working on a large block of code and scrolled far down the page, it can get a little distracting every time the page scrolls up to the notice when you save. This modification addresses this issue by making the notice float above the editor, ensuring it is always visible and preventing the page from scrolling.

How it does it…

How It Does It

This code snippet creates floating, auto-dismissing admin notices in the Code Snippets Pro ❤️ editor. Here’s how it works:

  • The code implements a class WP_Floating_Admin_Notice_With_AutoDismiss with configurable settings for dismiss delay and notice offset.
  • Class constants define default values (5 second dismiss delay, 40px top offset).
  • A WordPress options API integration allows persistent storage of custom settings.
  • The styling uses CSS position: sticky and z-index: 999 to float notices above the editor.
  • JavaScript implements the auto-dismiss functionality:
    • A main event listener waits for page load
    • An IIFE creates a private scope to avoid conflicts
    • The autoDismissNotice() function adds a fade-out class after the configured delay
    • A MutationObserver watches for new notices and applies the auto-dismiss behavior
  • The code checks for specific admin pages to only load on snippet edit screens
  • Security measures include input sanitization, output escaping, and proper WordPress coding standards
  • Error handling ensures graceful degradation if settings are missing
  • CSS transitions provide smooth fade-out animations when notices are dismissed

Download JSON for importing into Code Snippets Pro
View on…

Related Snips