[wp-trac] [WordPress Trac] #43559: wp_ajax_add_meta() does not allow empty values

WordPress Trac noreply at wordpress.org
Thu Mar 15 13:55:46 UTC 2018


#43559: wp_ajax_add_meta() does not allow empty values
-------------------------------+-----------------------------
 Reporter:  charlestonsw       |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  trunk
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 Users should be allowed to set post meta keys to an empty string.

 With custom post types there are many ways to use the custom meta data.
 In one of my use cases I use the meta values to drive a remote
 application.    The REST API sends back a JSON response.   If a key is
 present in the response it sets the corresponding value in the remote app.

 Having the ability to send back a key with an EMPTY STRING value is
 critical for writing simple reactive applications (Vue , React, etc.) that
 call upon the WordPress REST API without having to craft a completely
 custom solution.

 I am trying to use a standard WP REST endpoint to fetch a custom post
 type.  Without the ability to set a custom post type "custom field" to an
 empty string I am going to have to write a custom post type so a user can
 enter something crazy like   into that field , catch that, and then
 convert it to '' before sending to the remote app.     That is a bad hack
 -- when doing HTML/CSS values back to the remote app a user might actually
 WANT   in the output.

 Why are '' values being rejected in WP Posts custom meta in the first
 place?

 IMO this should be removed from \wp_ajax_add_meta() in wp-admin/includes
 /ajax-actions.php


 {{{
                 if ( '' == trim($key) )
                         wp_die( __( 'Please provide a custom field name.'
 ) );
 }}}


 Or at least add a filter/hook so this default behavior can be mitigated:

 {{{
                 if ( apply_filters( 'wp_reject_meta_values' , '' , $pid )
 == trim($key) )
                         wp_die( __( 'Please provide a custom field name.'
 ) );
 }}}

 Even better - allow users to provide an array of values to reject:

 {{{
                 $rejected_values = (array) apply_filters(
 'wp_reject_meta_values' , '' , $pid );
                 if ( in_array( trim( $key ) , $rejected_values )  )
                         wp_die( __( 'Please provide a custom field name.'
 ) );
 }}}

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


More information about the wp-trac mailing list