Managing SEO for every page manually can be time-consuming, especially if you have a large website with dozens or even hundreds of pages. Setting the featured image, writing meta descriptions, and assigning focus keywords one by one can take hours.
This is where custom automation scripts can save a huge amount of time. With just a few lines of PHP code, you can optimize your entire website automatically. Below are three simple scripts I use to handle my own projects. They work perfectly with Rank Math SEO and Elementor.
1. Set Featured Image Automatically from Elementor Banner
This script scans your Elementor data and finds the first background image used in a section (usually your banner). It then sets that image as the page’s featured image.
This is especially useful for sites where the banner image is visually central but not manually assigned as a featured image.
function ias_replace_banner_from_elementor_bg() {
if (!is_admin()) return;
if (get_option('_ias_elementor_banner_replaced')) return;
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$pages = get_posts($args);
$processed = 0;
foreach ($pages as $page) {
$post_id = $page->ID;
if (get_post_meta($post_id, '_ias_banner_replaced', true)) continue;
$elementor_data = get_post_meta($post_id, '_elementor_data', true);
if (empty($elementor_data)) continue;
$data = json_decode($elementor_data, true);
if (!is_array($data)) continue;
$image_url = ias_find_elementor_bg_image($data);
if ($image_url) {
$image_id = media_sideload_image($image_url, $post_id, null, 'id');
if (!is_wp_error($image_id)) {
delete_post_thumbnail($post_id);
set_post_thumbnail($post_id, $image_id);
update_post_meta($post_id, '_ias_banner_replaced', 'yes');
$processed++;
}
}
}
update_option('_ias_elementor_banner_replaced', true);
update_option('_ias_elementor_banner_replaced_count', $processed);
}
add_action('admin_init', 'ias_replace_banner_from_elementor_bg');
function ias_find_elementor_bg_image($elements) {
foreach ($elements as $element) {
if (!empty($element['settings']['background_image']['url'])) {
return $element['settings']['background_image']['url'];
}
if (!empty($element['elements']) && is_array($element['elements'])) {
$result = ias_find_elementor_bg_image($element['elements']);
if ($result) return $result;
}
}
return null;
}
Why it helps:
- Makes your blog cards and previews look consistent.
- Improves visual SEO (featured images show up in Google results and social previews).
- Works automatically without manual uploading.
2. Auto-Generate Rank Math Meta Descriptions
Meta descriptions influence click-through rates in search results. Writing them manually for every page is tedious, but this script creates one automatically.
It skips the first heading (usually your page title) and then takes the next 35 words from your page content to form a natural, readable summary.
function ias_update_rankmath_meta() {
if (!is_admin()) return;
if (get_option('_ias_rankmath_update_done')) return;
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$pages = get_posts($args);
$processed = 0;
foreach ($pages as $page) {
$post_id = $page->ID;
if (get_post_meta($post_id, '_ias_rankmath_updated', true)) continue;
$content = $page->post_content;
// Remove first H1 or H2
$content_without_first_heading = preg_replace('/<h[1-2][^>]*>.*?<\/h[1-2]>/is', '', $content, 1);
$clean_text = wp_strip_all_tags($content_without_first_heading);
$desc = wp_trim_words($clean_text, 35);
update_post_meta($post_id, 'rank_math_description', $desc);
$processed++;
}
update_option('_ias_rankmath_update_done', true);
update_option('_ias_rankmath_update_count', $processed);
}
add_action('admin_init', 'ias_update_rankmath_meta');
Why it helps:
- Saves time by creating descriptions for every page.
- Keeps descriptions consistent across your site.
- You can later refine specific ones manually if needed.
3. Set Focus Keyword as Page Title
Finally, the simplest and most important part. Your focus keyword tells Rank Math (and Google) what the page is about.
Instead of trying to guess or generate multiple keywords, this snippet resets all focus keywords to use only the page title. It’s a clean, focused approach that improves SEO clarity.
function ias_reset_rankmath_focus_keyword() {
if (!is_admin()) return;
if (get_option('_ias_reset_focus_keyword_done')) return;
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$pages = get_posts($args);
$processed = 0;
foreach ($pages as $page) {
$post_id = $page->ID;
$keyword = sanitize_text_field($page->post_title);
update_post_meta($post_id, 'rank_math_focus_keyword', $keyword);
$processed++;
}
update_option('_ias_reset_focus_keyword_done', true);
update_option('_ias_reset_focus_keyword_count', $processed);
}
add_action('admin_init', 'ias_reset_rankmath_focus_keyword');
Why it helps:
- Keeps SEO focus clear and consistent.
- Prevents keyword stuffing or confusion.
- Automatically resets old or irrelevant keywords from previous edits.
How to Use These Scripts
- Install the Code Snippets plugin (free on WordPress.org).
- Create a new snippet for each script above.
- Run each one once from your Admin Dashboard.
- Wait a few seconds and refresh to see the changes.
- Once complete, you can deactivate each snippet to keep things clean.
Why You Should Automate This
- Time Efficiency: Updating 50 or 100 pages manually could take hours. These scripts do it in seconds.
- Consistency: Every page follows the same SEO pattern, which Google loves.
- Control: You can tweak the logic (like word count, headings, or keywords) to match your brand strategy.
- Perfect for Rebranding: If you ever rebrand or restructure, you can rerun these scripts to regenerate all your SEO data instantly.
Final Thoughts
Automating small but repetitive SEO tasks can make a huge difference in your website’s performance. Tools like Rank Math already do a lot, but adding these custom PHP snippets gives you full control and saves time in the long run.
Once you’ve run these three snippets, every page on your site will have:
- A professional featured image pulled from your Elementor banner
- A natural meta description extracted from the page content
- A clean focus keyword that matches the title
It’s simple, fast, and works beautifully for content-heavy websites or institutes that publish multiple pages regularly.