Automatically add the focus keyword to a product from the product title

#1072938
  • Resolved Rcadventureland
    Rank Math free

    I am trying to automatically add the focus keyword to a product from the product title for existing products and then everytime a new product is added. I used the following script:

    /**
    * Function to automatically update the focus keyword with the product title
    */
    function update_focus_keywords() {
    $products = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘product’ // replace post with the name of your post type
    ));
    foreach ($products as $p) {
    update_post_meta($p->ID, ‘rank_math_focus_keyword’, strtolower(get_the_title($p->ID)));
    }
    }
    add_action(‘init’, ‘update_focus_keywords’);

    I get the following error in the editor. An error occurred while saving your changes. Please try again. If the problem persists, you may need to manually update the file via FTP.

    I did it via FTP as well but no luck and if I bring up a product page, this is displayed at the top: Updating failed. The response is not a valid JSON response.

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

    I apologize for the issue you are facing with the script and the error messages. It seems like there might be a couple of issues with the code or the way it’s being implemented. Here’s a corrected version of your script that should work without errors:

    /**
     * Function to automatically update the focus keyword with the product title
     */
    function update_focus_keywords() {
        $products = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'product' // replace post with the name of your post type
        ));
        foreach ($products as $p) {
            update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_the_title($p->ID)));
        }
    }
    add_action('init', 'update_focus_keywords');

    Key Changes:

    1. Corrected HTML Entities: Ensure that you are using => instead of => and $p->ID instead of $p->ID. This is crucial as using HTML entities can cause syntax errors in PHP.
    2. Error Handling: If you continue to receive errors, it may be beneficial to check your server’s error logs for more details on what might be causing the issue.
    3. JSON Response Error: The "Updating failed. The response is not a valid JSON response" error can sometimes occur due to issues with permalinks or server settings. You might want to try resetting your permalinks by going to Settings → Permalinks in your WordPress dashboard and simply clicking Save Changes without making any changes.

    If you still encounter issues after trying these steps, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further. If possible, include your site URL in your reply for more tailored assistance.

    Thank you for raising this concern!

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

    still the same issue. Is the path of the file

    domain-public_html-wp-content-themes

    or

    public_html-wp-content-plugins-seo-by-rank-math

    Hello,

    Thank you for contacting Rank Math support.

    You will have to create the rank-math.php file in your theme’s folder. Here is a guide that can help: https://rankmath.com/kb/wordpress-hooks-actions-filters/#rank-math-php-file

    You can also use a plugin like Code Snippets to add the code.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Automatically add the focus keyword to a product from the product title’ is closed to new replies.