Auto Focus keyword not identical to post title when using Apostrophe

#800717
  • Resolved Anonymous
    Rank Math free

    Hi,
    On my website, I am using the following code to automatically use the post title as focus keyword, yet when the title include a classic apotrophe (‘), the focus keywords converts it to another apostrophe (’). I am unable to find a way to fix this, i’ve tried editing the variable that is inserted into the focus keyword with str_replace but it simply doesn’t work.
    Do you have any idea how I could manage to fix this ?
    Thanks for the help

    https://pastebin.com/8qsQLr0m

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hello,

    I understand the issue you are facing with the automatic insertion of the post title as the focus keyword when the title contains an apostrophe. The conversion of the apostrophe to another character can be frustrating.

    To address this concern, you can modify the code snippet slightly to handle the apostrophe conversion correctly. You can use the wp_specialchars_decode function in PHP to decode the special characters like apostrophes before setting them as the focus keyword.

    Here is an example of how you can adjust the code snippet to handle apostrophes correctly:

    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'        => 'post' //replace post with the name of your post type
        ));
        foreach ($posts as $p) {
            $title = get_the_title($p->ID);
            $decoded_title = wp_specialchars_decode($title, ENT_QUOTES); // Decode special characters like apostrophes
            update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower($decoded_title));
        }
    }
    add_action('init', 'update_focus_keywords');

    By using wp_specialchars_decode with ENT_QUOTES, you can ensure that the apostrophes are handled correctly in the focus keyword. Please replace your existing code with this adjusted snippet and see if it resolves the issue.

    If you encounter any further difficulties or need additional assistance, feel free to reach out. Thank you for bringing this to our attention.

    Sources:
    How to Automate the Insertion of Focus Keyword » Rank Math

    Hello,

    Let us know how this goes after updating the filter with the one we have shared above.

    Meanwhile, if you have any other concerns, please don’t hesitate to contact us anytime to assist you further.

    Looking forward to helping you.

    Anonymous
    Rank Math free

    Hi,
    I have tested the code and am still facing the exact same issue. The post title is indeed used as the main focus keyword but the different apostrophes still causes the issue. Oddly enough, if I copy paste the Focus Keyword as the title of the post, Rank Math still doesn’t consider both of these strings to be equal. The other way around works though (manually copy pasting the title as the main keyword).

    Anonymous
    Rank Math free

    When trying to remove the apostrophe with str_replace, it still manages to appear in the focus keyword, thus making me wonder if this php code can actually edit this string ?

    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'        => 'livre'
        ));
        foreach ($posts as $p) {
            $title = get_the_title($p->ID);
            $title_without_apostrophes = str_replace("'", "", $title); // Remove apostrophes from the title
            $decoded_title = wp_specialchars_decode($title_without_apostrophes); // Decode special characters like apostrophes
            update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower($decoded_title));
        }
    }
    add_action('init', 'update_focus_keywords');

    Hello,

    Please use the following filter to add the correct apostrophe encoding in the focus keyword:

    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'        => 'livre' //replace post with the name of your post type
        ));
        foreach ($posts as $p) {
    		$title_main = strtolower(get_the_title($p->ID));
    		$title = str_replace('’', "'", $title_main);
            update_post_meta($p->ID, 'rank_math_focus_keyword', $title  );
        }
    }
    add_action('init', 'update_focus_keywords');

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Anonymous
    Rank Math free

    Hey, this for some reason still doesn’t fix the apostrophe issue. Any idea why ?

    Hello,

    It is working fine on our end. We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Anonymous
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Anonymous
    Rank Math free

    Logins have been created, is it necessary to include ftp information as well ? I edit files through the native wordpress theme files editor. I include my code in rank-math.php on my child theme
    I have removed your suggested code after trying it out, in case you are wondering (replaced it with a shorter version which did the same thing)

    Hello,

    You are not using the filter I have shared. You are using a different filter to update the focus keyword:

    Please replace it with the one I shared to fix the issue.

    Looking forward to helping you.

    Anonymous
    Rank Math free

    Hey, as explained in my previous message, I removed your code after testing it out, feel free to use it again and apply it to any cpt if you want
    Edit : I have applied your code again, and tested in a book review ( titled t’esting )

    Hello,

    Sorry, I didn’t see your last message. The code I shared had the HTML code for the apostrophe added by our plugin in the focus keyword. However, it was converted to the symbol by the editor. I have modified the code on your site:

    Now, the focus keyword is being added correctly:

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Anonymous
    Rank Math free

    Lovely ! Thanks for the help, I tried with an HTML code previously but I must have used the wrong one. Hope this ticket helps more people in the future

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on
    https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post

    about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Auto Focus keyword not identical to post title when using Apostrophe’ is closed to new replies.