[wp-trac] [WordPress Trac] #52518: sanitize_post_field filter for 'raw' context

WordPress Trac noreply at wordpress.org
Sat Feb 13 16:08:09 UTC 2021


#52518: sanitize_post_field filter for 'raw' context
-------------------------+-----------------------------
 Reporter:  Tkama        |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  5.6.1
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 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
 <?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
 <?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' );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52518>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list