Make WordPress Core

Opened 5 years ago

Last modified 2 months ago

#52518 new enhancement

sanitize_post_field filter for 'raw' context

Reported by: tkama's profile Tkama Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: reporter-feedback
Focuses: Cc:

Description

In some cases it can be useful to change values in raw context.

For example I need to change post_status value for replyto-comment ajax call, like so:

<?php
add_action( 'wp_ajax_replyto-comment', function() {

        add_filter( 'post_status', function(){
                return 'publish';
        } );

}, 0 );

But I can't do this because existing filters don't work for raw context.

I don't see any reasons to not add such filter for raw context too.

Code snippet to do this change:

<?php
        $prefixed = false;
        if ( false !== strpos( $field, 'post_' ) ) {
                $prefixed        = true;
                $field_no_prefix = str_replace( 'post_', '', $field );
        }

        if ( 'raw' === $context ) {

                if ( $prefixed ) {
                        /** This filter is documented in wp-includes/post.php */
                        $value = apply_filters( "{$field}", $value, $post_id, $context );
                } else {
                        $value = apply_filters( "post_{$field}", $value, $post_id, $context );
                }

                return $value;
        }

        if ( 'edit' === $context ) {
                $format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' );

Attachments (1)

wp-includes--posts.php.diff (1.2 KB) - added by Tkama 5 years ago.
/wp-includes/post.php

Download all attachments as: .zip

Change History (3)

@Tkama
5 years ago

/wp-includes/post.php

#1 @SergeyBiryukov
5 years ago

  • Component changed from General to Posts, Post Types

#2 in reply to: ↑ description @SirLouen
2 months ago

  • Keywords reporter-feedback added
  • Version 5.6.1 deleted

Replying to Tkama:

In some cases it can be useful to change values in raw context.

For how the code is structured, I cannot really figure out a nice case where those filters, which also are concurrent with this and this, could be of use

Can you put a good testing use case (check this), to showcase how you could be using this without being able of taking advantage of what we currently have available?

Note: See TracTickets for help on using tickets.