Yes, this can be done. Ultimately, adding and removing a widget to/from a sidebar means updating an option in the database with a call like this:
update_option( 'sidebars_widgets', array( ... ) )
If you look at the code of update_option
you see that there is a filter called just before the actual update. So if you want to do something before a widget is removed you can hook into:
add_filter ('pre_update_option_sidebars_widgets','wpse71075_pre_remove_widget',10,3);
At this point you only know that the list of active widgets is going to change, not whether it is addition or a removal. However, you do have $value
and $old_value
of the option, which you can compare inside your filter function (an array_search
for a widget’s label might be enough).
Read the answer to this question to learn about the structure of the sidebars_widgets
option. It depends on your goal what you actually want to detect and do.
Related Posts:
- Anyway to edit the titlebar of WordPress Widgets in the Admin area?
- WordPress tag cloud add more links
- How to use apply_filters(‘get_calendar’) to change get_calendar() output?
- WordPress hooks/filters insert before content or after title
- How do filters and hooks really work in PHP
- Trouble understanding apply_filters()
- Custom theme hooks / filters – passing arguments
- wp_mail – Remove sitename from email subject
- Is it possible to remove the filter from 4.8 text widget?
- How to hook wp_list_pages?
- WP Rest API – Upload media without saving attachment post
- How to make post and comment count unclickable with dashboard_glance_items hook
- Woocommerce Product Category Widget – hide categories that have no products in stock [closed]
- How to limit the pages displayed for choosing parent page on page attribute’s menu?
- How can I hide all posts that don’t have a thumbnail?
- How to exclude certain widget from showing up on home/front page? [duplicate]
- How can I reliably and globally disable wptexturize?
- Customise Jetpack Publicize text
- WordPress RSS feed – filter RSS content by custom field value
- filter the_title problem in nav
- How can I add a fifth option to the alignment picker?
- Filter the URL of next_posts_link & previous_posts_link
- WordPress: Apply filter/hook to a particular sidebar widgets?
- How to replace any occurence of Gravatars with a local placeholder image?
- Sensei LMS Hooks to Remove Content
- What hook/filter can I use to add/edit/show/hide the title under (on hover) links on the table view?
- Customize user account activation message
- Conditionally call add_action depending on post_type?
- Add a header before fields added with the attachment_fields_to_edit() filter
- Error when overriding only some audio shortcode HTML output
- Hide content editor for posts after approriate date
- Gutenberg disable the “block” tab in right sidebar
- Why my admin doesn’t work after adding rest_prepare_post filter?
- When to use actions and when to use filters
- add_filter to ‘woocommerce_before_main_content’ [closed]
- Editing
- Is possible dequeue/remove style from wp_footer() hook and add on wp_head() hook?
- Same Conditionals Not Working on Two Different Hooks
- Filter or Hook to catch pre-rendering of post content
- How to call a function or method that is Namespaced using another plugin
- check to see if hook is available
- Change username before login
- How do I target a single page to modify the comment form of only that page?
- How to removes all instances of thumbnails displayed in my theme?
- Insert term when page is published – avoid duplicates after edits
- When does a function assigned to the content_filtered_edit_pre filter hook fire?
- Passing values from a widget to a function within a plugin
- Capture post content before page renders
- Remove_action does not work
- Add content as soon starts
- Make an array filterable per hook
- Replace a menu with widget or a custom template file programmatically
- How do I add a checkmark to my-sites save settings hook
- How to know what filter to use and how to use it?
- How to filter backend post list showing only posts having a shortcode
- Add Lightbox To WordPress Native Gallery
- What hooks to hook onto for automatic cache clearing
- How can I replace an oEmbed URL in post with actual embed HTML
- Modify Redux Framework Options in Child Theme
- How to customize category_description()?
- Add Bootstrap Classes to Recent Posts Widget
- Detect when gutenberg editor title is available in Dom after editor load
- How can I output all apply_filters and do_action?
- Set default value for “Additional CSS class(es)” for specific block types
- Is it possible to bind a function to a filter hook via Ajax?
- How do I hook my function to run only after submitting login form
- How to add a filter to the get_body_class function?
- Prepending character(s) to a custom tag title?
- Modify existing plugin function with add_filter
- include specific Pages to wp_list_pages with filter
- How to override include_once pointed file using add_filter?
- Remove tags without a specific meta key from “choose from the most used tags”
- What is the action hook to use if you want to capture the new password during password change?
- an action hook when a post reaches a certain number of views
- Action hook to control access to certain parts of my site
- Need to return shortcode text instead of the output
- Replace Data In Post & Update Meta Field Post Is Saved
- Filter Media by attached page or blog post in Library
- How to add custom media library mime icons?
- Add an attribute to the root from functions.php with add_filter
- Would like to use hook or filters to customize email templates
- Remove image of srcset
- Security question – Display a General Custom Login Error Message
- Filter wp_mail based on content type
- Add HTML to the bottom of each post in a post list
- How can I add a prefix to titles displayed in sidebar using function.php?
- Incrementing content with extra text after save/publish
- wp_editor customization
- WP Dashboard -> Posts-> Filter by Category -> Form Method Change : Which Hook
- Filter dashboard custom post listing by user
- Admin New Order: Autofocus on Search a product
- NextGEN Gallery: Adding drop-down menu widget to gallery view without modifying plugin code [closed]
- How to filter part of a variable if it is no array?
- How do you disable the verification process of user email changes?
- Better way to change the default password reset url with the woocommerce one?
- Hooking/Filtering theme name to add theme directory
- How to elect position of new item output in a dropdown when using add_filter
- Replacing text using add_filter
- Passing value from one hook to another
- Change password reqts with NO plugin without breaking resetpass link?