Új hozzászólás Aktív témák

  • trisztan94

    őstag

    Újabb WP plugin fejlesztős kérdésem lenne. Végül rájöttem, hogy a legjobb egy custom post type készítése csak egy title beírási capability-vel, mert az már alapból tudja a hozzáadás és törlést.

    Ezeket a hookokat adtam hozzá:

    // 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 fügvény:

    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));
    }

    }
    }

    Validator osztály:

    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;
    }

    }

    }

    Ez azt csinálja, hogy ha helytelen a beírt kód, akkor kiírja szépen a hibaüzenetet, azonban attól függetlenül ugyanúgy publikálja. Tehát a probléma az, hogy validációtól függetlenül publikálja a postot.

    Valakinek ötlet?
    Köszi! :R

Új hozzászólás Aktív témák