【问题标题】:Field Validation and displaying error in wordpress custom post typewordpress 自定义帖子类型中的字段验证和显示错误
【发布时间】:2012-10-24 09:00:36
【问题描述】:

大家好,经过一些工作后,我在 wordpress 中开始了我的第一个插件,我在字段验证方面受到了打击。

问题是我有一个名为"preix_author_url" 的字段,然后在我的插件中使用

add_action('save_post', 'my_function_name');

我已经创建了一个验证类示例

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

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

"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']);
                }
            }
        }

现在,如果验证返回 false,我想在帖子页面中显示错误。但我没有办法显示这些错误或通知..

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    所以过了一会儿,我终于想通了。我保证会在 20 分钟内回复你,但我失败了,因为我认为这很容易!我在到处搜索后发现 admin_notices 在 save_post 钩子上不起作用!所以这是一个很好的解决你的问题的方法。

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

    我在我的个人网站上试过这个,效果很好!!我也从中学到了很多东西!

    【讨论】:

    • 我还有另一个问题..我现在还有另外 2 个字段如何显示这些错误?我们不能收集数组中的所有错误,然后将其转储到错误块中吗?
    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多