Notify IndexNox API on Category description Update

We’re using RankMath for all of our SEO needs, but one place hasn’t been integrated fully into any SEO Plugin: The Categories. We saw, that categories’ descriptions we’re outdated and no Search Engine was notified, whilst posts, and pages, we’re updated correctly. To add the categories’ edit, we’ve added the following Code sample.

Add it to your Theme’s functions.php, or using a Code Snippet Plugin. This is for categories only, though you might extend its reach.

For it to work, you need an API Key for the IndexNow API. Place it at YOUR_API_KEY. You can get your API Key, here.

// Notify IndexNow on category update
function notify_indexnow_on_category_update($term_id, $tt_id) {
    $term = get_term($term_id, 'category');
    if (is_wp_error($term)) {
        return;
    }
    $category_url = get_category_link($term_id);
    $indexnow_endpoint = 'https://api.indexnow.org/indexnow';
    $api_key = 'YOUR_API_KEY';
    $payload = json_encode([
        'host' => parse_url(home_url(), PHP_URL_HOST),
        'key' => $api_key,
        'urlList' => [$category_url]
    ]);
    $response = wp_remote_post($indexnow_endpoint, [
        'method' => 'POST',
        'body' => $payload,
        'headers' => ['Content-Type' => 'application/json']
    ]);
    if (is_wp_error($response)) {
        error_log('IndexNow API request failed: ' . $response->get_error_message());
    } else {
        error_log('IndexNow API request successful: ' . wp_remote_retrieve_body($response));
    }
}
add_action('edited_category', 'notify_indexnow_on_category_update', 10, 2);

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *