自定义字段是否为自定义分类?

时间:2013-02-28 作者:Acornrevolution

我有一个自定义的分类法,“国家”。我使用WP用户前端让用户张贴到网站上。大多数数据保存为自定义字段,因此我有一个自定义字段“cf\\u country”。我希望能够将该自定义字段设置为现有和所有未来帖子的分类法。我找到了一个转换器插件,但它是一次性的。我希望这一切自动发生。有没有办法做到这一点?

谢谢

Possible Solution `函数save\\u taxonomy\\u data($post\\u id){

// verify this came from our screen and with proper authorization.
if ( ! wp_verify_nonce( $_POST[\'_wpnonce\'], \'wpuf-add-post\' ) ) 
    return;

// OK, we\'re authenticated: we need to find and save the data
if ( isset( $_POST[\'cf_country\'] ) ) { 
    wp_set_object_terms( $post_id, $_POST[\'cf_country\'], \'country\' );
}
}
add_action(\'wpuf_add_post_after_insert\', \'save_taxonomy_data\');
function edit_taxonomy_data( $post_id ) {

// verify this came from our screen and with proper authorization.
if ( ! wp_verify_nonce( $_POST[\'_wpnonce\'], \'wpuf-edit-post\' ) ) 
    return;

// OK, we\'re authenticated: we need to find and save the data
if ( isset( $_POST[\'cf_country\'] ) ) { 
    wp_set_object_terms( $post_id, $_POST[\'cf_country\'], \'country\' );
}
}
add_action(\'save_post\', \'edit_taxonomy_data\');`

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

由于不知道您的输入表单是什么样子,我只能给您以下建议。基本上,您需要验证您是否想要/拥有保存数据的权限,然后接受元的输入,并通过wp_set_object_terms(). 您将希望在每次保存帖子时激发此。。。ie在save_post

function save_taxonomy_data($post_id) {

    // verify this came from our screen and with proper authorization.
    // your form should have a nonce <?php wp_nonce_field(\'taxonomy_country\',\'taxonomy_noncename\'); ?>
    if ( !wp_verify_nonce( $_POST[\'taxonomy_noncename\'], \'taxonomy_country\' )) {
        return $post_id;
    }

    // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
        return $post_id;


    // Check permissions
    if ( \'page\' == $_POST[\'post_type\'] ) {
        if ( !current_user_can( \'edit_page\', $post_id ) )
            return $post_id;
    } else {
        if ( !current_user_can( \'edit_post\', $post_id ) )
        return $post_id;
    }

    // OK, we\'re authenticated: we need to find and save the data
    // right now it is only working on posts and pages, need to adapt to other types here
    $post = get_post($post_id);
    if ( ($post->post_type == \'post\') || ($post->post_type == \'page\') ) { 

        isset( $_POST[\'post_country\'] )
            wp_set_object_terms( $post_id, $_POST[\'post_country\'], \'country\' );

    }

}
add_action(\'save_post\', \'save_taxonomy_data\');
改编自Custom Taxonomy Panels 也类似于Unable to save custom taxonomy terms in a custom-built metabox.

Second Attempt

第一次尝试主要是如何从元数据库保存分类数据。我浏览了一下WP用户前端代码,并做了一些调整。这是为了替换上面的内容,并假设cf\\u country是输入的name元素。

function save_taxonomy_data( $post_id ) {

    // verify this came from our screen and with proper authorization.
    if ( ! wp_verify_nonce( $_POST[\'_wpnonce\'], \'wpuf-add-post\' ) ) 
        return;

    // OK, we\'re authenticated: we need to find and save the data
    if ( isset( $_POST[\'cf_country\'] ) ) { 
        wp_set_object_terms( $post_id, $_POST[\'cf_country\'], \'country\' );
    }

}
add_action(\'wpuf_add_post_after_insert\', \'save_taxonomy_data\');

Third Attempt:

function save_taxonomy_data($post_id) {

    /* 
     * verify this came from our screen and with proper authorization.
    * your form should have a nonce <?php wp_nonce_field(\'wpuf-add-post\',\'_wpnonce\'); ?>
    */
    if ( !wp_verify_nonce( $_POST[\'_wpnonce\'], \'wpuf-add-post\' )) {
        return $post_id;
    }

    // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
        return $post_id;


    // Check permissions
    if ( \'page\' == $_POST[\'wpuf_post_type\'] ) { // post type is a hidden field in wpuf_
        if ( !current_user_can( \'edit_page\', $post_id ) )
            return $post_id;
    } else {
        if ( !current_user_can( \'edit_post\', $post_id ) )
        return $post_id;
    }

    // Work on all post types
    if ( isset( $_POST[\'post_country\'] ) ) 
        wp_set_object_terms( $post_id, $_POST[\'post_country\'], \'country\' );


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

结束

相关推荐

Admin sidebar customization

我的新客户wordpress站点在管理侧栏中没有插件、外观或任何其他默认项。谁能告诉我这些是怎么出现的吗。该站点正在主站点的子目录中运行。它有自己的wordpress安装。主题是前面的rttheme16。提前谢谢。