Make WordPress Core

Opened 4 years ago

Closed 2 years ago

#26235 closed enhancement (wontfix)

Add action when _fix_attachment_links fires

Reported by: dllh Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.7
Component: Administration Keywords: has-patch
Focuses: Cc:


There are sometimes circumstances under which it would be useful to fire an action when _fix_attachment_links() fires, since it does a wp_update_post() right after another wp_update_post() has run. For example, say you want to run an action on save_post exactly once per post update. If a given post has attachments that need fixing, the save_post action will fire twice.

The addition of an action within _fix_attachment_links() would allow plugins to know that the call to wp_update_post() was a second call and to remove any actions that shouldn't fire a second time.

A real life scenario: My site hooks onto publish_post and initiates some async processes (email subscription notifications, for example). When a post needs its attachments updated, these processes execute twice, causing fun things like duplicate emails. So I need a way to make sure the events that fire on publish_post fire exactly once even if the update itself executes more than once. I thought adding an action might be the proper way forward. Patch attached.

A sample use case:

function skip_async_publish_post_if_fixing_attachment_links() {
    remove_action( 'publish_post', 'queue_publish_post', 10, 2 );
add_action( '_fix_attachment_links', 'skip_async_publish_post_if_fixing_attachment_links' );

Attachments (2)

fix-attachment-links.patch (403 bytes) - added by dllh 4 years ago.
fix-attachment-links.2.patch (461 bytes) - added by dllh 4 years ago.
A new approach, addition/subtraction of filters

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
4 years ago

So I need a way to make sure the events that fire on publish_post fire exactly once even if the update itself executes more than once.

You can just call remove_action() in your current function attached to publish_post. This use case doesn't require a new hook.

#2 @SergeyBiryukov
4 years ago

  • Version changed from trunk to 3.7

#3 @dllh
4 years ago

I tried this and you're right that it works well for one-off publishing of posts, but it'll fail for things like imports and batch updates. For that matter, I think my fix would fail in that case as well, so I'll need to think this through some more.

#4 @dllh
4 years ago

I've come up with another approach. It requires the addition and removal of a filter, which I suspect still won't fly, but nothing ventured nothing gained. It may be better to name the filter more generically (something like "doing_secondary_wp_update_post" so that it could be used more readily in other contexts as well.

Just want to restate that it's not my intention to modify core just to meet my use case. I can always hack my local core if I really need to (though would prefer not to). It just seems plausible that there might be other use cases for which something along these lines would be useful as well.

4 years ago

A new approach, addition/subtraction of filters

#5 @chriscct7
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

If you're worried about attachments in publish_post you can just check the post type of the post being passed in. An attachment is of the attachment post type.

Further, you can also use the did_action function.

This doesn't seem like something that more than a handful of people would use.

Closing as wontfix

Note: See TracTickets for help on using tickets.