Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field Validation and displaying error in wordpress custom post type

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..

like image 629
user1797635 Avatar asked Nov 04 '12 06:11

user1797635


People also ask

Why custom fields are not showing WordPress?

If your WordPress site is missing the custom fields option under the 'Screen Options' menu, then you need to check if you have the Advanced Custom Fields (ACF) plugin active on your site. ACF is a very popular WordPress plugin that developers use to create custom meta boxes. ACF is running on over 1 million websites.

How do I display custom fields in a WordPress post?

Add WordPress Custom Post Fields Manually and Edit Your Theme to Display Them. By default, the custom fields option is hidden in WordPress. To enable this feature, access your page or post editor. Locate the Screen Options button on the right corner of your screen, and check the Custom Fields box.

How show custom field in custom post type?

You simply need to add the code to your theme template. For example: $value = get_field( 'my_field' ); if($value): echo $value; endif; Just change my_field to the name of your custom field.


1 Answers

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!

like image 84
loQ Avatar answered Sep 28 '22 19:09

loQ