Created
February 24, 2025 19:02
-
-
Save yuriinalivaiko/001b39c50b558a1a1cd3a2786ee7a2fb to your computer and use it in GitHub Desktop.
Ultimate Member customization. Shortcode that displays the profile data where you need.
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 | |
/** | |
* Shortcode that displays the profile data. | |
* Example: [um_user data="roles" user_id="1"] | |
* | |
* @param array $atts Shortcode attributes: | |
* - (string) data - Data type or usermeta key. Display name by default | |
* - (int) user_id - User ID. Profile ID or current user ID by default. | |
* | |
* @return string The profile data. | |
*/ | |
if ( ! function_exists( 'um_user_shortcode' ) && function_exists( 'UM' ) ) { | |
add_shortcode( 'um_user', 'um_user_shortcode' ); | |
function um_user_shortcode( $attr = array(), $content = '' ) { | |
$default_user_id = 0; | |
if ( um_is_core_page( 'user' ) ) { | |
$default_user_id = um_get_requested_user(); | |
} elseif ( is_user_logged_in() ) { | |
$default_user_id = um_profile_id(); | |
} elseif ( is_singular() ) { | |
$default_user_id = get_post()->post_author; | |
} | |
$defaults_atts = array( | |
'data' => 'display_name', | |
'user_id' => $default_user_id, | |
); | |
$atts = shortcode_atts( $defaults_atts, $attr, 'um_user_shortcode' ); | |
if ( empty( $atts['user_id'] ) || ! is_numeric( $atts['user_id'] ) ) { | |
return ''; | |
} | |
if ( um_user( 'ID' ) !== $atts['user_id'] ) { | |
$global_user_id = um_user( 'ID' ); | |
um_fetch_user( $atts['user_id'] ); | |
} | |
$value = um_user( $atts['data'] ); | |
if ( ! empty( $global_user_id ) ) { | |
um_fetch_user( $global_user_id ); | |
} | |
if ( empty( $value ) ) { | |
return ''; | |
} | |
switch ( $atts['data'] ) { | |
case 'roles': | |
$value = array_map( array( UM()->roles(), 'get_role_name' ), $value ); | |
break; | |
} | |
return is_array( $value ) ? implode( ', ', $value ) : (string) $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment