Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for your question and for mentioning Yoast in your description of the issue. Thanks as well for your patience as I work through the topics that accumulated during my travels.

    You may find this earlier topic of interest:

    MLA Conflicts with Yoast SEO Sitemap

    You can adapt the code in the example plugin developed for that topic and see if it improves your results. To get the example plugin, navigate to the Settings/Media library Assistant Documentation tab and click the “Example Plugins” button. Type “yoast” in the text box and click “Search Plugins” to filter the table.

    You are looking for “MLA Yoast SEO Example” plugin. Find that plugin and hover over the title in the left-most column. Click the “Install” (or “Update”) rollover action, then go to the WordPress Plugins/Installed Plugins submenu and activate the example plugin as you would any other plugin. You can also click the “Download” link to get a copy of the code for your own development.

    As you will see, the example plugin hooks a filter provided by Yoast, wpseo_sitemap_urlimages, runs the shortcodes in the post/page and returns the URLs for the images selected by the shortcode(s). You can replace the code that parses the post/page content with your do_shortcode('[mla-gallery]') from the template.

    I hope you find the example plugin helpful. I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above suggestions. Thanks for your interest in MLA.

    Thread Starter Ibnul H.

    (@ibnul)

    Hello,
    Thanks for your reply and explanation.

    Actually we’ve already installed and activated the “MLA Yoast SEO Example” plugin but still its not showing the number of images in the sitemap. It’s showing only 1 image.

    Also I’m not clear what you mean by :
    As you will see, the example plugin hooks a filter provided by Yoast, wpseo_sitemap_urlimages, runs the shortcodes in the post/page and returns the URLs for the images selected by the shortcode(s). You can replace the code that parses the post/page content with your do_shortcode(‘[mla-gallery]’) from the template.

    Would you please make me clear ?

    Thanks for your help.

    Plugin Author David Lingren

    (@dglingren)

    The “MLA Yoast SEO Example” plugin won’t work as-is because it looks for shortcodes in the content of the post/page, and your do_shortcode('[mla-gallery]') is in the PHP template. You will have to copy the code from the example plugin to a plugin of your own or add the code to your theme’s functions.php file. Whoever modified the PHP template should have enough PHP skills to do this.

    To get you started, here is a stripped down version of the example plugin that works on my test system:

    <?php
    /*
    Plugin Name: Ibnul Yoast SEO Example
    Description: Supports WordPress SEO by Yoast Page Analysis and XMP Sitemap generation
    Author: David Lingren
    Version: 1.00
    */
    
    class IbnulYoastSEOExample {
    	public static function initialize() {
    		/*
    		 * Filter: 'wpseo_sitemap_urlimages' - Allows updates to the list of images in the page/post
    		 * Filter: 'wpseo_sitemap_entry' - adjusts the entire entry before it gets added to the sitemap
    		 *
    		 * Defined/applied in /wordpress-seo/inc/class-sitemaps.php
    		 */
    		add_filter( 'wpseo_sitemap_urlimages', 'IbnulYoastSEOExample::wpseo_sitemap_urlimages', 10, 2 );
    	}
    
    	public static function wpseo_sitemap_urlimages( $url, $post_id ) {
    		global $post;
    
    		$post = get_post( $post_id ); // Set the parent post/page; used in [mla_gallery]
    		$the_gallery = do_shortcode( '[mla_gallery]' );
    
    		// If MLA is not active the shortcode is not processed; substitute empty results.
    		if ( $the_gallery == '[mla_gallery]' ) {
    			$the_gallery = '';
    		}
    
    		// Collect the Title values for gallery items
    		$titles = array();
    		$ref_count = preg_match_all( '/\<img.*aria-describedby=".*-([0-9]*)".*src="([^"]*)"/', $the_gallery, $references );
    		if ( $ref_count ) {
    			foreach( $references[1] as $ref_index => $reference ) {
    				$item = get_post( absint( $reference ) );
    				if ( !empty( $item->post_title ) ) {
    					$titles[ $references[2][ $ref_index ] ] = $item->post_title;
    				}
    			}
    		}
    
    		// Collect the URL and Alt Text values for gallery items
    		$ref_count = preg_match_all( '/\<img.*src="([^"]*)".*alt="([^"]*)"/', $the_gallery, $references );
    		if ( $ref_count ) {
    			foreach( $references[1] as $ref_index => $reference ) {
    				if ( !empty( $titles[ $reference ] ) ) {
    					$url[] = array( 'src' => $reference, 'title' => $titles[ $reference ], 'alt' => $references[2][ $ref_index ] );
    				} else {
    					$url[] = array( 'src' => $reference, 'alt' => $references[2][ $ref_index ] );
    				}
    			}
    		}
    
    		unset( $ref_count, $references );
    		return $url;
    	}
    } // Class IbnulYoastSEOExample
    
    // Install the filter at an early opportunity
    add_action('init', 'IbnulYoastSEOExample::initialize');
    ?>
    

    You can copy the above code to a PHP file and add it as a plugin to your system. Let me know if you have any problems or further questions about the above code.

    Thread Starter Ibnul H.

    (@ibnul)

    Hello,
    I really appreciate your quick help and code.

    I’ve added the above code as a plugin and activated.
    I think it’s working as it was number of images 1 for all the portfolio items.
    But after activating the plugin its showing multiple.
    https://echoes.paris/portfolio-sitemap.xml
    You can have a look here.
    Actually still the number of images is not correct.
    Is there any chance of being correct later please ?

    Thanks a ton again.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the kind words and the good news regarding the sample code I suggested.

    You wrote “the number of images is not correct.” I regret that I am not in a position to work with you on perfecting the code for your specific site. Any remaining issues you have with XML sitemap generation will have to be worked out with your site’s developers or with the Yoast SEO support staff.

    If you find an issue that is directly related to Media Library Assistant or the [mla_gallery] shortcode post an update here and I will investigate it. Thanks for your understanding and for your interest in MLA.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘index images in yoast seo sitemap’ is closed to new replies.