How to Build a Keyword Tracking System in the WordPress Dashboard
This guide explains how to create a PHP‑based keyword tracking system for WordPress, covering why to track keywords, setting up a custom dashboard widget, storing data in a custom table, fetching rankings via API, and displaying real‑time results on the dashboard.
Creating a keyword tracking system in WordPress allows you to monitor keyword performance directly from the dashboard. Below is a step‑by‑step guide.
Why Track Keywords?
Deepen SEO insights: tracking keywords is the key to unlocking website traffic growth potential. Precise analysis reveals high‑search‑volume and relevant keywords, enabling effective SEO strategies that attract target audiences and significantly boost traffic.
Continuous performance monitoring and optimization: dynamic changes in keyword rankings directly indicate SEO effectiveness. Regular tracking helps identify successful strategies, consolidate advantages, and quickly spot declining rankings to adjust tactics, maintaining or improving visibility.
Set Up the System
Step 1: Create a Custom Dashboard Widget
Add the widget to the WordPress dashboard for easy access.
function add_keyword_dashboard_widget() {
wp_add_dashboard_widget(
'keyword_tracker',
'Keyword Performance Tracker',
'display_keyword_tracker'
);
}
add_action('wp_dashboard_setup', 'add_keyword_dashboard_widget');
function display_keyword_tracker() {
echo '
Keyword
Rank
Change
';
}Step 2: Store Keywords in the Database
Use a custom database table to store keyword data.
function create_keyword_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'keyword_tracker';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
keyword varchar(255) NOT NULL,
rank int(5) DEFAULT 0 NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__, 'create_keyword_table');Step 3: Fetch and Update Keyword Rankings
Use an API (e.g., Google Search Console API) to fetch keyword rankings.
function fetch_keyword_rankings() {
// 模拟 API 调用以进行演示
$keywords = ['example', 'test', 'wordpress'];
$ranks = [3, 5, 10];
foreach ($keywords as $index => $keyword) {
update_keyword_rank($keyword, $ranks[$index]);
}
}
function update_keyword_rank($keyword, $rank) {
global $wpdb;
$table_name = $wpdb->prefix . 'keyword_tracker';
$wpdb->replace($table_name, [
'keyword' => $keyword,
'rank' => $rank
]);
}
// 安排函数每天运行
if (!wp_next_scheduled('update_keyword_rankings')) {
wp_schedule_event(time(), 'daily', 'update_keyword_rankings');
}
add_action('update_keyword_rankings', 'fetch_keyword_rankings');Step 4: Display Data in the Dashboard
Retrieve and display the data in the dashboard widget.
function load_keyword_data() {
global $wpdb;
$table_name = $wpdb->prefix . 'keyword_tracker';
$keywords = $wpdb->get_results("SELECT * FROM $table_name");
foreach ($keywords as $keyword) {
echo '
' . esc_html($keyword->keyword) . '
' . esc_html($keyword->rank) . '
' . calculate_rank_change($keyword->keyword, $keyword->rank) . '
';
}
}
function calculate_rank_change($keyword, $current_rank) {
// 排名变化逻辑的占位符
return '+1'; // 模拟改变
}
add_action('wp_dashboard_setup', 'load_keyword_data');Conclusion
By adopting this carefully crafted PHP‑based system, you can seamlessly integrate it into the WordPress dashboard for efficient real‑time keyword performance monitoring. The system provides strong customization, flexible extensibility, and meets specific SEO needs for individuals or teams, keeping you ahead in a competitive market.
PHP Practical Development Fast‑Track
Scan the QR code to receive free learning materials
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.