WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 21 months ago

#34706 closed enhancement (fixed)

Introduce an edit_post_{$post->post_type} action

Reported by: garrett-eclipse Owned by: SergeyBiryukov
Milestone: 5.1 Priority: normal
Severity: normal Version: 4.4
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

Hello,

I was in the post.php file and found all the action hooks for when a post is saved. Noticed there's a new save_post_{$post->post_type} hook as of 3.7.0 and thought it would be nice to have similar done for edit_post so can tie into CPT directly with the filter.

Would look something like;

<?php
        /**
         * Fires once an existing post has been updated.
         *
         * The dynamic portion of the hook name, `$post->post_type`, refers to
         * the post type slug.
         *
         * @since 3.7.0
         *
         * @param int     $post_ID Post ID.
         * @param WP_Post $post    Post object.
         */
        do_action( "edit_post_{$post->post_type}", $post_ID, $post );

Attachments (4)

34706.diff (684 bytes) - added by Mte90 3 years ago.
added the filter
34706.2.diff (675 bytes) - added by Mte90 3 years ago.
without empy line
34706.3.diff (442 bytes) - added by garrett-eclipse 22 months ago.
34706.4.diff (444 bytes) - added by garrett-eclipse 22 months ago.

Download all attachments as: .zip

Change History (34)

#1 @DrewAPicture
3 years ago

  • Keywords needs-patch added

Hi @garrett-eclipse, welcome to Trac! Would you like to try submitting a patch for this?

@Mte90
3 years ago

added the filter

#2 @Mte90
3 years ago

  • Keywords dev-feedback added; needs-patch removed

Analyzing the code seems that exist already a edit_post filter and the save_post_{$post->post_type} has a parameter update that let to be executed only on update of the post.
So I am not sure if this feature is so required.
In any case enjoy the patch :-)

#3 @Mte90
3 years ago

Also the edit_post filter is also on comment.php file and another mention on post.php inside the wp_publish_post function but before do another patch I want to be sure that this ticket need to move on.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


3 years ago

#5 @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 5.0

#6 @danieltj
3 years ago

  • Keywords close added; dev-feedback removed

After reviewing the state of this function within post.php, I don't think there's a need for a new action here as the existing actions already provide enough to check what the post type is.

In all honesty though, if this was added, I wouldn't have a problem but if you want to trigger an action when a Book (example custom post type) is updated, you could always do something to the effect of:

add_action( 'edit_post', 'my_edit_action', 10, 2 );

function my_edit_action( $post_id, $post ) {

    if ( 'book' == $post->post_type ) {

        // do something now...

    }

}

Also, the save_post and save_post_{$post->post_type} hooks are called irrespective of whether it's a post being saved or updated so they should both be able to be used here without issues.

I'll recommend closing this, although feel free to reverse that.

#7 follow-up: @garrett-eclipse
3 years ago

Thanks @Mte90 for taking this on, sorry I didn't take a stab at it haven't had time outside of work since I had kids.

The patch looks great.

And I think the main benefit on this hook just like the save_post is to allow for cleaner code without the post_type conditional check.

An excerpt explaining from here;
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

NOTE: As of WP 3.7, an alternative action has been introduced, which is called for specific post types: save_post_{post_type}. >Hooking to this action you wouldn't have to check on the post type (ie: if ( $slug != $_POSTpost_type? ) in the sample above).

Hope that's helpful in determining if this should push forward

#8 in reply to: ↑ 7 @danieltj
3 years ago

Replying to garrett-eclipse:

Thanks @Mte90 for taking this on, sorry I didn't take a stab at it haven't had time outside of work since I had kids.

The patch looks great.

And I think the main benefit on this hook just like the save_post is to allow for cleaner code without the post_type conditional check.

An excerpt explaining from here;
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

NOTE: As of WP 3.7, an alternative action has been introduced, which is called for specific post types: save_post_{post_type}. >Hooking to this action you wouldn't have to check on the post type (ie: if ( $slug != $_POSTpost_type? ) in the sample above).

Hope that's helpful in determining if this should push forward

All I'd say then is remove the blank line break immediately after the if statement and this hook looks ready to go. My only concern was the number of hooks already available due to the post object being available.

Feel free to remove the close tag, the patch itself is fine.

#9 @garrett-eclipse
3 years ago

Thanks @danieltj I'll leave this in @Mte90's trusted hands

@Mte90
3 years ago

without empy line

#10 @Mte90
3 years ago

removed the empty line ;-)
Now need only a check by others :-D

#11 @garrett-eclipse
2 years ago

Thanks @Mte90 it's looking great. I think we just need a thumbs up from @danieltj to push this has-patch and needs-unit-tests

This ticket was mentioned in Slack in #core by garrett-eclipse. View the logs.


2 years ago

#13 @garrett-eclipse
2 years ago

  • Keywords has-patch 2nd-opinion needs-testing needs-unit-tests needs-docs needs-codex added; close removed

This ticket was mentioned in Slack in #core by garrett-eclipse. View the logs.


2 years ago

#15 @garrett-eclipse
2 years ago

  • Keywords 2nd-opinion removed
  • Milestone changed from 5.0 to 4.9.9

Updating Milestone to raise into 4.9.9
Removed '2nd-opinion' as it was received via Slack from @SergeyBiryukov
Permalink - https://wordpress.slack.com/archives/C02RQBWTW/p1532547221000150

Last edited 2 years ago by garrett-eclipse (previous) (diff)

#16 @SergeyBiryukov
2 years ago

Seems like a good idea for convenience, would be consistent with save_post_{$post->post_type} added in #16176.

#17 @SergeyBiryukov
2 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 43535:

Posts, Post Types: Introduce edit_post_{$post->post_type} hook.

The hook fires before the general edit_post hook and has the same parameters.

It also complements the save_post_{$post->post_type} hook added in [25050].

Props Mte90, garrett-eclipse.
Fixes #34706.

#18 @SergeyBiryukov
2 years ago

  • Keywords fixed-major added; needs-testing needs-docs needs-codex removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for 4.9.9 consideration.

#19 @SergeyBiryukov
2 years ago

In 43616:

Docs: Update @since version for edit_post_{$post->post_type} action introduced in [43535] to 4.9.9.

See #34706.

#20 @SergeyBiryukov
2 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 43617:

Posts, Post Types: Introduce edit_post_{$post->post_type} hook.

The hook fires before the general edit_post hook and has the same parameters.

It also complements the save_post_{$post->post_type} hook added in [25050].

Props Mte90, garrett-eclipse.
Merges [43535] and [43616] to the 4.9 branch.
Fixes #34706.

#21 @pento
2 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

[43617] needs to be reverted from the 4.9 branch, then this ticket can be moved to the 5.0.1 milestone.

trunk will need to be updated to reflect the correct version.

#22 @SergeyBiryukov
2 years ago

In 43707:

Posts, Post Types: Revert [43617] from the 4.9 branch.

This change is out of the 4.9.x scope, and will be reintroduced in 5.0.x.

See #34706.

#23 @SergeyBiryukov
2 years ago

  • Milestone changed from 4.9.9 to 5.0.1

#24 @pento
22 months ago

  • Milestone changed from 5.0.1 to 5.0.2

#25 @pento
22 months ago

  • Keywords needs-patch added; has-patch needs-unit-tests fixed-major removed
  • Milestone changed from 5.0.2 to 5.1

Moving this to 5.1, the docs need updating to reflect the correct version.

#26 @DrewAPicture
22 months ago

  • Keywords needs-docs added

#27 follow-up: @garrett-eclipse
22 months ago

  • Keywords has-patch added; needs-patch needs-docs removed

Version bumped to 5.1 in 34706.3.diff

@DrewAPicture I dropped the needs-docs tag, but if it related to something other than the version bump in docblock let me know. Thanks

#28 in reply to: ↑ 27 ; follow-up: @DrewAPicture
22 months ago

Replying to garrett-eclipse:

Version bumped to 5.1 in 34706.3.diff

@DrewAPicture I dropped the needs-docs tag, but if it related to something other than the version bump in docblock let me know. Thanks

Nope, that was enough. The purpose of the keyword is to signal docs contributions, and that's been accomplished.

Side note: We use 3-digit x.x.x style versions in core DocBlocks, so 5.1 should be changed to 5.1.0 ;-)

#29 in reply to: ↑ 28 @garrett-eclipse
22 months ago

Replying to DrewAPicture:

Side note: We use 3-digit x.x.x style versions in core DocBlocks, so 5.1 should be changed to 5.1.0 ;-)

Thanks @DrewAPicture I've addressed in 34706.4.diff

#30 @SergeyBiryukov
21 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 44347:

Docs: Update @since tag for edit_post_{$post->post_type} hook introduced in [43535].

Props garrett-eclipse.
Fixes #34706.

Note: See TracTickets for help on using tickets.