Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active December 6, 2023 12:54
Show Gist options
  • Save yuriinalivaiko/079c952fbc79d6bbf784834b1871f570 to your computer and use it in GitHub Desktop.
Save yuriinalivaiko/079c952fbc79d6bbf784834b1871f570 to your computer and use it in GitHub Desktop.
This code adds a shortcode that displays the header section of the Ultimate Member profile.
<?php
/**
* Shortcode that displays the profile header.
* Example: [um_profile_header form_id="7" user_id="1"]
*
* @global \wpdb $wpdb
*
* @param array $atts Shortcode attributes:
* - (int) form_id - profile form ID. The first profile form if blank.
* - (int) user_id - user ID. The current page/post author if blank.
*
* @return string The profile header HTML.
*/
function um_profile_header_shortcode( $atts ) {
$args = shortcode_atts(
array(
'form_id' => 0,
'user_id' => 0,
),
$atts
);
global $wpdb;
if ( empty( $args['form_id'] ) ) {
$args['form_id'] = (int) $wpdb->get_var(
"SELECT pm.post_id
FROM {$wpdb->postmeta} pm
WHERE pm.meta_key = '_um_mode'
AND pm.meta_value = 'profile'
LIMIT 1;"
);
}
if ( empty( $args['user_id'] ) ) {
$args['user_id'] = is_singular() ? get_the_author_meta( 'ID' ) : um_profile_id();
}
if ( empty( $args['form_id'] ) || empty( $args['user_id'] ) ) {
return;
}
$post_data = UM()->query()->post_data( $args['form_id'] );
if ( is_array( $post_data ) ) {
$args = array_merge( $args, $post_data );
}
$args = apply_filters( 'um_pre_args_setup', $args );
$args = apply_filters( 'um_shortcode_args_filter', $args );
if ( ! um_get_requested_user() ) {
um_set_requested_user( $args['user_id'] );
um_fetch_user( $args['user_id'] );
}
ob_start();
?>
<div class="um um-profile um-viewing <?php echo esc_attr( 'um-' . $args['form_id'] ); ?> um-profile-header-shortcode">
<div class="um-form" data-mode="profile">
<?php
do_action( 'um_profile_header_cover_area', $args );
do_action( 'um_profile_header', $args );
do_action( 'um_profile_navbar', $args );
?>
</div>
</div>
<?php
UM()->shortcodes()->dynamic_css( $args );
$content = ob_get_clean();
if ( um_get_requested_user() ) {
um_reset_user();
}
return $content;
}
add_shortcode( 'um_profile_header', 'um_profile_header_shortcode' );
@yuriinalivaiko
Copy link
Author

Instruction

Install

Copy a code snippet above (without the opening php tag) and paste it to the functions.php file in the active theme directory.

Add shortcode

You can add the [um_profile_header] shortcode to the page/post content to display the page/post author like the header section of the Ultimate Member profile.

You can add this shortcode to the sidebar. The shortcode will be shown on the single (post) pages and will be hidden on archive (category) pages.

Attributes

Use optional attribute form_id to select a profile form you prefer if you created multiple profile forms.
Use optional attribute user_id to display the profile header for the certain user.

Examples

[um_profile_header]
[um_profile_header form_id="7"]
[um_profile_header form_id="7" user_id="1"]

Screenshots

Image - Shortcode view in the post content.
01

Image - Shortcode view in the sidebar.
02

@demian1985
Copy link

demian1985 commented Feb 21, 2023

@yuriinalivaiko could you explain to me why I get no_cover class for the user's post while the user has submitted their profile cover?

See here the post: https://snipboard.io/P06SCI.jpg
See here the profile: https://snipboard.io/P06SCI.jpg

Edit: when I add a specific from ID it is showing. I think it's depending on this.

@yuriinalivaiko
Copy link
Author

@demian1985
Maybe you have multiple profile forms with different settings. The shortcode gets settings from the first fount profile form if there is no form_id attribute.

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