Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active February 12, 2023 20:38
Show Gist options
  • Select an option

  • Save yuriinalivaiko/8e4de41eb79304f6a08df57043e9c810 to your computer and use it in GitHub Desktop.

Select an option

Save yuriinalivaiko/8e4de41eb79304f6a08df57043e9c810 to your computer and use it in GitHub Desktop.
This code snippet adds all approved users whose Ultimate Member profiles are public to the sitemap.
<?php
/**
* Add all approved users whose Ultimate Member profiles are public to the sitemap.
* Add this code to the file functions.php in the active theme directory.
*/
add_filter( 'wp_sitemaps_max_urls', 'um_sitemaps_users_max_urls', 10, 2 );
add_filter( 'wp_sitemaps_users_pre_max_num_pages', 'um_sitemaps_users_max_num_pages', 10, 1 );
add_filter( 'wp_sitemaps_users_pre_url_list', 'um_sitemaps_users_get_url_list', 10, 2 );
/**
* Filters the maximum number of URLs displayed on a users sitemap.
*
* @see https://developer.wordpress.org/reference/hooks/wp_sitemaps_max_urls/
*
* @param int $max_urls The maximum number of URLs included in a sitemap. Default 2000. Change to 100 for users.
* @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user').
* @return int
*/
function um_sitemaps_users_max_urls( $max_urls, $object_type ) {
if ( 'user' === $object_type ) {
$max_urls = 100;
}
return $max_urls;
}
/**
* Filters the max number of pages for a user sitemap before it is generated.
* Returning a non-null value will effectively short-circuit the generation.
*
* @see https://developer.wordpress.org/reference/hooks/wp_sitemaps_users_pre_max_num_pages/
*
* @param int|null $max_num_pages The maximum number of pages. Default null.
* @return int
*/
function um_sitemaps_users_max_num_pages( $max_num_pages ) {
if ( get_option( 'blog_public' ) ) {
$args = array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'account_status',
'value' => 'approved',
'compare' => '=',
),
),
);
$query = new WP_User_Query( $args );
$total_users = $query->get_total();
if ( 0 < $total_users ) {
$max_num_pages = (int) ceil( $total_users / wp_sitemaps_get_max_urls( 'user' ) );
}
}
return $max_num_pages;
}
/**
* Filters the users URL list before it is generated.
* Returning a non-null value will effectively short-circuit the generation.
*
* @see https://developer.wordpress.org/reference/hooks/wp_sitemaps_users_pre_url_list/
*
* @param array|null $url_list The URL list. Default null.
* @param int $page_num Page of results.
* @return array
*/
function um_sitemaps_users_get_url_list( $url_list, $page_num ) {
if ( get_option( 'blog_public' ) ) {
$args = array(
'fields' => 'ids',
'number' => wp_sitemaps_get_max_urls( 'user' ),
'paged' => $page_num,
'meta_query' => array(
array(
'key' => 'account_status',
'value' => 'approved',
'compare' => '=',
),
),
);
$query = new WP_User_Query( $args );
$users = $query->get_results();
$url_list_new = array();
foreach ( $users as $user_id ) {
/**
* Skip users whose profiles can not be indexed by searching engines.
* Settings that influence profile indexing by the priority:
* "Search engine visibility" in [wp-admin > Settings > Reading]
* "Profile Privacy" in [Account > Privacy]
* "Avoid indexing my profile by search engines in [Account > Privacy]
* "Avoid indexing profile by search engines" in [wp-admin > Ultimate Member > User Roles > Edit Role]
* "Avoid indexing profile by search engines" in [wp-admin > Ultimate Member > Settings > General > Users]
*/
if ( UM()->user()->is_profile_noindex( $user_id ) ) {
continue;
}
$sitemap_entry = array(
'loc' => um_user_profile_url( $user_id ),
);
$url_list_new[] = apply_filters( 'um_wp_sitemaps_users_entry', $sitemap_entry, $user_id );
}
if ( 0 < count( $url_list_new ) ) {
$url_list = $url_list_new;
}
}
return $url_list;
}
@yuriinalivaiko
Copy link
Author

yuriinalivaiko commented Jul 23, 2022

This gist is a part of the article How to add profiles to sitemap.

Documentation: https://docs.ultimatemember.com/
Support forum: https://wordpress.org/support/plugin/ultimate-member/

Example:
example - sitemap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment