Grow your CSS skills. Land your dream job.

Post Slug to Tag

  • # June 14, 2013 at 6:19 pm

    I created a WordPress function to auto create post tags from post slugs, and it seems to work pretty well, but wondering if it can be improved as I’m a php newbie.

    add_action(‘save_post’, ‘slug_to_tag’);
    function slug_to_tag($post_id) {
    global $post;
    $sluggish = $post -> post_name;
    $pattern = ‘/-/';
    $replacement = ‘, ‘;
    $sluggies = preg_replace($pattern, $replacement, $sluggish);
    wp_set_post_terms($post -> ID, $sluggies, ‘post_tag’, true);
    }

    Thanks for any help!

    # June 14, 2013 at 8:31 pm

    Had to revise code to this, but would appreciate any input. Thank you!

    function slug_to_tag($post_ID) {
    $gpt = get_post($post_ID);
    $sluggish = $gpt -> post_name;
    $pattern = ‘/-/';
    $replacement = ‘, ‘;
    $sluggies = preg_replace($pattern, $replacement, $sluggish);
    wp_set_post_terms($post_ID, $sluggies, ‘post_tag’, true);
    }

    add_action(‘save_post’, ‘slug_to_tag’);

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".