BuddyDev

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.

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.

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.

B). Inject the button at the bottom of the post content.

Have fun!

 

One Response to BuddyPress Private Message button Shortcode

  • Hello,

    Please how can I add a CSS class to style the link on blog posts.