Mentions in Buddypress
BuddyPress provides functionality for mentions, but as of now, the BuddyPress REST API does not have a dedicated endpoint for mentions specifically. However, mentions are typically integrated into the activity streams and are used when users mention other users in their activity posts (i.e., when typing @username
in the activity content).
If you want to interact with mentions in BuddyPress, you need to handle them through activity content or by filtering activity stream data. Here's how you can interact with the activity stream and potentially capture mentions:
1. Create an Activity with Mentions
When you create an activity, you can include mentions by using @username
in the activity content. This triggers BuddyPress to notify the mentioned user.
Endpoint:
http://127.0.0.1/wordpress/wp-json/buddypress/v1/activity
Request Payload Example (Creating Activity with Mentions):
{
"content": "Hey @username1, check this out!",
"user_id": 1,
"type": "activity_update"
}
In this case, the @username1
mention would trigger a notification for the user with the username username1
.
Example Request:
curl -X POST "http://127.0.0.1/wordpress/wp-json/buddypress/v1/activity" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"content": "Hey @username1, check this out!",
"user_id": 1,
"type": "activity_update"
}'
2. Get Activities with Mentions
If you want to retrieve activities that contain mentions, you can simply fetch the activities and check for mentions in the content
field.
Endpoint:
http://127.0.0.1/wordpress/wp-json/buddypress/v1/activity
Example Request:
curl -X GET "http://127.0.0.1/wordpress/wp-json/buddypress/v1/activity?per_page=5&page=1" \
-H "Authorization: Bearer YOUR_TOKEN"
Example Response (with mentions in content):
{
"activities": [
{
"id": 1,
"user_id": 1,
"content": "Hey @username1, check this out!",
"date": "2025-01-03T14:00:00",
"type": "activity_update",
"url": "http://127.0.0.1/wordpress/activity/1",
"author_name": "User 1",
"author_avatar": "http://127.0.0.1/wp-content/uploads/avatars/1/avatar.jpg"
}
]
}
You can check the content
field of each activity for mentions, which are typically denoted with the @username
format.
3. Notifications for Mentions
When you mention another user in your activity post, BuddyPress automatically generates a notification for the mentioned user. You can retrieve notifications for a user (which may include mentions) as follows:
Endpoint:
http://127.0.0.1/wordpress/wp-json/buddypress/v1/notifications
Example Request (Get Notifications):
curl -X GET "http://127.0.0.1/wordpress/wp-json/buddypress/v1/notifications?user_id=1" \
-H "Authorization: Bearer YOUR_TOKEN"
Example Response (with mention notifications):
{
"notifications": [
{
"id": 1,
"user_id": 2,
"object_id": 1,
"content": "You were mentioned in a post by @username1.",
"date": "2025-01-03T14:00:00",
"type": "mention",
"is_new": true,
"url": "http://127.0.0.1/wordpress/activity/1",
"author_name": "User 1"
}
]
}
In this example, user 2
receives a notification because they were mentioned in a post by username1
.
4. Check Mentions in Activity (Filtering by Content)
While there's no direct endpoint to filter mentions, you can retrieve activities and manually filter them based on content containing @username
:
Example Code for Filtering Mentions:
fetch('http://127.0.0.1/wordpress/wp-json/buddypress/v1/activity')
.then(response => response.json())
.then(data => {
const mentions = data.activities.filter(activity => activity.content.includes('@'));
console.log(mentions);
});
This code retrieves all activities and filters the ones that contain the @
symbol, which would indicate a mention.
Summary
While BuddyPress doesn't have a dedicated API endpoint for mentions, you can leverage activity creation and notifications to manage and track mentions. When users mention others in their activity content, BuddyPress will generate the appropriate notifications for those users, which can be accessed via the notifications API.
Let me know if you need more specific information or examples!
Comments
Post a Comment