Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use the Semrush API to fetch domain ranks in WordPress


How can I use the Semrush API to fetch domain ranks in WordPress


Here's an example of how you can use the Semrush API to fetch domain ranks in a WordPress plugin:

1. Install the Semrush API client library for PHP:

You can use Composer to install the `silktide/semrush-api` package:

bash
composer require silktide/semrush-api

2. Create a new WordPress plugin and add the following code:

php
<?php
/*
Plugin Name: Semrush Domain Ranks
*/

require_once __DIR__ . '/vendor/autoload.php';

use Silktide\SemRushApi\ClientFactory;

function semrush_get_domain_ranks() {
    $client = ClientFactory::create('your_semrush_api_key');
    $result = $client->getDomainRanks('example.com', [
        'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US,
        'export_columns' => [
            \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_RANK,
            \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_TRAFFIC,
            \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_BUDGET,
        ]
    ]);

    foreach ($result as $row) {
        $rank = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_RANK);
        $traffic = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_TRAFFIC);
        $adwords_budget = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_BUDGET);

        echo "Domain Rank: $rank, Traffic: $traffic, AdWords Budget: $adwords_budget\n";
    }
}

add_action('admin_menu', function() {
    add_management_page('Semrush Domain Ranks', 'Semrush Domain Ranks', 'manage_options', 'semrush-domain-ranks', 'semrush_get_domain_ranks');
});

3. Replace `'your_semrush_api_key'` with your actual Semrush API key.

4. The code creates a new WordPress admin page under Tools > Semrush Domain Ranks that displays the domain ranks, traffic, and AdWords budget for the domain "example.com" in the US database.

5. You can customize the API call to retrieve data for different domains or databases by modifying the arguments passed to the `getDomainRanks()` method.

6. The plugin uses the `silktide/semrush-api` library to make the API request and process the response. It iterates over the `Result` object and prints out the domain rank, traffic, and AdWords budget for each row.

By integrating the Semrush API with WordPress, you can build custom tools and dashboards to display domain-level SEO data and insights directly within your WordPress admin area. This allows you to leverage Semrush's data to inform your content and marketing strategies without leaving your WordPress site.

Citations:
[1] https://github.com/silktide/semrush-api
[2] https://developer.semrush.com/api/basics/get-started/
[3] https://developer.semrush.com/api/v3/analytics/domain-reports/
[4] https://www.semrush.com/kb/5-api
[5] https://www.semrush.com/kb/972-api-reports