[wp-trac] [WordPress Trac] #43019: Hook to validate post form data before save

WordPress Trac noreply at wordpress.org
Thu Dec 17 18:34:20 UTC 2020


#43019: Hook to validate post form data before save
-------------------------------------+------------------------------
 Reporter:  henry.wright             |       Owner:  (none)
     Type:  enhancement              |      Status:  new
 Priority:  normal                   |   Milestone:  Awaiting Review
Component:  Database                 |     Version:  5.6
 Severity:  major                    |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:
-------------------------------------+------------------------------
Changes (by rafdizzle86):

 * version:   => 5.6
 * component:  General => Database
 * severity:  normal => major


Comment:

 I've found a "work-around" for updates. You can hook into the filter
 `wp_insert_post_data` and return a `null` or some other falsey value that
 is not an array. This is because a few lines down there's a call to
 `$wpdb->update( $wpdb->posts, $data, $where )` which does a validation on
 `$data` in the first few lines:

 {{{
 if ( ! is_array( $data ) || ! is_array( $where ) ) {
         return false;
 }
 }}}

 In the same hook you can update an option with a notification as to why
 you failed the update - in the following request you can display that
 notification using the `admin_notices` hook.

 I haven't looked that much into `$wpdb->insert()` - however it seems more
 complicated as it calls a `_insert_replace_helper` method that does some
 SQL data validation.

 Hopefully this helps other folks who are wondering why WordPress and
 WooCommerce don't have a simple hook for data validation and halting the
 save. Seems like a rudimentary hook that should be part of any database
 save process!

 WooCommerce devs: please include an explicit filter that allows 3rd party
 developers to validate data!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43019#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list