Function for adding an IndexNow link to your list of entries

No comments

 

IndexNow is a free and open source protocol under an Attribution-ShareAlike Creative Commons licence that allows you to notify search engines that you have just published something new, that a piece of content has been modified and updated or that a URL has been removed by sending a ping to the search engines to speed up the crawling and possible indexing process that could otherwise take hours, days or weeks.

This does not mean that it guarantees you immediate indexing or a better position, it just reduces the time it takes for each search engine to go through your page and crawl what is new, updated or deleted as you will be communicating it to them at the moment. As of today, the search engines that accept Index Now pings are: Microsoft Bing, Naver, Seznam.cz, Yandex and Yep.

How to create an Index Now API key

To use IndexNow you need an API key. This key will verify that you are the owner of the website and must contain a minimum of 8 and a maximum of 128 hexadecimal characters. The key can only contain lowercase (a-z), uppercase (A-Z), numbers (0-9) and hyphens (-). So I understand that as long as it complies with the structure it doesn't matter where you generate it from.

But to be on the safe side, just visit this Bing page and generate it and then download it with the arrow on the right.

Function for adding an IndexNow link to your list of entries 0

The downloaded .txt file should be placed in the root of the page. You are now ready to start submitting pages.

How to submit URLs with Index Now

There are different ways to do this. Many SEO plugins already integrate Index Now as a feature, but if you don't use any of those plugins there is one created by Bing dedicated just to this task.

IndexNow Plugin

This is the quickest and easiest way to do it and saves you from having to generate and host the key on your site as the plugin will generate the API key on activation (it will generate a new one every time you deactivate and reactivate the plugin).

You can change this key from time to time, but as it is a unique key for a website, Index Now advises you not to change it too often.

The plugin allows you to automatically submit URLs every time you post something new, make manual submissions, view statistics of submitted URLs, view recent submitted URLs and resubmit them.

Manual sending from a browser

To send a URL(you can also send them in groups) manually from a browser as an HTTP request, after having hosted the .txt with the API key in your root, you can do it in two ways (where https://tu-url-com, will be the URL of your page you want to send and API_KEY will be the API key of Index Now.

https://www.bing.com/indexnow?url=https://tu-url-com/&key=CLAVE_API

https://api.indexnow.org/indexnow?url=https://tu-url-com/&key=CLAVE_API

To verify that it has been sent correctly, you can go to the Bing Webmaster Tools page (you must have an account associated with your domain) and you will find the sending history.

Function for adding an IndexNow link to your list of entries 1

The intermediate option I propose between the plugin and manual submission is a function that adds a custom column with a link to submit to Index Now in your list of entries that will look like this:

Function for adding an IndexNow link to your list of entries 2

This way you can send each URL whenever you want with a single click. You must take into account that, although you can send up to 10,000 URLs per day, there is a time interval between each submission, which is about 5 minutes, so it does not make sense to make mass submissions without paying attention to this interval. Other related questions are answered in the IndexNow FAQ.

*Remember to replace YOUR_API_KEY_DE_INDEX_NOW with your API key in // Index Now settings in the code.

// Añade una columna personalizada para Index Now en la lista de posts
function add_index_now_column($columns) {
    $columns['index_now'] = 'Index Now';
    return $columns;
}
add_filter('manage_posts_columns', 'add_index_now_column');

// Rellenar la columna personalizada con un enlace
function add_index_now_column_content($column_name, $post_id) {
    if ($column_name === 'index_now') {
        $post_url = get_permalink($post_id);
        if ($post_url) {
            echo '<a href="#" class="send-to-index-now" data-post-id="' . esc_attr($post_id) . '">Enviar a Index Now</a>';
        } else {
            echo 'URL no disponible';
        }
    }
}
add_action('manage_posts_custom_column', 'add_index_now_column_content', 10, 2);

// Añadir el script para manejar el clic en el enlace
function add_index_now_script() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('.send-to-index-now').on('click', function(e) {
                e.preventDefault();
                var post_id = $(this).data('post-id');
                $.post(ajaxurl, {
                    action: 'send_to_index_now',
                    post_id: post_id
                }, function(response) {
                    if (response.success) {
                        alert('Enviada a Index Now con éxito');
                    } else {
                        alert('Error: ' + response.data);
                    }
                });
            });
        });
    </script>
    <?php
}
add_action('admin_footer', 'add_index_now_script');

// Función para enviar la URL a Index Now
function send_to_index_now() {
    if (!isset($_POST['post_id'])) {
        wp_send_json_error('ID del post no proporcionado');
    }

    $post_id = intval($_POST['post_id']);
    $post_url = get_permalink($post_id);

    if (!$post_url) {
        wp_send_json_error('No se pudo obtener la URL del post');
    }

    // Configuración de Index Now
    $api_key = 'TU_API_KEY_DE_INDEX_NOW'; // Reemplaza con tu clave API de Index Now
    $index_now_url = 'https://api.indexnow.org/indexnow/'; // Endpoint de Index Now

    // Datos para enviar a Index Now
    $data = [
        'host' => parse_url($post_url, PHP_URL_HOST),
        'key' => $api_key,
        'keyLocation' => 'https://' . parse_url($post_url, PHP_URL_HOST) . '/' . $api_key . '.txt',
        'urlList' => [$post_url],
    ];

    // Enviar la solicitud HTTP POST a Index Now
    $response = wp_remote_post($index_now_url, [
        'headers' => ['Content-Type' => 'application/json; charset=utf-8'],
        'body' => json_encode($data),
    ]);

    if (is_wp_error($response)) {
        wp_send_json_error('Error al enviar la URL a Index Now: ' . $response->get_error_message());
    }

    $response_code = wp_remote_retrieve_response_code($response);
    if ($response_code === 200) {
        wp_send_json_success('Enviada a Index Now con éxito');
    } else {
        wp_send_json_error('Error al enviar la URL a Index Now. Código de respuesta: ' . $response_code);
    }
}
add_action('wp_ajax_send_to_index_now', 'send_to_index_now');

Suscríbete para recibir los post en tu email sin publicidad

Related articles

Leave a comment

Este blog se aloja en LucusHost

LucusHost, el mejor hosting