WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 7 months ago

#52518 new enhancement

sanitize_post_field filter for 'raw' context

Reported by: Tkama Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.6.1
Component: Posts, Post Types Keywords:
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 7 months ago.
/wp-includes/post.php

Download all attachments as: .zip

Change History (2)

@Tkama
7 months ago

/wp-includes/post.php

#1 @SergeyBiryukov
7 months ago

  • Component changed from General to Posts, Post Types
Note: See TracTickets for help on using tickets.