Skip to content

Instantly share code, notes, and snippets.

@imath
Created February 2, 2013 18:09
Show Gist options
  • Save imath/4698633 to your computer and use it in GitHub Desktop.
Save imath/4698633 to your computer and use it in GitHub Desktop.
In reply to this topic : http://bp-fr.net/groupes/utilisation-configuration-optimisation-de-buddypress/forum/topic/comment-ajouter-un-nouveau-critere-de-tri-aux-topic/ . Adding a new nav item to BuddyPress Forum dir (in case of bbPress 1.2 powered forums..) for a given topic tag.
<?php
/*** Beginning of the code to paste into the functions.php of your BuddyPress theme / child theme ***/
/**
* Hooks bp_forums_directory_group_types and adds a forum directory nav item
*
* @global $wp_taxonomy_object
* @uses $wp_taxonomy_object->get_term_by() to fetch tag datas by name
* @uses bb_get_tag_link() to get permalink for the given tag id.
* @author imath
*/
function jose_adds_forum_tag_nav() {
global $wp_taxonomy_object;
/* the name of your bbPress (1.2 for BuddyPress group forums) tag */
$tag = 'support';
/* get tag data */
$bb_term_object = $wp_taxonomy_object->get_term_by( 'name', $tag, 'bb_topic_tag' );
/* number of topics that uses this tag */
$count = $bb_term_object->count;
/* permalink to tag */
$tag_link = bb_get_tag_link( intval( $bb_term_object->term_id ) );
?>
<li id="forum-dir-tag" class="no-ajax"><a href="<?php echo trailingslashit( $tag_link ); ?>" data-tagname="support">support <span class="tag-count"><?php echo $count;?></span></a></li>
<?php
}
add_action( 'bp_forums_directory_group_types', 'jose_adds_forum_tag_nav' );
/**
* Hooks bp_after_directory_forums_page to inject some javascript
*
* In bp-default/_inc/ global.js launch ajax actions that are catched by ajax.php
* In previous jose_adds_forum_tag_nav we added a no-ajax class to li#forum-dir-tag & in order
* to avoid global.js to run its script, the following script will use global.js bp_ajax_request
* to load the forum template for the given tag request.
* @author imath
*/
function jose_custom_js() {
?>
<script type="text/javascript">
/* <![CDATA[ */
jQuery(document).ready(function($){
$('.item-list-tabs ul li a').click( function(){
if ( $(this).parent().attr('id') == 'forum-dir-tag' ) {
var li = $(this).parent();
var ulparent = li.parent();
// let's remove any selected li
ulparent.find('li').each( function(){
if( $(this).hasClass('selected') )
$(this).removeClass('selected');
});
//let's add our li as selected and loading..
li.addClass('selected current');
li.addClass('loading');
// the tag to request
var tagname = $(this).attr('data-tagname');
if ( bp_ajax_request )
bp_ajax_request.abort();
bp_ajax_request = jq.post( ajaxurl, {
action: 'forums_filter',
'cookie': encodeURIComponent(document.cookie),
'object': 'forums',
'filter': 'tags',
'search_terms': tagname,
'scope': jq.cookie('bp-forums-scope'),
'page': 1,
'extras': jq.cookie('bp-forums-extras')
},
function(response)
{
jq('div.forums').fadeOut( 100, function() {
jq(this).html(response);
jq(this).fadeIn(100);
});
jq('.item-list-tabs li.selected').removeClass('loading');
});
return false;
} else {
$('#forum-dir-tag').removeClass('selected current');
}
});
});
/* ]]> */
</script>
<?php
}
add_action( 'bp_after_directory_forums_page', 'jose_custom_js' );
/*** end of the code to paste ***/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment