Rank Math WPBakery Sitemap Fix

Many of us started using new SEO plugin for our WordPress websites. Looks like we have alternative plugin to Yoast finally. Don’t get me wrong, I really enjoyed Yoast but I was sick and tired of their adverts to buy premium… Why premium? Nice options not always must come as paid features. With help comes Rank Math. In short time over 30k+ installations and 5 stars rate.

I’m using Rank Math on all my websites since over a month and I totally love it! It’s super easy to setup (Rank Math can copy your Yoast settings), brilliant backend and what’s more important super easy to use. On top of that all works, Google is happy with the work that Rank Math is doing on my websites.

rank math wordpress seo

Sounds like a lovely story. During my tests I actually discovered one nasty thing. Google is not indexing my images. After some checking Armands from A-Fotografy and I have found that none of them were in the sitemap. Blog posts were fine, but 0 images on pages. Pages were done via WPBakery Page Builder. That was the issue – something doesn’t work between WPBakery and Rank Math.

Solution

Thanks to Rank Math awesome support we have a solution! And it’s super simple to apply to everyone who is using WPBakery and Rank Math.

add_filter( 'rank_math/sitemap/urlimages', function( $images, $id ){
    if ( empty( $images ) ) {
        $post = get_post( $id );
        if ( $post && strpos( $post->post_content, '[vc_row' ) !== false ) {
            preg_match_all( '/(?:image|images|ids|include)\=\"([^\"]+)\"/', $post->post_content, $matches );
            foreach ( $matches[1] as $m ) {
                $ids = explode( ',', $m );
                foreach ( $ids as $id ) {
                    if ( (int) $id ) {
                        $images[] = array(
                            'src' => wp_get_attachment_url( $id ),
                            'title' => get_the_title( $id ),
                        );
                    }
                }
            }
        }
    }

    return $images;
}, 10, 2 );

Copy above code as it is and paste it at the end of your functions.php (ideally in child theme so the code remains after update). Thanks to that few lines of code your images on pages done by WPBakery will appear again in sitemap and Google Image Search.

Simple working solution! Enjoy!

Czy wiesz, że udostępniając ten artykuł pomagasz mi? Dziękuję za Twoje wsparcie.

Leave a Reply

Your email address will not be published.