Autofill Focus Keywords using Jquery

#584511
  • Resolved Anonymous
    Rank Math free

    Hello
    I am using AI content generator for products which generate content for description, short description, tags.
    I want to autoload focus keywords tags using jQuery. Check this link https://tinyurl.com/2q86hhtb
    I didn’t find solution from stack overflow or any other platform. Please guide.

    Thank you.
    Bilal Ahmad

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

    Thank you for contacting Rank Math today.

    We have a tutorial on how to automate the insertion of focus keywords in the different post types here: https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/

    ​​​​​​Hope this helps solve your issues.

    Don’t hesitate to get in touch if you have any other questions.

    Anonymous
    Rank Math free

    Hello,

    Thanks for your reply.

    Actually, AI content generator that we are using perform the ajax request so i want to add the keywords using js function without the page refresh. I don’t want to use Rank math plugin hooks because I don’t want to add on page load.
    I hope you will understand my concern and give me some guide regarding the issue.

    thank you.

    Hello,

    The data for the focus keywords are saved in a meta key called rank_math_focus_keyword and saved inside the postmeta table with the ID of the post where this should be present in the field post_id.

    That’s what our PHP code snippet does, it adds the data into this field using the init hook from WordPress.

    In this case, you would need to make sure that the AJAX request you are currently using is able to connect to the database and perform the same actions and save data into the database.

    Please note that you can submit this with an AJAX request but you would still need to catch the response and use PHP to save the data into the postmeta table, so it would be better to skip the AJAX part and save it directly using the PHP code snippets we shared in the tutorial above.

    If you want some code sample to save the data using AJAX this is a very simple example:

    
    jQuery(document).ready(function($) {
        // AJAX request to save data
        $('#my-form').on('submit', function(e) {
            e.preventDefault();
            
            // Get form data
            var formData = $(this).serialize();
    
            // AJAX request
            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'save_data',
                    form_data: formData
                },
                success: function(response) {
                    // Handle success response
                    console.log(response);
                },
                error: function(error) {
                    // Handle error response
                    console.log(error);
                }
            });
        });
    });
    

    And to catch the request you could use something like this in your functions.php file or similar:

    
    function save_data_callback() {
        // Verify the AJAX request and user permissions
        if (wp_verify_nonce($_POST['nonce'], 'my_ajax_nonce') && current_user_can('edit_posts')) {
            // Get form data
            $form_data = $_POST['form_data'];
            parse_str($form_data, $form_fields);
    
            // Get the post ID
            $post_id = isset($form_fields['post_id']) ? intval($form_fields['post_id']) : 0;
    
            // Save data into postmeta table
            if ($post_id > 0) {
                foreach ($form_fields as $key => $value) {
                    $sanitized_value = sanitize_text_field($value);
                    update_post_meta($post_id, $key, $sanitized_value);
                }
    
                // Return a success response
                wp_send_json_success('Data saved successfully.');
            }
        }
    
        // Return an error response
        wp_send_json_error('Error saving data.');
    }
    add_action('wp_ajax_save_data', 'save_data_callback');
    add_action('wp_ajax_nopriv_save_data', 'save_data_callback');
    

    The code snippets shared above would need to be modified to cater to your specific needs on the website.

    Don’t hesitate to get in touch if you have any other questions.

    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 ‘Autofill Focus Keywords using Jquery’ is closed to new replies.