Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Post Slug to Tag

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #45542
    manonatelier
    Member

    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!

    #138774
    manonatelier
    Member

    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)
  • The forum ‘Other’ is closed to new topics and replies.