URL as focus keyword

#577996
  • Resolved Tom Derbyshire
    Rank Math free
    Content AI Starter

    Hello

    Is there anyway to configure the post URL to always be the focus keyword instead of the title?

    I publish quite often so lots of these type of things would save me a lot of time. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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

    Tom Derbyshire
    Rank Math free
    Content AI Starter

    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

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘URL as focus keyword’ is closed to new replies.