Trying to alter layout of bp_activity_action()
-
The default layout of the Actions (forum actions) is like this:
Admin posted on the forum topic TOPIC NAME in the group GROUP NAME: 39 minutes ago · View · Delete
Content of forum post
I want it to look like this:
TOPIC NAME
Admin said:
Content of Forum Post
View Full – Delete
Group/Category: GROUP NAME
I think that layout would be more intuitive for my users.
It would be way helpful to be able to sort and have more control over <?php bp_activity_action() ?>
Has anyone made progress in this regard? Any tips would be appreciated.
-
I’ve been trying to figure this out for a little while and I’m noticing a variable used called the $activities_template;
Which may be the type of thing i’m looking for, is anyone familiar with how to alter the Activities Template or can point me to a tutorial?
Thanks
That’s interesting. Try var_dump of $activities_template and see what’s in it? I’m curious myself.
echo '<pre>'; var_dump($activities_template); echo '</pre>';
(Side note You can do a rough hack of your /entry.php file, for example, flip it so
<?php bp_activity_content_body() ?> is on top of <?php bp_activity_action() ?>, which puts more emphasis on the message content. This doesn’t achieve the high level of control you want, though.)
Huh, $activities_template is a cool variable. It gets you part of the way there (or all the way there, if you are willing to get complicated).
For example, to get “Admin said:” , you can use $activities_template->activities[0]->display_name (for the first activity item; to get the whole list you would have to do a “foreach” loop).
You can get Content of Forum Post the same way. View Full is already in /entry.php, and Delete is an admin option that comes up as part of <?php bp_activity_action() ?>
It is more problematic when it comes to TOPIC NAME and GROUP NAME, since you would have to reverse engineer the pre-baked $activities_template->activities[0]->action string. I would probably do something like:
1. check $activities_template to see if the current item is a forum post
2. if so, run php string functions to strip out the hyperlinked TOPIC NAME
3. and also run php string functions again on the same string to strip out the hyperlinked GROUP name
Would that solve it?
Thanks for the idea 3sixty, looks like what i’m doing may not even be possible, look at this var dump:
["action"]=>
string(345) "<a href="http://domain.com/members/admin/" title="Admin">Admin</a> posted on the forum topic <a href="http://domain.com/groups/introduce-yourself-232181584/forum/topic/hi-everyone-im-bill/">Hi Everyone, I'm Bill</a> in the group <a href="http://domain.com/groups/introduce-yourself-232181584/">Introduce Yourself</a>"
["content"]=>
string(27) "More testing of Signatures."Maybe some extremely tricky CSS can pull it off, i’m not sure. I wonder how I could alter that “action” variable. It would be nice to have alot more control over it.
I think you’re definitely headed in the right direction
Looks like the Forum Topic isn’t placed in it’s own variable but it’s all bunched together with the “action” variable, so the entire variable is:
Admin Posted on the Forum Topic TOPIC etc…
I wish there was a specific variable for the Forum Topic and the Group associated with the activity, then your idea would work perfectly!
I’m thinking there must be someway to alter the output of that “action” variable without re-writing core by making a new bp_activity_action() and outputting an action variable that would allow greater control.
I found the action varialbe is assigned like this:
$action = $activities_template->activity->action;
But I don’t know where to go from there, I’m spinning in circles lol.
You can alter the action variable using php string manipulation functions. I would start with trim(), which might be all you need to get rid of the boilerplate text.
It’s probably best to take a multi-faceted approach.
Instead of changing the way that content appears on the way out (by the time you call the global $activities_template, the content is already written in the db), you might consider manipulating it on the way in. Less overhead that way. For group related stuff like forum posts, open up bp-groups.php and search for uses of the function groups_record_activity. You’ll notice that the arguments for this function are almost always filtered. For instance, in the function groups_new_group_forum_post, you can modify the way that the activity action is displayed – you might do something like this in your theme’s functions.php:
function my_format_activity_action($activity_action, $post_id, $post_text, &$topic) {
$action = $post_text->topic_poster_name . " said:";
return $action;
}
add_filter( 'groups_activity_new_forum_topic_action', 'my_format_activity_action', 1, 4 );That’s a lot easier than after-the-fact string manipulation, both for you as a developer and for the system that has to do the processing. Fish around in the files for other places where apply_filters appears – you can build functions like the one I lay out above for any filtered output.
As far as moving things around and putting them in different orders, you’ll definitely want to look at the activity/entry.php template file. That’s where all the markup appears, so any changes (like ordering and appearance) should happen there and in CSS.
And if you just want to change the wording here and there, you might also consider using language files (https://codex.buddypress.org/how-to-guides/customizing-labels-messages-and-urls/). It’s a relatively future-proof way to do customization.
Excellent hint Boone but the codex link you posted is a page not found.
No idea if this is useful, but I’ll post it for reference.
You can remove the meta links and time since in the activity_action via a filter (put the functions in your functions.php of your theme):
function remove_activity_meta( $content ) {
return '';
}
add_filter( 'bp_activity_permalink', 'remove_activity_meta' );
add_filter( 'bp_activity_time_since', 'remove_activity_meta' );
add_filter( 'bp_activity_delete_link', 'remove_activity_meta' );You can then add the time since wherever you want using:
<?php echo bp_core_time_since( bp_get_activity_date_recorded() ) ?> ago
Hey Everyone, thanks for the help, I tried the Filters but couldn’t quite get it working (yet)
I was able to get the method that 3sixty suggested using regular expressions, here is the code:
<?php $activityString = $activities_template->activities[$i]->action . ' endofstring'; /* Entire Activity into a String */
//echo $activityString . '<br/>';
/* Split up the Activity into it's different pieces and echo them out */
$authorName = $activities_template->activities[$i]->display_name;
$forumTopic = array();
$activityGroup = array();
$viewDelete = array();
/* If it is a forum topic, display forum topic name */
if (preg_match('/posted on the forum topic\s+([\s\S]*?)\s+in the group/', $activityString, $forumTopic)) {
echo $forumTopic[1] . '<br/>';
echo $authorName . ' said: <br/>';
}
/* Display Content Body */
bp_activity_content_body();
/* Print out the View and Delete links - THIS STILL ISN't WORKING */
if (preg_match('/·\s+([\s\S]*?)\s+·/', $activityString, $viewDelete)) {
echo 'Group: ' . $viewDelete[1];
}
/* If activity is associated with a group/category, print that out */
if (preg_match('~in the group\s+([\s\S]*?</a>):~', $activityString, $activityGroup)) {
echo 'Group: ' . $activityGroup[1];
}
?>As you can see it’s split up based on text surrounding the different activity items like it selects text in between ‘posted on the forum topic’ and ‘in the group’ and assigns it to a string.
I think the filters approach might be a bit better though, i’ll look into doing that.
So how would I create a function like so?
get_forum_topic_string(){
}
I think if I had an example like that I would be able to understand how filters work better.
Also is there anywhere I can look to see all the filters I can use in stuff like this that Andy showed? add_filter( ‘bp_activity_time_since’, ‘remove_activity_meta’ );
Thanks.
The method I talk about above – using a filter on groups_activity_new_forum_topic_action – will only work for future activity updates. It doesn’t change the display of anything that’s already written in the database.
I am looking for the same display-format (as mentioned in the initial post) for ALL activity-stream-items, so that it will be easier read-able for my users. I have changed the font-size of my page to 14px and the Group-names are very long, which is the reason I need to change the display-position of those items.
can I use the code from Andy for ALL activity-items, not just the Forums-activity ?
Please help.
Many thanks !
I was having the same issue and saw this thread while researching it.
Not sure if this is the “best way” to solve this but it worked for me with minimal core change and that’s what I was looking for.In bp-activity-templateargs.php goto the function named bp_insert_activity_meta copy this entire function to your functions.php file….rename this function to “fb_insert_activity_meta” or whatever you prefer.
Below and outside of this function add the line add_action ( ‘override_insert_activity_meta’, ‘fb_insert_activity_meta’);
Now back to the bp-activity-templateargs.php….goto the function named bp_get_activity_action() and change this:
if ( !empty( $action ) )
$action = bp_insert_activity_meta( $action );to this:
if ( !empty( $action ) )
{
if(has_action(‘override_insert_activity_meta’))
{
do_action(‘override_insert_activity_meta’, $action);
}else
{
$action = bp_insert_activity_meta( $action );
}}
This will basically override the function and allow u to change the content there with minimal change to the core as far as I can see.
I thought I’d resurrect this post, as it’s almost 1/2 a year old. Maybe someone has figured this out? I’d love to know the secret
@ehegwer – this was just answered the other day in the forums and filtering the output – do a quick search.
thanks!
How I can modify bp_activity_action to show only post title & author?
@jordashtalon how i can get post name, post url & other as you did it for the author name ($authorName = $activities_template->activities[$i]->display_name;)?
“Andy Peatling said 8 months, 4 weeks ago:
No idea if this is useful, but I’ll post it for reference.You can remove the meta links and time since in the activity_action via a filter (put the functions in your functions.php of your theme):
function remove_activity_meta( $content ) {
return ”;
}
add_filter( ‘bp_activity_permalink’, ‘remove_activity_meta’ );
add_filter( ‘bp_activity_time_since’, ‘remove_activity_meta’ );
add_filter( ‘bp_activity_delete_link’, ‘remove_activity_meta’ );
How i can remove also a “say:”, “publish new post:” phrases?
- The topic ‘Trying to alter layout of bp_activity_action()’ is closed to new replies.