mardi 5 avril 2016

Validate Wordpress Custom Post Type fields

I'm developing a plugin, in which the administrator has the ability to add and remove ZIP codes on the backend. I found that the best way to do this is by creating a custom post type named zip_code with only a title being supported , as that functionality is already built-in to Wordpress.

What I'm having trouble with is validating the title, as it must be a valid ZIP code to avoid errors on the front end.

I've added the following action hooks:

    // Action hook to intercept Wordpress' default post saving function and redirect to ours
    add_action('save_post', 'zip_code_save');

    $validator = new Validator();
    // Called after the redirect
    add_action('admin_head-post.php', array($validator, 'add_plugin_notice'));

zip_code_save function:

public function zip_code_save() {

    global $post;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    if (isset($_POST['post_type']) && $_POST['post_type'] == 'zip_code') {

        $validator = new Validator();

        if (!$validator->validate(get_the_title($post->ID))) {

            $validator->update_option(1);
            return false;
        } else {
            update_post_meta(
                $post->ID,
                'zip_code', get_the_title($post->ID));
        }

    }
}

And finally this is my Validator class:

class Validator { 
    //This for your your admin_notices hook
    function show_error() {
        echo '<div class="error">
       <p>The ZIP Code entered is not valid. <b>Note</b>: only US ZIP codes are accepted.</p>
       </div>';
    }

    //update option when admin_notices is needed or not
    function update_option($val) {
        update_option('display_my_admin_message', $val);
    }

    //function to use for your admin notice
    function add_plugin_notice() {
        if (get_option('display_my_admin_message') == 1) {
            // check whether to display the message
            add_action('admin_notices', array(&$this, 'show_error'));
            // turn off the message
            update_option('display_my_admin_message', 0);
        }
    }

    function validate($input) {

        $zip = (isset($input) && !empty($input)) ? sanitize_text_field($input) : '';

        if ( !preg_match( '/(^\d{5}$)|(^\d{5}-\d{4}$)/', $zip  ) ) {
            return false;
        } else {
            return true;
        }

    }

}

The above code successfully outputs an error message if the entered ZIP is not valid, however regardless of the error, it does publish the post. Is there a way to block the publishing of the post if the title is not valid?

Also, is there a way to prevent WP of creating drafts automatically? Since there is so little data, it's really irrelevant here, and more a hassle.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire