Opened 9 years ago
Closed 6 years 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)
Change History (34)
#2
@
7 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
@
7 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.
7 years ago
#6
@
7 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:
↓ 8
@
7 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
@
7 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.
#11
@
6 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.
6 years ago
#13
@
6 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.
6 years ago
#15
@
6 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
#16
@
6 years ago
Seems like a good idea for convenience, would be consistent with save_post_{$post->post_type}
added in #16176.
#17
@
6 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 43535:
#18
@
6 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.
#21
@
6 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.
#25
@
6 years 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.
#27
follow-up:
↓ 28
@
6 years 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:
↓ 29
@
6 years 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
@
6 years 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
Hi @garrett-eclipse, welcome to Trac! Would you like to try submitting a patch for this?