BuddyPress Private Message button Shortcode
Welcome back folks.
We will see today how can we create a shortcode for the BuddyPress Send Private Message button as well as auto injecting the Send Private Message to post entries for the authors.
Goal:-
- Creating shortcode for the BuddyPress Send Private Message button to send Private message to any user using user id or username.
- Auto adding the Send private Message Button for post author.
Now that our goals are clear, let us do it step by step.
Step 1:- Creating a generic function to get the url for sending private message to a given user id.
Since BuddyPress does not provide a generic function for generating the link for sending message, let us create one.
1 2 3 4 5 6 7 8 9 10 | /** * Get a link to send PM to the given User. * * @param int $user_id user id. * * @return string */ function buddydev_get_send_private_message_to_user_url( $user_id ) { return wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( $user_id ) ); } |
This function will give us the url for sending private message to the given user id.
Step 2:- Create shortcode for the private message button.
Let us create a shortcode now. We are going to use 'bp-pm-button' as the shortcode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /** * Shortcode [bp-pm-button username=optional_some_user_name] * * @param array $atts shortcode attributes. * @param string $content content. * * @return string */ function buddydev_private_message_button_shortcode( $atts, $content = '' ) { // User is not logged in. if ( ! is_user_logged_in() ) { return ''; } $atts = shortcode_atts( array( 'user_id' => '', 'username' => '', 'label' => 'Send Private Message', ), $atts ); $user_id = absint( $atts['user_id'] ); $user_login = $atts['username']; // if the username is given, override the user id. if ( $user_login ) { $user = get_user_by( 'login', $user_login ); if ( ! $user ) { return ''; } $user_id = $user->ID; } if ( ! $user_id ) { if ( ! in_the_loop() ) { return ''; } $user_id = get_the_author_meta('ID' ); } // do not show the PM button for the user, if it is aimed at them. if ( bp_loggedin_user_id() === $user_id ) { return ''; } // if we are here, generate the button. $button = sprintf('<a href="%1$s">%2$s</a>', buddydev_get_send_private_message_to_user_url( $user_id ), $atts['label'] ); return $button . $content; } add_shortcode( 'bp-pm-button', 'buddydev_private_message_button_shortcode' ); |
As you see, the shortcode accepts 3 options:-
- username:- (optional)(string) The user name of the user to whom we want the logged in user to send the private message
- user_id:- (optional)(numeric) The user id of the user for whom the link should be created.
- label:- (optional) (string), The button label.
Example:- To generate button to send private message to the user 'admin' we wilol use the following shortcode.
[bp-pm-button username=admin]
Note:- If you do not specify user id or username and you are using it in a post loop, It will show a link to send message to the author of the post.
Part 2:- Auto Injecting the private message at the bottom of the post.
If you do not want to use the shortcode and want to create a Send Private Message for post author, you can do that by using the following code
a). Get the button for the post author. Please note that you must have the function from step 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Get the button for the post author. * * @return string */ function buddydev_get_author_private_message_button() { if ( ! in_the_loop() || ! is_user_logged_in() ) { return ''; } $user_id = get_the_author_meta('ID'); // can't pm to themselves. if ( bp_loggedin_user_id() === $user_id ) { return ''; } return sprintf('<a href="%1$s">%2$s</a>', buddydev_get_send_private_message_to_user_url( $user_id ), 'Send Private Message' ); } |
B). Inject the button at the bottom of the post content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Do you want to automatically inject the button on each post? * * @param string $entry_content entry content. * * @return string */ function buddydev_inject_private_message_button( $entry_content ) { // append at the bottom. $entry_content = $entry_content . buddydev_get_author_private_message_button(); // if you want to only do it on single entry pages(single post etc), you can comment the above line // and uncomment the block below /* if ( is_singular() ) { $entry_content = $entry_content . buddydev_get_author_private_message_button(); } */ // want to test for a post type, you may use is_singular('post_type_name') instead of is_singular() return $entry_content; } // append to the post entry? add_filter( 'the_content', 'buddydev_inject_private_message_button' ); |
Have fun!
Hello,
Please how can I add a CSS class to style the link on blog posts.