metabox - How to make a meta box field a requirement

I'm building a plugin that has a meta box. Some of the fields in the meta box are required. Is using jQuery the onl

I'm building a plugin that has a meta box. Some of the fields in the meta box are required. Is using jQuery the only method for achieving this? Can I require that a field is filled in using PHP?

I'm building a plugin that has a meta box. Some of the fields in the meta box are required. Is using jQuery the only method for achieving this? Can I require that a field is filled in using PHP?

Share Improve this question edited Nov 18, 2019 at 11:22 Henders 1051 silver badge6 bronze badges asked Apr 22, 2013 at 17:18 user28566user28566 1
  • 1 You can either fill it with a default value, or check if the field is empty, maybe provide the user a notice on that, and don't allow for/apply the default functionality. But maybe you could get a more detailed and useful answer if you provided more information...? – tfrommen Commented Apr 22, 2013 at 17:23
Add a comment  | 

3 Answers 3

Reset to default 4

You can use Javascript to create a first-line convenience warning, but that is not a secure solution. You will need to interrupt the post save to truly create a required field.

function req_meta_wpse_96762($data){
  $allow_pending = false;
  if (isset($_POST['meta'])) {
    foreach ($_POST['meta'] as $v) {
      if ('your_required_key' === $v['key'] && !empty($v['value'])) {
        $allow_pending = true;
      }
    }
  }
  if (false === $allow_pending) {
    $data['post_status'] = 'draft';
  }
  return $data;
}
add_action('wp_insert_post_data','req_meta_wpse_96762');

That will also reset the post to 'Draft' if the meta field is deleted.

There is no way to require input per PHP. Only the browser can do that, and the browser gets the output after PHP is done.

You can add the attribute required:

<input name=foo required>

But not all browsers support that, and you have to check the value still in your validation handler.

I know this is an old question but it's first result on Google... Here my two cents:

Remember add all javascript fields validations before.

Thanks to @s_ha_dum answer, this is my custom meta box validation method:

        public static function validateMetaBoxesFields($postData, $postArray) {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $postData;
        }
        if (array_key_exists('post_type', $postData) && $postData['post_type'] === 'MY_POST_TYPE') {
            if (array_key_exists('post_status', $postData) && $postData['post_status'] === 'publish') {
                $valid = true;
                // Check meta box fields (in $postArray) if something not valid, set $valid to false;
                if (!$valid) {
                    $postData['post_status'] = 'draft';
                    add_filter('redirect_post_location', array (self::class, 'alterRedirectMessageOnEventSave'));
                }
            }
        }
        return $postData;
    }

Add filter:

add_filter('wp_insert_post_data', array (EventsPostTypeService::class, 'validateMetaBoxesFields'), 99, 2);

Due WordPress is showning post saved message, I need to filter redirect post location query. This is the method:

        public static function alterRedirectMessageOnEventSave($location) {
        $location = remove_query_arg('message', $location);
        // 550 or other number, @see wp-admin/edit-form-advanced.php L:146 - 183
        $location = add_query_arg('message', 550, $location);
        return $location;
    }

And finally we add our custom message:

        public static function addCustomPostUpdateMessages($messages) {
        $messages['post'][550] = __('Post Type cannot be created due empty required fields: %s', 'my_text_domain');
        return $messages;
    }

%s can be replaced with a transient data, or other way you want to save not valid meta box fields.

Remember to add filter:

add_filter('post_updated_messages', array (EventsPostTypeService::class, 'addCustomPostUpdateMessages'), 10, 1);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744994868a4605129.html

相关推荐

  • metabox - How to make a meta box field a requirement

    I'm building a plugin that has a meta box. Some of the fields in the meta box are required. Is using jQuery the onl

    17小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信