Disable HTML in activity updates
-
Hey. Been playing with BP for two months now and I’ve just realised I can add HTML codes to the activity updates (my bad )
Is there any way of disabling or limiting the use of HTML tags ?
Thanks !
-
If you want to remove all HTML, this should work:
`function bbg_remove_activity_html( $content ) {
return strip_tags( $content );
}
add_filter( ‘bp_get_activity_content_body’, ‘bbg_remove_activity_html’, 1 );
add_filter( ‘bp_get_activity_content’, ‘bbg_remove_activity_html’, 1 );
add_filter( ‘bp_activity_content_before_save’, ‘bbg_remove_activity_html’, 1 );`If you want to remove HTML tags more selectively, check out the filter at the end of this function, which will allow you to modify which tags are allowed: https://buddypress.trac.wordpress.org/browser/tags/1.5.4/bp-activity/bp-activity-filters.php#L90
@boonebgorges thank you very much ! I’ll try this
@boonebgorges I’ve tried and it worked but I didn’t have in mind that mentions also use html to link to users, so using strip_tags is also removing that ``.
I’d like to allow @mentions and links but not the explicit use of ``. Would that be possible ?
Thanks again !
I don’t know off the top of my head how to do that, unfortunately. The only way I can imagine is something like this:
– filter bp_activity_allowed_tags and disallow all tags except for ``
– then write another filter that catches the content very late in the process (eg ‘bp_activity_content_before_save’ with a priority of 9999) and does some fancy regex to figure out which remaining `` tags are @-mentions and which are not, and strips the latter.Good luck!
Thank you again !
I’ll try to implement this but in the meanwhile I’ll just allow links. I don’t imagine this being a security threat.
Cheers !
I’m a newbie to php and the wordpress/buddypress way of doing things, and I seem to learn best by seeing examples, so I’m posting my “action” for overriding the allowed tags. I was able to get one other approach to work where I removed filters on bp_activity_filter_kses and added my own, but this version also works and seems cleaner. If anyone knows of a better approach, please let me know. I’m all about optimizing my code.
#********************************************************* # set allowable html tags #********************************************************* function my_allowed_tags() { global $allowedtags; $allowedtags = array( 'a' => array( 'href' => array (), 'title' => array ()), 'abbr' => array( 'title' => array ()), 'acronym' => array( 'title' => array ()), 'b' => array(), 'blockquote' => array( 'cite' => array ()), 'cite' => array (), 'code' => array(), 'pre' => array(), 'del' => array( 'datetime' => array ()), 'em' => array (), 'i' => array (), 'q' => array( 'cite' => array ()), 'strike' => array(), 'strong' => array(), 'sub' => array(), 'sup' => array() ); // adding any additional tags this way because it's easier to read $allowedtags['h1'] = array(); $allowedtags['h2'] = array(); $allowedtags['h3'] = array(); $allowedtags['h4'] = array(); $allowedtags['h5'] = array(); $allowedtags['h6'] = array(); $allowedtags['u'] = array(); $allowedtags['center'] = array(); $allowedtags['big'] = array(); $allowedtags['small'] = array(); $allowedtags['tt'] = array(); $allowedtags['br'] = array(); $allowedtags['p'] = array(); $allowedtags['dl'] = array(); $allowedtags['dt'] = array(); $allowedtags['dd'] = array(); $allowedtags['ul'] = array(); $allowedtags['ul']['style'] = array(); $allowedtags['li'] = array(); $allowedtags['li']['style'] = array(); $allowedtags['ol'] = array(); $allowedtags['ol']['style'] = array(); $allowedtags['font'] = array(); $allowedtags['font']['size'] = array(); $allowedtags['font']['color'] = array(); $allowedtags['font']['face'] = array(); } add_action( 'bp_activity_allowed_tags', 'my_allowed_tags', 10 );
One thing I noticed from my example above is that it only works for new posts to the activity stream. My goal was to weed out image tags because I don’t want pictures to display. However, if a prior entry has an image tag it still seems to get through and the picture is displayed. My other solution didn’t solve that problem either.
I’d be okay with pictures displaying if I could somehow create thumbnails that would link to the image. Otherwise images cause the page to load slowly, and for some odd reason some people feel the need to post pictures of every meal they eat…disgusting!
- The topic ‘Disable HTML in activity updates’ is closed to new replies.