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?