Make WordPress Core

Opened 3 months ago

Last modified 4 days ago

#60038 new enhancement

Pass original array of arguments to hooks that fire within `wp_insert_post()`

Reported by: johnbillion's profile johnbillion Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: good-first-bug has-patch
Focuses: Cc:

Description

The $postarr value passed to the wp_insert_post() function contains context that can be useful to callbacks attached to the actions that fire within this function, but this value isn't passed to any of them. Strangely enough it is passed to most of the filters there.

As an example, a plugin of mine needs to perform logic within a callback on the wp_insert_post action based on a mixture of the tax_input argument and a custom argument. The wp_insert_post_data filter needs to be used to access these values.

The $postarr value gets copied to $unsanitized_postarr. This is the value that should be passed to the actions because it contains the original input array, rather than $postarr which includes defaults and sanitised values. Or both should be passed, as per the filters in the function.

This affects the following actions:

  • pre_post_update
  • edit_attachment
  • attachment_updated
  • add_attachment
  • edit_post_{$post->post_type}
  • edit_post
  • post_updated
  • save_post_{$post->post_type}
  • save_post
  • wp_insert_post

And the following filters:

  • add_trashed_suffix_to_trashed_posts

Attachments (4)

60038.diff (5.9 KB) - added by rizwan_47 3 months ago.
Added $postarr arg to the missing hooks and filter
60038.1.diff (17.6 KB) - added by sukhendu2002 4 weeks ago.
60038.2.diff (10.0 KB) - added by sukhendu2002 4 weeks ago.
Patch refreshed
60038.2.2.diff (4.9 KB) - added by marukou1976 4 days ago.
convert to utf-8

Download all attachments as: .zip

Change History (8)

@rizwan_47
3 months ago

Added $postarr arg to the missing hooks and filter

This ticket was mentioned in PR #5747 on WordPress/wordpress-develop by mrhatia.


3 months ago
#1

  • Keywords has-patch added; needs-patch removed

There are some missing arguments in the core i added it and resolve following ticket

Trac ticket: https://core.trac.wordpress.org/ticket/60038

#2 @johnbillion
5 weeks ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release

Thanks for the patch @rizwan_47! But, it doesn't do what I mentioned in the description. The $unsanitized_postarr array needs to be passed, either on its own or along with $postarr. Would you like to take another look?

@sukhendu2002
4 weeks ago

@sukhendu2002
4 weeks ago

Patch refreshed

#3 @orufovyjosoq1976
2 weeks ago

To address the issue where the original array of arguments is not passed to hooks that fire within wp_insert_post(), you can modify the core WordPress functionality to ensure that the original $postarr array is passed to these hooks.

Here's how you can do it:

  1. Identify Affected Hooks: Review the actions and filters listed where the original $postarr array should be passed.
  1. Modify Core WordPress Functionality: You'll need to locate the wp_insert_post() function in WordPress core (usually found in wp-includes/post.php). Within this function, find the hooks mentioned and modify them to pass the original $postarr array or both $postarr and $unsanitized_postarr.
  1. Update Documentation: Make sure to update the documentation comments for these hooks to reflect the changes made, explaining that the original $postarr array is now passed.

Here's an example of how you might modify one of the hooks (wp_insert_post):

`php
/

  • Inserts or updates a post. *
  • @since 2.5.0 *
  • @param int|WP_Error $post_id The post ID on success. WP_Error on failure.
  • @param WP_Post $post WP_Post object for the post.
  • @param bool $update Whether this is an existing post being updated.
  • @param array $postarr Array of sanitized (and unsanitized) post data. */

do_action( 'wp_insert_post', $post_id, $post, $update, $postarr );
`

Modify it to include $postarr:

`php
do_action( 'wp_insert_post', $post_id, $post, $update, $postarr, $unsanitized_postarr );
`

By making these modifications, you ensure that hooks within wp_insert_post() have access to the original $postarr array, providing valuable context for plugin or theme developers to perform custom logic based on the original input data.

@marukou1976
4 days ago

convert to utf-8

#4 @marukou1976
4 days ago

  • Keywords has-patch added; needs-patch removed

I'm re-uploading 60038.2.2diff as it was corrupted. Specifically, it was in UTF-16LE, so I have converted it to UTF-8. I have not made any changes to the source code.

The content itself includes $unsanitized_postarr as an argument, which I believe meets the requirements. I'm also developing a plugin, and I find this to be more convenient.

I've changed the status to has-patch.

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