Ultimate Member with The SEO Framework

-
ultimate-member-theseo-framework

I’ve found a way to integrate Ultimate Member with the The SEO Framework( TSF ) plugin and I’m happy to share the solution with you! This includes the Member Profile URLs to the TSF Sitemap. It generates a SiteMap file on your site: www.yoursite.com/sitemap.xml for Search Engine Optimization.

If you are new to The SEO Framework, please read this doc. And for those who are new to WP plugin customization, you can add the code in your theme’s functions.php file or use Code Snippet plugin.

Read the descriptions below to learn how the code snippet works.

add_filter( 'the_seo_framework_sitemap_additional_urls', function( $custom_urls = [] ) {

    if( ! function_exists('um_get_core_page') ) return $custom_urls;

    $users = get_users();
   
    foreach( $users as $user ){
        $user_id = $user->ID;
        $user_profile_slug = UM()->user()->get_profile_link( $user_id );
        $last_date_modified = get_user_meta( $user_id, "um_last_date_modified",true );
        if( $last_date_modified ){
            $custom_urls["{$user_profile_slug}"] =  [
                'lastmod'  => date('Y-m-d H:i:s', $last_date_modified ), // difficult to determine.
            ];
        }else{
            $custom_urls["{$user_profile_slug}"] =  [
                'lastmod'  => $user->user_registered, // difficult to determine.
            ];
        }
    }
	

	return $custom_urls;
} );

add_action( 'um_after_user_updated', function( $user_id ){
    update_user_meta( $user_id, "um_last_date_modified", current_time('timestamp') );
});

The filter hook ‘the_seo_framework_sitemap_additional_urls’ allows us to include Member Profile URLs to the site map file.

add_filter( 'the_seo_framework_sitemap_additional_urls', function( $custom_urls = [] ) {
	return $custom_urls;
} );

In the example below, we are retrieving the users using the WordPress core function get_users. By default, it retrieves all users including the administrators. To learn how to retrieve users by user role, please read this doc.

     $users = get_users();
   
    foreach( $users as $user ){
        $user_id = $user->ID;
        $user_profile_slug = UM()->user()->get_profile_link( $user_id );
        $last_date_modified = get_user_meta( $user_id, "um_last_date_modified",true );
        if( $last_date_modified ){
            $custom_urls["{$user_profile_slug}"] =  [
                'lastmod'  => date('Y-m-d H:i:s', $last_date_modified ), // difficult to determine.
            ];
        }else{
            $custom_urls["{$user_profile_slug}"] =  [
                'lastmod'  => $user->user_registered, // difficult to determine.
            ];
        }
    }
	

We have two conditions to add the last modification date of the mebmer/user profile. By default, If we install the TSF plugin, the sitemap will generate the profile URLs with the last modified date which is requred in the sitemap. We are using the user_registered value from the wp_user tables as a default date and time value.

        if( $last_date_modified ){
            $custom_urls["{$user_profile_slug}"] =  [
                'lastmod'  => date('Y-m-d H:i:s', $last_date_modified ), // difficult to determine.
            ];
        }else{
            $custom_urls["{$user_profile_slug}"] =  [
                'lastmod'  => $user->user_registered, // difficult to determine.
            ];
        }

But once a user updated their profile, it falls back to the um_last_date_modified added by the code snippet below. The code snippet updates the date-time value on profile update.

add_action( 'um_after_user_updated', function( $user_id ){
    update_user_meta( $user_id, "um_last_date_modified", current_time('timestamp') );
});

According to the TSF documentation when updating the sitemap :

TSF caches the sitemap’s generated content in the database for 604 800 seconds (1 week).

It refreshes whenever you:

  • Update or publish any type of post or page;
  • Update the permalink settings;
  • Or update the SEO settings

And that’s all!

Hope you learned something new today! Don’t forget to leave a comment or ask any questions in the comment section.

Help me reach other people who need the help
the seo frameworktsfultimate member
Leave a Response

Leave a Reply

Your email address will not be published. Required fields are marked *