Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active September 19, 2023 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuriinalivaiko/0fc328e5681172543307a352a44d0797 to your computer and use it in GitHub Desktop.
Save yuriinalivaiko/0fc328e5681172543307a352a44d0797 to your computer and use it in GitHub Desktop.
This code adds a shortcode [um_friends_button] that displays the "Add Friend" button. This code requires the "Ultimate Member" and "Ultimate Member - Friends" plugins to be installed.
<?php
/**
* Add a shortcode [um_friends_button] that displays the "Add Friend" button.
*
* Attributes:
* - (int) post_id - The post/page ID. Optional. Default: current post/page ID.
* - (int) user_id - The user ID. Optional. Default: requested user ID or the post/page author ID.
*
* Example: [um_friends_button user_id="20"]
*
* Notes:
* - the button is shown for logged in users only.
* - the button is hidden if the attribute `user_id` is the current user.
*/
if ( defined( 'um_friends_version' ) && ! shortcode_exists( 'um_friends_button' ) ) {
function um_friends_button_shortcode( $atts = array() ) {
$def_atts = array(
'post_id' => get_the_ID(),
'user_id' => um_get_requested_user(),
);
$args = shortcode_atts( $def_atts, $atts, 'um_friends_button' );
$user_id1 = is_numeric( $args['user_id'] ) ? $args['user_id'] : ( is_numeric( $args['post_id'] ) ? get_post( $args['post_id'] )->post_author : 0 );
$user_id2 = get_current_user_id();
if ( $user_id1 > 0 && $user_id2 > 0 ) {
wp_enqueue_script( 'um_friends' );
wp_enqueue_style( 'um_friends' );
return '<div class="um um-friends-button-shortcode">' . UM()->Friends_API()->api()->friend_button( $user_id1, $user_id2 ) . '</div>';
}
}
add_shortcode( 'um_friends_button', 'um_friends_button_shortcode' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment