Make WordPress Core

Opened 8 months ago

Closed 6 months ago

Last modified 4 days ago

#63115 closed enhancement (fixed)

wp_insert_post missing do_action for pre_post_insert

Reported by: hrohh's profile Hrohh Owned by: audrasjb's profile audrasjb
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch commit
Focuses: Cc:

Description

Hi, it is possible add new action
https://github.com/WordPress/wordpress-develop/blob/6.7/src/wp-includes/post.php#L4382-L5078
to line 4792

<?php do_action( 'pre_post_insert', $data ); 

like we have in update

<?php do_action( 'pre_post_update', $post_id, $data ); 

It would be usefull for detection duplicate content in database for example. We can rewire our post_id.

Change History (10)

This ticket was mentioned in PR #8527 on WordPress/wordpress-develop by @sainathpoojary.


8 months ago
#1

  • Keywords has-patch added

This PR introduces the pre_post_insert hook, allowing developers to modify or inspect post data before insertion, similar to pre_post_update. This enhances flexibility for use cases like duplicate content detection.

Trac ticket: #63115

#2 @peterwilsoncc
8 months ago

  • Milestone changed from Awaiting Review to 6.9
  • Type changed from feature request to enhancement
  • Version 6.7 deleted

I can see how this would be useful for consistency with the actions for updating a post.

As it's an enhancement, it won't be possible to get in to WordPress 6.8 so I've added it to the milestone for WordPress 6.9.

#3 @audrasjb
6 months ago

  • Keywords commit added
  • Owner set to audrasjb
  • Status changed from new to accepted

This looks good to me. Self assigning for commit.

@audrasjb commented on PR #8527:


6 months ago
#4

I added a commit to update the since declaration.
Looks good to go.

#5 @audrasjb
6 months ago

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

In 60293:

Posts, Post Types: Add pre_post_insert action hook before inserting a new post.

This changeset adds a pre_post_insert action hook before inserting a new post via wp_insert_post(), allowing developers to modify or inspect post data before insertion. It brings consistency with the pre_post_update action hook triggered when a post is updated.

Props Hrohh, peterwilsoncc, audrasjb.
Fixes #63115.

#6 @audrasjb
6 months ago

I forgot to credit @sainathpoojary for his patch. Done manually.

#7 @westonruter
6 months ago

@audrasjb Since it is an action, how would this allow for developers to modify the data prior to insertion?

For modification wouldn't the the wp_insert_post_data filter be used for this? It gets applied for inserts as well as updates.

In any case, having an action to compliment pre_post_update seems to make sense.

#8 @desrosj
4 months ago

#15230 was marked as a duplicate.

#9 @nikunj8866
5 weeks ago

I agree with @westonruter, the wp_insert_post_data filter is indeed the right place for developers to modify data before insertion or update.

I also think having a pre_post_insert action would be valuable. It could serve as a companion to pre_post_update, allowing developers to run additional logic before a new post is inserted.

For example, one practical use case would be enforcing a unique post title cross the site for a specific post type. The pre_post_insert action would make this type of validation straightforward to implement.

#10 @desrosj
4 days ago

The miscellaneous developer-focused changes developer note mentioned that the new pre_insert_post hook was introduced in [60293]: https://make.wordpress.org/core/2025/11/17/miscellaneous-developer-focused-changes-in-6-9/.

Last edited 4 days ago by desrosj (previous) (diff)
Note: See TracTickets for help on using tickets.