I’m trying to implement a feature in the WordPress editor that can show a notification whenever someone saves a post, but I want this to happen only under specific conditions. Let’s say I want the notification to pop up if the user hasn’t added any tags or if the post is marked as ‘draft’ for more than a week. Ideally, the notification should be something like, “Hey, don’t forget to add some tags!” or “This draft has been sitting for too long. Ready to publish?”
I’m pretty decent with coding, but I’m relatively new to WordPress’s hook and filter system. What’s the best way to implement this? I’ve looked into using JavaScript and the WordPress REST API, but I’m not too sure where to start. Should I write a custom function that checks these conditions when a post is saved? Also, how do I ensure that my notification only shows up under those conditions and is user-friendly, without being too intrusive?
I’ve heard of using the `wp.data` package for managing state in the post editor, which seems like it could be useful here. But what’s the best approach for hooking into the save action and performing these checks without slowing down the saving process?
And another thing, if I manage to get this working, how do I make sure that this notification doesn’t overwhelm users, especially if they save frequently while editing? I want to balance being helpful without being annoying. Should I consider implementing a temporary dismissal option for the notification or something similar?
If anyone has come across a similar challenge or has any snippets or guidance on this, I’d really appreciate your thoughts. It feels like a fun project, but I’m a bit stuck on the best implementation strategy. Any advice or examples you can share would be super helpful!
To implement a notification system in the WordPress editor that triggers under specific conditions, you can leverage WordPress’s built-in hooks and the JavaScript `wp.data` package. Begin by registering a custom script in your theme or plugin that enqueues your JavaScript file which will handle the notification logic. Use `wp.hooks` to hook into the ‘savePost’ action. In this hooked function, check if the post has any tags and if it’s been marked as ‘draft’ for more than a week. You can use the WordPress REST API to fetch the post data and determine these conditions. If either condition is met, trigger a user-friendly notification using `wp.data.dispatch(‘core/notices’).createNotice()`, providing a dismissible option to ensure that users won’t be inundated with alerts if they save frequently.
To make your notification system less intrusive, implement a local storage strategy to track whether the user has already dismissed the alerts. This means that once they dismiss a notification, you can prevent it from showing again for a defined period, like a day or a week. Additionally, consider debouncing the notification trigger to further reduce noise. This can be achieved by setting a timeout or using a flag that checks if a notification has been shown recently. This way, you maintain a balance between helping users remember important actions and respecting their workflow by not disrupting their editing process.