Hello,
Thank you for contacting Rank Math support.
We understand that you want to use the focus keyword as the post URL instead of the title.
You can try implementing this code and let us know if it works for you:
add_action( 'save_post', 'wpse75679_save_post' );
function wpse75679_save_post( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// verify post is not a revision
if ( ! wp_is_post_revision( $post_id ) ) {
// unhook this function to prevent infinite loop
remove_action( 'save_post', 'wpse75679_save_post' );
// get the focus keyword from Rank Math
$focus_keyword = get_post_meta($post_id, 'rank_math_focus_keyword', true);
// sanitize the focus keyword and check if it is not empty
$focus_keyword = sanitize_title( $focus_keyword );
if ( ! empty( $focus_keyword ) ) {
// update the post slug with the focus keyword
wp_update_post( array(
'ID' => $post_id,
'post_name' => $focus_keyword
));
}
}
// re-hook this function
add_action( 'save_post', 'wpse75679_save_post' );
}
This code will change the post URL to the focus keyword that you set in the Rank Math meta box. It will also sanitize the focus keyword and check if it is set or not before updating the post slug. Please note that this code will only work for new or updated posts. It will not change the URLs of your existing posts unless you edit them and update them.
To use this code, please follow these steps: https://rankmath.com/kb/wordpress-hooks-actions-filters/
We hope this helps you use the focus keyword as the post URL. If you have any further questions or issues, please let us know.
Best regards
Genius! Thanks very much! 🙂
Hi there,
We’re delighted to hear that your issue has been resolved. We appreciate your kind words and your support.
I’m closing this ticket now but if you ever have another question or need any help in the future, please don’t hesitate to create a new forum topic. We’ll be more than happy to assist you again.
Thank you for choosing Rank Math and have a wonderful day!
Cheers