Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Created May 14, 2024 13:14
Show Gist options
  • Save kimcoleman/8e181e7824014954cc721dcd1ba32d55 to your computer and use it in GitHub Desktop.
Save kimcoleman/8e181e7824014954cc721dcd1ba32d55 to your computer and use it in GitHub Desktop.
Filters the Yoast meta title and meta description on the profile page to display the user's display name and biography.
<?php
/**
* Filters the Yoast meta title on the profile page to display the user's display name.
*
* @param string title The current page's generated title.
* @return string The filtered title.
*/
function my_pmpro_profile_page_meta_title( $title ) {
global $pmpro_pages;
// Return early if we're not on the profile page.
if ( empty( $pmpro_pages['profile'] ) || ! is_page( $pmpro_pages['profile'] ) ) {
return $title;
}
// Get the profile user's display name.
$pu = pmpromd_get_user();
$display_name = pmpro_member_directory_get_member_display_name( $pu );
if ( ! empty( $display_name ) ) {
$title = $display_name;
}
return $title;
}
add_filter( 'wpseo_title', 'my_pmpro_profile_page_meta_title' );
/**
* Filters the Yoast meta description on the profile page to display the user's biography.
*
* @param string $description The current page's generated meta description.
* @return string The filtered meta description.
*/
function my_pmpro_profile_page_meta_description($description) {
global $pmpro_pages, $current_user;
// Return early if we're not on the profile page.
if ( empty( $pmpro_pages['profile'] ) || ! is_page( $pmpro_pages['profile'] ) ) {
return $description;
}
// Get the profile user.
$pu = pmpromd_get_user();
// If a bio is set, update the meta description with it
if ( ! empty( $pu->description ) ) {
$description = $pu->description;
}
return $description;
}
add_filter( 'wpseo_metadesc', 'my_pmpro_profile_page_meta_description' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment