Tutorials

Automate WordPress SEO with 3 Small PHP Snippets (Rank Math + Elementor)

Three copy-paste PHP snippets that set featured images from Elementor banners, write Rank Math meta descriptions and set focus keywords across your whole site.

On this page 5 sections
  1. 011. Set the featured image from your Elementor banner
  2. 022. Generate Rank Math meta descriptions
  3. 033. Use the page title as the focus keyword
  4. 04How to run them
  5. 05Why automate this?

Setting featured images, meta descriptions and focus keywords one page at a time can take hours on a big site. A few lines of PHP can do it all in seconds.

Here are three snippets I use on my own projects. They work with Rank Math SEO and Elementor.

This scans each page's Elementor data, finds the first section background image (usually your banner) and sets it as the featured image.

php
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: consistent blog cards and social previews, and featured images that can show in search results, with no manual uploading.

2. Generate Rank Math meta descriptions

This skips the first heading (usually the page title) and uses the next 35 words of your content as a natural summary.

php
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: every page gets a description, they all follow the same pattern, and you can still refine the important ones by hand later.

3. Use the page title as the focus keyword

The simplest one. It resets every focus keyword to the page title, so each page has one clear topic.

php
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: clear focus, no keyword stuffing, and old irrelevant keywords cleaned up.

How to run them

  1. Install the free Code Snippets plugin from WordPress.org.
  2. Create a new snippet for each script.
  3. Open your admin dashboard once to run them.
  4. Wait a few seconds and refresh to see the changes.
  5. Deactivate the snippets afterwards to keep things tidy.

Each snippet stores a flag so it only runs once.

Why automate this?

  • Time: 50 to 100 pages done in seconds instead of hours
  • Consistency: every page follows the same structure
  • Control: tweak the word count, headings or keyword logic to suit you
  • Rebrands: re-run them any time you restructure

After running all three, every page has a featured image from its banner, a readable meta description and a focus keyword that matches its title. Simple, fast, and great for content-heavy sites.

Syed Imran Murtaza

Need something like this built?

I build custom WordPress plugins, websites and web apps. Tell me what you need.

Let’s talk
→keep reading

You might also like