Last active
June 16, 2023 18:59
-
-
Save yuriinalivaiko/cd6430c42ace7d1b3c49a63f4c74b3c4 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 generated by Yoast SEO plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add all approved users whose Ultimate Member profiles are public to the sitemap. | |
*/ | |
add_filter( 'wpseo_sitemap_entry', 'um_wpseo_sitemap_entry', 10, 3 ); | |
add_filter( 'wpseo_sitemap_exclude_author', 'um_wpseo_sitemaps_users' ); | |
/** | |
* Filter URL entry before it gets added to the sitemap. | |
* | |
* @param array $url Array of URL parts. | |
* @param string $type URL type. | |
* @param object $user Data object for the URL. | |
* @return array | |
*/ | |
function um_wpseo_sitemap_entry( $url, $type, $user ) { | |
if ( 'user' === $type ) { | |
$url['loc'] = um_user_profile_url( $user->ID ); | |
} | |
return $url; | |
} | |
/** | |
* Retrieve users, taking account of all necessary exclusions. | |
* | |
* @param array $users Array of user objects to filter. | |
* @return array | |
*/ | |
function um_wpseo_sitemaps_users( $users ) { | |
if ( get_option( 'blog_public' ) ) { | |
$current_page = empty( get_query_var( 'sitemap_n' ) ) ? 1 : (int) get_query_var( 'sitemap_n' ); | |
$max_entries = (int) apply_filters( 'wpseo_sitemap_entries_per_page', 1000 ); | |
$args = array( | |
'offset' => ( ( $current_page - 1 ) * $max_entries ), | |
'number' => $max_entries, | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => 'account_status', | |
'value' => 'approved', | |
'compare' => '=', | |
), | |
array( | |
'relation' => 'OR', | |
array( | |
'key' => 'wpseo_noindex_author', | |
'value' => 'on', | |
'compare' => '!=', | |
), | |
array( | |
'key' => 'wpseo_noindex_author', | |
'compare' => 'NOT EXISTS', | |
), | |
), | |
), | |
); | |
$users = get_users( $args ); | |
foreach ( $users as $i => $user ) { | |
/** | |
* 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 ) ) { | |
unset( $users[ $i ] ); | |
} | |
} | |
} | |
return $users; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/