Field Validation and displaying error in wordpress custom post type

user1797635 picture user1797635 · Nov 4, 2012 · Viewed 7.9k times · Source

Hello guys i started my first plugin in wordpress after few work i got struck in field validation..

Problem is i have a field called "preix_author_url" then in my plugin i use

add_action('save_post', 'my_function_name');

i have created a validation class example

<?php
class validator {
    public static function isUrl($str = '') {
        if(strlen($str) == 0)
            return FALSE;

        return preg_match('!^http(s)?://[\w-]+\.[\w-]+(\S+)?$!i',$str);
    }
}

in "my_function_name()"

    function my_function_name(){
            global  $post;
            if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
            if(isset($_POST['post_type']) && $_POST['post_type'] == 'wallpapers'){
                require_once( WALLP_FILE_PATH . '/wallp-core/wallp-validator.php' );                
                $validate = new validator();
                if(isset($_POST['preix_author_url'])){
                    if($validate->isUrl($_POST['preix_author_url']))
                        update_post_meta($post->ID, 'preix_author_url', $_POST['preix_author_url']);
                }
            }
        }

Now i want to show error in post page if validate return false. But i didnt get the way to display those errors or notification..

Answer

loQ picture loQ · Nov 5, 2012

So after a little while i figured this out at last. I promise to come back to you with in 20 minutes but I failed because I thought it was that easy!! I found out after searching everywhere that admin_notices wont work on save_post hook! So heres a good solution with your problem.

    //for action hooks
add_action('save_post', 'my_function_name');
$validator = new Validator();
// called after the redirect
add_action('admin_head-post.php', array(&$validator, 'add_plugin_notice'));

//change your my_function_name with this
function my_function_name() {
    global $post;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
    if (isset($_POST['post_type']) && $_POST['post_type'] == 'wallpapers') {
        require_once( WALLP_FILE_PATH . '/wallp-core/wallp-validator.php' );
        $validate = new validator();
        if (isset($_POST['preix_author_url'])) {
            //if($validate->isUrl($_POST['preix_author_url']))
            //update_post_meta(
            //$post->ID, 'preix_author_url', $_POST['preix_author_url']);
            $validator = new Validator();
            if (!$validator->isUrl($_POST['preix_author_url'])) {
                $validator->update_option(1);
                return;
            } else {
                update_post_meta(
                        $post->ID, 
                        'preix_author_url', $_POST['preix_author_url']);
            }
        }
    }
}

//ive also revised your class
class Validator {
    //new isUrl validation
    //better use filter_var than preg_match
    function isUrl($str = '') {
        if (filter_var($str, FILTER_VALIDATE_URL) === FALSE) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    //You can change the error message here. 
    //This for your your admin_notices hook
    function show_error() {
        echo '<div class="error">
       <p>Error Found!!</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); 
        }
    }
}

I tried this in my personal website and it worked beautifully!! Ive also learned a lot of things with this!