WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 4 months ago

#34706 new enhancement

Introduce an edit_post_{$post->post_type} action

Reported by: garrett-eclipse Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.4
Component: Posts, Post Types Keywords: close
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 (2)

34706.diff (684 bytes) - added by Mte90 4 months ago.
added the filter
34706.2.diff (675 bytes) - added by Mte90 4 months ago.
without empy line

Download all attachments as: .zip

Change History (12)

#1 @DrewAPicture
12 months ago

  • Keywords needs-patch added

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

@Mte90
4 months ago

added the filter

#2 @Mte90
4 months 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
4 months 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.


4 months ago

#5 @SergeyBiryukov
4 months ago

  • Milestone changed from Awaiting Review to 5.0

#6 @danieltj
4 months 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
4 months 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
4 months 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
4 months ago

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

@Mte90
4 months ago

without empy line

#10 @Mte90
4 months ago

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

Note: See TracTickets for help on using tickets.