Viewing 18 posts - 1 through 18 (of 18 total)
  • Author
    Posts
  • #881439

    Hi guys! I hope you’re doing well. I recently implemented a fix for Yoast SEO to adjust all image urls in our sitemap to point to our cdn to ensure proper indexing by google. The script we added to functions.php, as per Yoast recommendations, is below (with appropriate urls replaced):

    function wpseo_cdn_filter( $uri ) {
    return str_replace( 'http://example.com', 'http://cdn.example.com', $uri );
    }
    add_filter( 'wpseo_xml_sitemap_img_src', 'wpseo_cdn_filter' );

    You can see the article this was taken from here: https://kb.yoast.com/kb/images-in-the-xml-sitemap/

    This changed sitemap image urls in the post sitemap but not in the page sitemap. I had Yoast look into this and they’ve narrowed down to a theme issue. Their response is as follows:

    “Thank you for your continued patience. The filter works as expected when using the default Twenty Seventeen theme so we compared your pages to posts for further investigation. When you use the Avia layout builder to add a gallery, the filter is not applied.

    The most common reason is that the Avia layout builder is modifying the image URL outside of where the filter is being applied. Please contact the support team for the Avia layout builder for further assistance on how to change the image URL in their output to the CDN URL.”

    Can you please let me know how best to ensure that all image urls, even in the page sitemap, point to our cdn url? This is very important for Google SEO and I would imagine that many other clients either have or will be having this issue as well. Thank you so much for your time!

    I’ve provided access to our staging site in the private section below so that you can see what is happening.

    Best,
    Jonathan

    #881768

    Hey Jonathan,
    Enfold uses the wpseo_sitemap_urlimages filter that is at the bottom of that article.
    Please try adding this function:

    function wpseo_cdn_filter_sitemap( $uri ) {
    return str_replace( 'http://example.com', 'http://cdn.example.com', $uri );
    }
    add_filter( 'wpseo_sitemap_urlimages', 'wpseo_cdn_filter_sitemap' );

    Best regards,
    Mike

    #882069

    Hi Mike,

    Thank you for looking into this! Unfortunately that doesn’t seem to have done the trick. Do you know why it might not be working? Please feel free to go into the staging site and try this out, it’s currently implemented how you’ve said but the page sitemap urls didn’t change, except for the one test page that’s not using avia elements.

    Login info in the private section below.

    Thank you so much.

    Best,
    Jonathan

    #882402

    Hi,

    Unfortunately, it would require quite some time and customization of the theme to achieve this, so I am sorry to tell you that this is not covered by our support. However, if it’s really important for you to get this done, you can always hire a freelancer to do the job for you :)

    Best regards,
    Basilis

    #882408

    Hi Basilis,

    Thank you for your time. I believe this should be covered by support as it’s clearly an issue with the way the theme is handling image urls. My first thought was Yoast support, which I went to, but since it’s a matter of the way Enfold handles images I am hoping your team can tell me what to do. Thank you for understanding.

    Are you sure this would require extensive work and not just a tweak to the code provided by Mike? Please let me know as if just a script I need to add to functions.php then this is something I can do with your guidance. Also, since not something unique to me, this is something that all theme users will benefit from so I am hoping you can take the time to instruct me.

    Thank you,
    Jonathan

    #883223

    Hi guys, just following up on this. Thank you very much in advance as I know may take a bit of your time but I think it’s important we have a solution here for all users to benefit from when needed.

    Thank you!

    Best,
    Jonathan

    #883560

    Hi team, I’m very confused – Mike gave solution which unfortunately just didn’t work and then Basilis said you can’t help. I’m a long time user and supporter of Enfold and I truly believe this is a theme issue. Also, since it’s not unique to me as it will affect all users of enfold and Yoast I believe it is in your best interest to help with a solution as well. Can you please revisit this and discuss internally and hopefully point me to a solution?

    Thank you for your consideration.

    Best,
    Jonathan

    #883569

    Hi Jonathan,

    Hope you are doing well :)

    Sorry for the confusion! Could you please post FTP logins to your staging site here privately as well? :)

    Best regards,
    Yigit

    #883570

    Hi Yigit,

    Thanks so much – yes of course please see below!

    Best,
    Jonathan

    #883716

    Hi!

    The images are indexed correctly but I’m not sure why you have to change the uri. Where are you checking the image uri? ( see private field )

    UPDATE: You can modify the config-wordpress-seo > config.php file, look for this code around line 157.

    $src   = wp_get_attachment_url($id);
    

    Replace it with:

    $src   = wp_get_attachment_url($id);
    $src = str_replace( 'http://example.com', 'https://cdn.example.com', $src );
    

    Cheers!
    Ismael

    #883719

    Hi Ismael,

    Thank you. Sorry, I don’t understand your question – can you please explain? I was just using the code suggested by Yoast to change the image URLs shown in the page and post sitemaps to point to the cdn urls. Is this what your suggested code will do?

    Also, if you don’t mind can you please edit your code above to just show generic urls. I prefer that this thread doesn’t show up if people google our URL :)

    Thank you,
    Jonathan

    #883783

    Hi,

    I removed the urls. Did you try the modification in the config-wordpress-seo > config.php file?

    Best regards,
    Ismael

    #884174

    Hi Ismael,

    That worked! Thank you so much. It seems like I need to use the code Yoast provides still to alter the post sitemap image urls, and the code you provided alters the page sitemap image urls. Oh and to answer your question why I want to change these it’s because with a cdn google doesn’t index the images correctly if the urls aren’t changed in the sitemap to point to the cdn which is where the html in the page is referencing. So very important to be able to do, and thank you :)

    Two final questions if you don’t mind:

    1) Does the code you provided only alter the image urls in the page sitemap, or is there an impact anywhere else I should be aware of?
    2) Is there a script I can add to functions.php that will have the same effect of what you provided above? The reason I ask is because I manage my functions.php scripts in a plugin so that they aren’t overwritten on theme updates. It would be nice to be able to affect this change through there if possible.

    Thank you again. You guys are seriously amazing.

    Best,
    Jonathan

    #884517

    Hi,

    Glad it worked.

    1.) Yes, it will only alter the url in the sitemap created by SEO plugin.

    2.) Copy the “avia_filter_wpseo_sitemap_urlimages”, change the function name and then do the modification. You need to disable the current filter.

    remove_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages', 10, 2);
    add_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages_new_name', 10, 2);
    function avia_filter_wpseo_sitemap_urlimages_new_name() {
        // code here
    }
    

    Best regards,
    Ismael

    #884756

    Hi Ismael,

    Thank you for your time on this. The script didn’t seem to work so I compared to the original config.php file and noticed that it included “$images, $post_id” in the brackets after the old function name. I added those in after the new function name in our code and it seems to be doing something now but for some reason it’s only changing some of the urls but not all, and it also changes the structure of the sitemap, which then doesn’t match what’s on the page. Please see full code I added to functions.php (with urls changed) below, and links to the sitemaps for your reference in the private section. Do you have any idea why this isn’t performing the same thing as adding the modification directly to config.php as discussed earlier? If this is too much trouble just let me know and I’ll maintain as a direct modification to the theme files, but if you can easily see why this isn’t working that is ideal so that this is future-proof for any theme updates.

    When you said I need to disable the current filter, were you referring to what the code does or is there a step I missed? I may have misunderstood your instructions so please check the below to make sure correct. Thank you!

    remove_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages', 10, 2);
    add_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages_new_name', 10, 2);
    function avia_filter_wpseo_sitemap_urlimages_new_name($images, $post_id)
    	{
    	  $post = get_post($post_id);
    	  if (is_object($post)) {
    			$content = $post->post_content;
    			$elements = apply_filters('avf_add_elements_wpseo_sitemap',
    			 array(
    				'masonry' => array(
    					'pattern' => '/\[av_masonry_gallery [^]]*]/',
    					'source' => 'ids'
    				),
    				'gallery' => array(
    					'pattern' => '/\[av_gallery [^]]*]/',
    					'source' => 'ids'
    				),
    				'horizontal' => array(
    					'pattern' => '/\[av_horizontal_gallery [^]]*]/',
    					'source' => 'ids'
    				)
    				 /*
    				'accordion' => array(
    					'pattern' => '/\[av_slideshow_accordion(.+?)?\](?:(.+?)?\[\/av_slideshow_accordion\])?/sim',
    					'source' => 'sid'
    				),
    				'slideshow' => array(
    					'pattern' => '/\[av_slideshow(.+?)?\](?:(.+?)?\[\/av_slideshow\])?/sim',
    					'source' => 'sid'
    				),
    				'slideshow_full' => array(
    					'pattern' => '/\[av_slideshow_full(.+?)?\](?:(.+?)?\[\/av_slideshow_full\])?/sim',
    					'source' => 'sid'
    				),
    				'slideshow_fullscreen' => array(
    					'pattern' => '/\[av_fullscreen(.+?)?\](?:(.+?)?\[\/av_fullscreen\])?/sim',
    					'source' => 'sid'
    				)
    				*/
    			), $elements, $post_id);
    
    			$ids = avia_extract_shortcodes_attachment_ids($elements, $content);
    
    			foreach ($ids as $id)
    			{
    				$title = get_the_title($id);
    				$alt   = get_post_meta($id, '_wp_attachment_image_alt', true);
    				$src   = wp_get_attachment_url($id);
    				$src = str_replace( 'http://www.domain.com', 'https://cdn.domain.com', $src );
    				$images[] = array('src' => $src, 'title' => $title, 'alt' => $alt);
    			}
    	  }
    
    	  return $images;
    	}
    #885078

    Hi,

    The images are indexed twice in the staging site. Please look for this line.

    remove_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages', 10, 2);
    

    Replace it with:

    add_action('after_setup_theme', 'ava_disable_wpseo_sitemap_filter');
    function ava_disable_wpseo_sitemap_filter() {
         remove_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages', 10, 2);
    }

    Best regards,
    Ismael

    #885203

    Hi Ismael,

    Thank you very much. That seems to have worked! So just to confirm that it was right for me to have added in “$images, $post_id” between the brackets and that I implemented this all correctly so it doesn’t negatively affect anything elsewhere – all I did was added the below code (based on our discussions above) into functions.php via a script management plugin. If that is what you intended and the below code looks good then please confirm and you can go ahead and mark this resolved. Thank you so much :)

    add_action('after_setup_theme', 'ava_disable_wpseo_sitemap_filter');
    function ava_disable_wpseo_sitemap_filter() {
         remove_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages', 10, 2);
    }
    add_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages_new_name', 10, 2);
    function avia_filter_wpseo_sitemap_urlimages_new_name($images, $post_id)
    	{
    	  $post = get_post($post_id);
    	  if (is_object($post)) {
    			$content = $post->post_content;
    			$elements = apply_filters('avf_add_elements_wpseo_sitemap',
    			 array(
    				'masonry' => array(
    					'pattern' => '/\[av_masonry_gallery [^]]*]/',
    					'source' => 'ids'
    				),
    				'gallery' => array(
    					'pattern' => '/\[av_gallery [^]]*]/',
    					'source' => 'ids'
    				),
    				'horizontal' => array(
    					'pattern' => '/\[av_horizontal_gallery [^]]*]/',
    					'source' => 'ids'
    				)
    				 /*
    				'accordion' => array(
    					'pattern' => '/\[av_slideshow_accordion(.+?)?\](?:(.+?)?\[\/av_slideshow_accordion\])?/sim',
    					'source' => 'sid'
    				),
    				'slideshow' => array(
    					'pattern' => '/\[av_slideshow(.+?)?\](?:(.+?)?\[\/av_slideshow\])?/sim',
    					'source' => 'sid'
    				),
    				'slideshow_full' => array(
    					'pattern' => '/\[av_slideshow_full(.+?)?\](?:(.+?)?\[\/av_slideshow_full\])?/sim',
    					'source' => 'sid'
    				),
    				'slideshow_fullscreen' => array(
    					'pattern' => '/\[av_fullscreen(.+?)?\](?:(.+?)?\[\/av_fullscreen\])?/sim',
    					'source' => 'sid'
    				)
    				*/
    			), $elements, $post_id);
    
    			$ids = avia_extract_shortcodes_attachment_ids($elements, $content);
    
    			foreach ($ids as $id)
    			{
    				$title = get_the_title($id);
    				$alt   = get_post_meta($id, '_wp_attachment_image_alt', true);
    				$src   = wp_get_attachment_url($id);
    				$src = str_replace( 'https://www.domain.com', 'https://cdn.domain.com', $src );
    				$images[] = array('src' => $src, 'title' => $title, 'alt' => $alt);
    			}
    	  }
    
    	  return $images;
    	}
    #885435

    Hi,

    Yes, the code looks correct. And the staging site doesn’t index the images twice so it’s all good. We’ll close the thread now. :)

    Best regards,
    Ismael

Viewing 18 posts - 1 through 18 (of 18 total)
  • The topic ‘Sitemap image URLs for CDN’ is closed to new replies.