简体   繁体   中英

auto create tags wordpress

How we can make auto create tags/category from title when publising not save the page?

I try to modified this code:

add_action('save_post', 'add_title_as_category');
function add_title_as_category( $postid ) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    $post = get_post($postid);
    if ( $post->post_type == 'page') { 
        $term = get_term_by('slug', $post->post_name, 'category');
        if ( empty($term) ) {
            $add = wp_insert_term( $post->post_title, 'category', array('slug'=> $post->post_name) );
            if ( is_array($add) && isset($add['term_id']) ) {
                wp_set_object_terms($postid, $add['term_id'], 'category', true );
            }
        }
    }  
}

This code work good when editing and then saving the page but not when "publising". I am using another plugin that create the pages and is not supporting the creation of Tags and categories.

I think the problem is the "save_post" but I try other function such as 'publish_post' from wordpress.org too.

If you have any idea on how to achieve this your more than welcome :)

publish_post is in fact correct action to hook into when you want to do something after post has been published. But since you're using some plugin to create pages, you should try hooking into wp_insert_post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM