无法完成元盒保存功能和清理/验证

时间:2017-07-30 作者:The WP Intermediate

在谈到这一点之前,我咨询过多次→

  1. Validation Function
  2. On StackOverFlow
在此处查阅了其他现有文章:

1个WS1

2WS2

最后,

我想出了我的版本→

    function cpmb_add_admin_styles() {
    wp_enqueue_style( \'cpmb-admin\', plugins_url( \'b-cpt/css/admin.css\' ) );
}
add_action( \'admin_enqueue_scripts\', \'cpmb_add_admin_styles\' );



function cpmb_add_admin_scripts() {

     /*Get the current screen*/
    $screen = get_current_screen();

    // Compare the current screen\'s ID with \'post\'. If they are equal, enqueue the JavaScript
    if( \'post\' == $screen->id ) {
        wp_enqueue_script( \'cpmb-admin\', plugins_url( \'b-cpt/js/admin.js\' ) );
    }

}
add_action( \'admin_enqueue_scripts\', \'cpmb_add_admin_scripts\' );




function cpmb_add_meta_box() {
    add_meta_box(
        \'cpmb_video\',             // The ID for the meta box
        \'Add Video URL\',          // The title of the meta box
        \'cpmb_display_meta_box\',  // The function for rendering the markup
        \'post\',                   // We\'ll only be displaying this on post pages
        \'advanced\',               // Where the meta box should appear
        \'high\'                    // The priority of where the meta box should be displayed
    );
}
add_action( \'add_meta_boxes\', \'cpmb_add_meta_box\' );




function cpmb_display_meta_box( $post ) {

    // Define the nonce for security purposes
    wp_nonce_field( plugin_basename( __FILE__ ), \'cpmb-nonce-field\' );

    // Start the HTML string so that all other strings can be concatenated
    $html = \'\';


    // Display the \'MP3 File\' label and its file input element
    $html .= \'<label id="video-url" for="video-url">\';
        $html .= \'Video URL\';
    $html .= \'</label>\';
    $html .= \'<input type="URL" id="video-url" name="video-url" value="" />\';



    $video_type = get_post_meta($post->ID,\'my_video_type\',true);
    $video_id = get_post_meta($post->ID,\'my_meta_box_text\',true);


    $html . = \'<p>\'
    $html . = \'<label for="my_meta_box_text">Select video type:</label>\'
         <!-- added select for selecting Vedio type -->
    $html . = \'<select name="my_video_type" id="my_video_type">  \'
    $html . = \'<option <?php echo ($video_type == \'youtube\') ? "selected=\'selected\'" : "" ;?> value="youtube">Youtube</option>\'
    $html . = \'<option <?php echo ($video_type == \'vimeo\') ? "selected=\'selected\'" : "" ;?> value="vimeo">Vimeo</option>\'
    $html . = \'</select>\'
        <!-- added select for selecting Vedio type -->
    $html . = </p>

    $html . = \'<p>\'
        $html . = \'<label for="my_meta_box_text">Youtube/Vimeo ID:</label>\'
        $html . = \'<input type="text" name="video-url" id="video-url" value="<?php echo $video_id; ?>" />\'
    $html . = \'</p>\'
    echo $html;



}




function cpmb_save_meta_box_data( $post_id ) {

    // If the user has permission to save data...
    if ( cpmb_user_can_save( $post_id, \'cpmb-nonce-field\' ) ) {

        // ...and if the MP3 file is setup, then check to make sure its valid and save it, as well
        if ( isset( $_POST[\'video-url\'] ) && ! empty( $_POST[\'video-url\'] ) ) {

            if ( url_allowed( $_POST[\'video-url\'] ) ) {

                }

        }

    }

}
add_action( \'save_post\', \'cpmb_save_meta_box_data\' );

function url_allowed( $url ) {
        $allowed_hosts = array(
                        \'youtube.com\',
                        \'vimeo.com\'
                        );
        if ( in_array( parse_url( $url, PHP_URL_HOST ), $allowed_hosts ) ) {
            return true;
        }
        return false;
    }

function cpmb_user_can_save( $post_id, $nonce ) {

    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], plugin_basename( __FILE__ ) ) );
    return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;

}
但save函数中有些地方不完整:

if ( url_allowed( $_POST[\'video-url\'] ) ) {

                }

        }
请帮我完成这个请帮助我消除这段代码中的任何错误。

1 个回复
最合适的回答,由SO网友:hwl 整理而成

验证后,只需使用表单输入更新post meta:

 if ( url_allowed( $_POST[\'video-url\'] ) ) {
     update_post_meta( $post_id, \'video-url\', esc_url( $_POST[\'video-url\'] ) );
 } else {
     //if user edits entry to remove a url from input, it will be deleted
     delete_post_meta( $post_id, \'video-url\' );
 }
如果你看看esc_url(), 您可以看到它正在使用wp_allowed_protocolswp_kses_normalize_entities().

<小时>

Side Note:

它还使用wp_parse_url(), php的包装器parse_url(), 检查不同部件。与我们在url_allowed() 函数(实际上我将更新that answer 使用wp_parse_url() 如果您愿意)。

结束

相关推荐

更新页面(update-core.php)和插件页面(plugins.php)恢复到主页

我在Wordpress网站的管理视图中收到通知,我有一个网站和插件的可用更新(在我的网络管理仪表板中,在“插件”和“更新”旁边的红色圆圈中有“1”)。。。但当我尝试同时转到“更新”页和;插件页面,是否显示主页?此时的URL为http:///wp-admin/network/update-core.php/和http:///wp-admin/plugins.php/分别地因此,我永远无法到达真正的更新页面,也无法更新我的Wordpress或插件。如何显示更新或插件页面?