WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#24631 closed defect (bug) (invalid)

transition_post_status activated also when there's no transition?

Reported by: hooknose Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.8
Component: General Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

I'm not sure where I should post this because I don't know if it's a bug or a feature.

It's about the action hook transition_post_status:

http://codex.wordpress.org/Post_Status_Transitions

According to the above page, "A generic transition_post_status action is also emitted for every status change." That description implies that this action hook is only activated when there's a post status change, but when I experimented with it I've discovered it's activated also when there is no status change, when $new_status is the same as $old_status. Looking at the code, the function wp_transition_post_status() (in wp-includes/post.php) is activating it and is called from wp_insert_post() without conditioning it on a post status change.

My question is, is that a bug or is that how it's supposed to be?

If that's how it's suppose to be the description in the documentation should change to reflect what this action hook really does, which is just to offer the *possibility* to detect a status transition, because otherwise people think it's enough to check just one of the statuses to detect a specific change - for instance, if the old status is 'publish' the new status can't be 'publish', so the post is 'unpublished', while in reality both the old and new status can be 'publish' - this can cause a critical mistake. See here, for instance:

http://blog.joshschumacher.com/2011/10/11/wordpress-transition_post_status-action/

Personally, I don't think the code itself should change to be activated only for post status transitions. It offers more flexibility and options the way it is, and if one needs to use it only for status transitions they can simply add one line of code to the top of their callback function:

if ($new_status == $old_status) return;

But I'd like an authoritative answer from someone in the development team to the question if it's a bug because my code currently relies on it to do something like that:

if ($new_status == $old_status) { update X }
elseif ($new_status = 'publish') { update X & move X to 'show' }
elseif ($old_status = 'publish') { update X & move X to 'hide' }

So if it's a bug and it's fixed in the future to be activated only for post status transitions my website will break. I don't want to rely on a bug, I want it to be a feature.

Change History (5)

#1 @SergeyBiryukov
8 years ago

  • Description modified (diff)
  • Summary changed from transition_post_status activated also when there's no tarnsition? to transition_post_status activated also when there's no transition?
  • Version changed from 3.5.2 to 2.8

#2 @SergeyBiryukov
8 years ago

My question is, is that a bug or is that how it's supposed to be?

It's a feature, implemented in #8792.

#3 @SergeyBiryukov
8 years ago

  • Keywords dev-feedback removed

#4 follow-up: @hooknose
8 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Great! Thanks for your reply :)

I wonder if there's a possibility to change the description in the codex because it's bound to cause misunderstandings. The description should make clear it's activated also when there's no status transition so people won't think it's enough to check just one of the statuses.

#5 in reply to: ↑ 4 @SergeyBiryukov
8 years ago

  • Milestone Awaiting Review deleted

Replying to hooknose:

The description should make clear it's activated also when there's no status transition so people won't think it's enough to check just one of the statuses.

The Codex is a wiki, feel free to make that change.

Note: See TracTickets for help on using tickets.