0

I would like to modify the slug of new posts by adding text before and after the original slug. The slug is then sent over an API via a plugin that hooks into save_posts and transition_post_status.

add_action( 'save_post', array( $this, 'action_save_post' ), 11 );
add_action( 'transition_post_status', array( $this, 'action_transition_post_status' ), 9, 3 );

I was originally using a method as indicated here which uses save_post, however while this method did correctly change the post slug on the website, the old slug still got sent over the API.

In the method linked to above, I tried changing the save_post hook priority to 6 from 10, however that still didn't work.

I then tried something totally new, which was this:

function custom_permalink_slug( $original_slug, $slug, $post_id, $post_status, $post_type, $post_parent ) {
    $support_post_type = array( 'deal' );
    if ( in_array( $post_type, $support_post_type ) ) {
        $rand = '';
        $rands = get_the_terms($post_id, 'rand');
        if (!empty($rands) && !is_wp_error($rands)) {
            $rand = $rands[0]; // Only get first rand if there is more than one
            $rand = sanitize_title( $rand->name );
        }
        if (empty( $rand ) || strpos(  $original_slug, $rand ) !== false) {
            return; // No subtitle or already in slug
        }
        $slug = $rand . '-' . $slug . '-home';
    }
    return $slug;
}
add_filter( 'pre_wp_unique_post_slug', 'custom_permalink_slug', 100, 6 );

This method seems like it changes the slug correctly, as it displays the new slug on the post edit screen, however in reality it is still using the old slug (presumably because it doesn't change the post_name meta field).

Any ideas how to get this working correctly?

9
  • Have you tried with the wp_after_insert_post hook instead of save_post? What's the code in your action_transition_post_status() function?
    – Sally CJ
    Feb 11 at 17:02
  • @SallyCJ I tried changing save_post to wp_after_insert_post in the plugin code, however that didn't help. Here is a link to the plugin code: github.com/aaroneaton/better-yourls/blob/…
    – Edegist
    Feb 14 at 0:09
  • So you want to change the post_name (post slug) and then you tried using the save_post hook, but when you make your remote API call, you're still getting the old post slug, i.e. the one before you added the "before" and "after" text? If so, then why not update the slug and make the API call via the same callback? I.e. Store the slug in a variable, pass it to wp_update_post(), and use that variable with your API request.
    – Sally CJ
    Feb 15 at 4:42
  • Thanks for your reply @SallyCJ. I would rather a method that wouldn't require me to edit the actual API plugin's files. Do you think that is possible in this instance?
    – Edegist
    Feb 16 at 5:07
  • Actually, I couldn't find any instance of post_name in the code you linked to, and looking at line 419, is that what you meant by, "The slug is then sent over an API via a plugin"? Or did you mean line 404 which retrieves the permalink which uses the post slug?
    – Sally CJ
    Feb 16 at 6:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.