Javascript 如何下拉到元数据框并将元数据中的数据保存在db wp中?

Javascript 如何下拉到元数据框并将元数据中的数据保存在db wp中?,javascript,php,jquery,html,wordpress,Javascript,Php,Jquery,Html,Wordpress,我创建了一个自定义帖子类型的名称课程。对于此自定义帖子类型,创建一个动态下拉框元数据显示在下拉框中,但不保存在db中。请检查我在保存数据时出错的代码。我的元数据框显示良好,但我输入的数据未保存。也就是说,当我在帖子上单击“更新”时,元框将恢复为我的占位符文本 if ( ! function_exists('add_courses') ) { // Register Custom Post Type function add_courses() { $labels = a

我创建了一个自定义帖子类型的名称课程。对于此自定义帖子类型,创建一个动态下拉框元数据显示在下拉框中,但不保存在db中。请检查我在保存数据时出错的代码。我的元数据框显示良好,但我输入的数据未保存。也就是说,当我在帖子上单击“更新”时,元框将恢复为我的占位符文本

 if ( ! function_exists('add_courses') ) {
    // Register Custom Post Type
    function add_courses() {
    $labels = array(
    'name'                  => _x( 'Courses', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Course', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'Courses', 'text_domain' ),
    'name_admin_bar'        => __( 'Courses', 'text_domain' ),
    'archives'              => __( 'Course Archives', 'text_domain' ),
    'attributes'            => __( 'Course Attributes', 'text_domain' ),
    'parent_item_colon'     => __( 'Parent Course:', 'text_domain' ),
    'all_items'             => __( 'All Courses', 'text_domain' ),
    'add_new_item'          => __( 'Add New Course', 'text_domain' ),
    'add_new'               => __( 'Add New Course', 'text_domain' ),
    'new_item'              => __( 'New Course', 'text_domain' ),
    'edit_item'             => __( 'Edit Course', 'text_domain' ),
    'update_item'           => __( 'Update Course', 'text_domain' ),
    'view_item'             => __( 'View Course', 'text_domain' ),
    'view_items'            => __( 'View Courses', 'text_domain' ),
    'search_items'          => __( 'Search Course', 'text_domain' ),
    'not_found'             => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
    'featured_image'        => __( 'Featured Image', 'text_domain' ),
    'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
    'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
    'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
    'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
    'items_list'            => __( 'Courses list', 'text_domain' ),
    'items_list_navigation' => __( 'Courses list navigation', 'text_domain' ),
    'filter_items_list'     => __( 'Filter Courses list', 'text_domain' ),
    );
    $args = array(
    'label'                 => __( 'Course', 'text_domain' ),
    'description'           => __( 'Add Courses', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'thumbnail', 'comments', 'trackbacks', 
 'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
    'taxonomies'            => array('post_tag'),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
    'show_in_rest'          => true,
    'register_meta_box_cb' => 'wpt_add_faculty_metaboxes',
    );
    register_post_type( 'course_type', $args );
    // register taxonomy
    register_taxonomy('courses', 'course_type', array('hierarchical' => true, 'label' => 'Course Categories', 'query_var' => true, 'rewrite' => array( 'slug' => 'course-type' )));
    }
    add_action( 'init', 'add_courses', 0 );
    function wpt_add_faculty_metaboxes() {
    add_meta_box(
    'wpt_faculty_member',
    'Faculty',
    'wpt_faculty_member',
    'course_type',
    'side',
    'default'
    );
    }
    add_action( 'add_meta_boxes', 'wpt_add_faculty_metaboxes' );
    function wpt_faculty_member() {
    global $post;
    wp_nonce_field( basename( __FILE__ ), 'event_fields' );
    $faculty_roles = get_post_custom( $post->ID );
    $args = array(
    'role'    => 'faculty',
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
    );
    $faculties = get_users( $args );
    echo '
    <select name="faculty_role" id="faculty_role">
      <option value="">Select Faculty...
      </option>';
      foreach ( $faculties as $faculty ) :
      echo '
      <option value="' . $faculty->ID . '">' . $faculty->user_login . '
      </option>';
      endforeach;
      echo '
    </select>';
    }
    /**
    * Save the metabox data
    */
    add_action( 'save_post', 'wpt_save_events_meta', 1, 2 );
    function wpt_save_events_meta( $post_id, $post ) {
    // Return if the user doesn't have edit permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
    return $post_id;
    }
    // Verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times.
    if ( ! isset( $_POST['faculty_role'] ) || ! wp_verify_nonce( $_POST['event_fields'], basename(__FILE__) ) ) {
    return $post_id;
    }
    // Now that we're authenticated, time to save the data.
    // This sanitizes the data from the field and saves it into an array $events_meta.
    $events_meta['faculty_role'] = esc_textarea( $_POST['faculty_role'] ); 
    // Cycle through the $events_meta array.
    // Note, in this example we just have one item, but this is helpful if you have multiple.
    foreach ( $events_meta as $key => $value ) :
    // Don't store custom data twice
    if ( 'revision' === $post->post_type ) {
    return;
    }
    if ( get_post_meta( $post_id, $key, false ) ) {
    // If the custom field already has a value, update it.
    update_post_meta( $post_id, $key, $value );
    } else {
    // If the custom field doesn't have a value, add it.
    add_post_meta( $post_id, $key, $value);
    }
    if ( ! $value ) {
    // Delete the meta key if there's no value
    delete_post_meta( $post_id, $key );
    }
    endforeach;
    }
    }
如果(!function_存在('add_courses')){
//注册自定义邮件类型
函数add_courses(){
$labels=数组(
'name'=>\ux('Courses','Post Type General name','text\u domain'),
'singular_name'=>_x('Course','Post Type singular name','text_domain'),
“菜单名称”=>“(“课程”、“文本域”),
'name_admin_bar'=>\('Courses'、'text_domain'),
“档案”=>(课程档案”,“文本域”),
“属性”=>“(“课程属性”、“文本域”),
“父课程:”、“文本域”),
“所有项目”=>“(“所有课程”、“文本域”),
“添加新项目”=>(“添加新课程”、“文本域”),
'add_new'=>uuu('add new Course','text_domain'),
“新建项目”=>“(“新建课程”、“文本域”),
“编辑项目”=>“(“编辑课程”、“文本域”),
'update_item'=>'('update Course','text_domain'),
“查看项目”=>“(“查看课程”、“文本域”),
“查看项目”=>“(“查看课程”、“文本域”),
“搜索项目”=>“(“搜索课程”、“文本域”),
'not_found'=>'not found','text_domain'),
'not_found_in_trash'=>,
“特色图片”=>“(“特色图片”、“文本域”),
“设置特色图像”=>“(“设置特色图像”、“文本域”),
“删除特色图片”=>“(“删除特色图片”、“文本域”),
“使用特色图片”=>“(“用作特色图片”、“文本域”),
'插入项目'=>'('插入项目','文本域'),
'上载到\u此\u项目'=>'('上载到此项目','文本\u域'),
“项目列表”=>“(“课程列表”、“文本域”),
“项目列表导航”=>“(“课程列表导航”、“文本域”),
“筛选项目列表”=>(“筛选课程列表”、“文本域”),
);
$args=数组(
'label'=>\('Course','text\u domain'),
'description'=>\('Add Courses','text\u domain'),
“标签”=>$labels,
'支持'=>数组('title','editor','thumbnail','comments','trackbacks',
“修订”、“自定义字段”、“页面属性”、“发布格式”),
“分类法”=>array('post_标记'),
“分层”=>正确,
“public”=>正确,
'show_ui'=>true,
“在菜单中显示”=>true,
“菜单位置”=>5,
“在管理栏中显示”=>true,
“在导航菜单中显示”=>true,
“can_export”=>true,
“has_archive”=>true,
“从搜索中排除”=>false,
“公开可查询”=>正确,
“能力类型”=>“页面”,
“在休息中显示”=>正确,
“register_meta_box_cb”=>“wpt_add_faculty_Metabox”,
);
注册后类型('course\u type',$args);
//注册分类
注册分类法(“课程”、“课程类型”、“数组”(“分层”=>true、“标签”=>“课程类别”、“查询变量”=>true”、“重写”=>array”(“slug”=>“课程类型”));
}
添加_操作('init','add_courses',0);
函数wpt\u add\u faculty\u metaboxes(){
添加元框(
“wpt大学教员”,
“教员”,
“wpt大学教员”,
“课程类型”,
"一边",,
“默认”
);
}
添加操作(“添加元框”、“wpt添加元框”);
职能wpt_教员_成员(){
全球$员额;
wp_nonce_字段(basename(_文件);“event_字段”);
$faculty\u roles=get\u post\u custom($post->ID);
$args=数组(
“角色”=>“教员”,
'orderby'=>'user\u nicename',
“订单”=>“ASC”
);
$faculties=获取用户($args);
回声'
选择教员。。。
';
foreach(学院为$faculty):
回声'
“.$faculty->user\u login”
';
endforeach;
回声'
';
}
/**
*保存metabox数据
*/
添加动作('save_post','wpt_save_events_meta',1,2);
函数wpt\u save\u events\u meta($post\u id,$post){
//如果用户没有编辑权限,则返回。
如果(!当前用户\u可以('edit\u post',$post\u id)){
返回$post_id;
}
//确认此信息来自我们的屏幕并经过适当授权,
//因为save_post可以在其他时间触发。
如果(!isset($_POST['faculty_role'])| | |!wp_verify_nonce($_POST['event_fields']),basename(uuu文件))){
返回$post_id;
}
//现在我们已经通过了身份验证,是时候保存数据了。
//这将清理字段中的数据并将其保存到数组$events\u meta中。
$events_meta['faculty_role']=esc_textarea($u POST['faculty_role']);
//循环浏览$events\u元数组。
//注意,在本例中,我们只有一个项目,但如果您有多个项目,这将非常有用。
foreach($events\u meta作为$key=>$value):
//不要存储自定义数据两次
如果('revision'==$post->post\u类型){
返回;
}
if(get_post_meta($post_id,$key,false)){
//如果自定义字段已经有值,请更新它。
更新发布元数据($post\u id,$key,$value);
}否则{
//如果自定义字段没有值,请添加它。
添加post-meta($post-id,$key,$value);
}
如果(!$value){
//如果没有值,请删除元键
删除后元($post\u id,$key);
add_action( 'save_post_my_post_type', 'wpse63478_save' );
function wpse63478_save() {
    //save stuff
 }