Skip to content

Instantly share code, notes, and snippets.

@shanebp
Last active May 13, 2016 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanebp/dd4173382e6b0d659482fb0622726cc4 to your computer and use it in GitHub Desktop.
Save shanebp/dd4173382e6b0d659482fb0622726cc4 to your computer and use it in GitHub Desktop.
Add a custom tab to Members Directory and load custom template
<?php
// add tab 'Testo' on Members Directory Page
function asdf_testo_tab() {
if ( ! bp_current_component( 'members' ) )
return;
if ( bp_is_current_action( 'testo' ) )
return;
$button_args = array(
'id' => 'testo',
'component' => 'members',
'link_text' => __( 'Testo', 'bp' ),
'link_title' => __( 'Testo', 'bp' ),
'link_class' => 'testo no-ajax',
'link_href' => trailingslashit( bp_get_members_directory_permalink() . 'testo' ),
'wrapper' => false,
'block_self' => false,
);
?>
<li><?php echo bp_get_button( apply_filters( 'bp_get_testo_map_button', $button_args ) ); ?></a></li>
<?php
}
add_action( 'bp_members_directory_member_types', 'asdf_testo_tab' );
// load the custom template
function asdf_testo_show_map() {
// When clicking on the 'Testo' tab on the Members Directory, bp_is_members_component() will always be false - Why?
// The equivalent conditionals work properly in Groups - why?
// bp_is_groups_component() will always be true in this approach:
// https://codex.buddypress.org/add-custom-tab-to-groups-directory/
// What is the reason for the discrepancy ?
if ( bp_is_members_component() )
echo 'this is the members component';
else
echo 'this is NOT the members component';
// Because we cannot use the preferred conditionals - instead parse the url
// This works. The template is loaded.
// But this error is thrown:
// Notice: Trying to get property of non-object in .../wp-includes/query.php on line 4520
// unless we manually set the current component
// Why?
$requested_url = bp_get_requested_url();
if( ( strpos( $requested_url, BP_MEMBERS_SLUG ) !== false ) && ( strpos( $requested_url, 'testo' ) !== false ) ) {
// manually set current component to 'members'
$bp = buddypress();
$bp->current_component = BP_MEMBERS_SLUG;
new BP_Members_Theme_Compat_Testo();
bp_core_load_template( 'members/testo' );
}
}
add_action( 'bp_actions', 'asdf_testo_show_map' );
class BP_Members_Theme_Compat_Testo {
public function __construct() {
add_action( 'bp_setup_theme_compat', array( $this, 'is_testo' ) );
}
public function is_testo() {
add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'directory_dummy_post' ) );
add_filter( 'bp_replace_the_content', array( $this, 'directory_content' ) );
}
public function directory_dummy_post() {
bp_theme_compat_reset_post( array(
'ID' => 0,
'post_title' => 'Testo',
'post_author' => 0,
'post_date' => 0,
'post_content' => '',
'post_type' => 'page',
'post_status' => 'publish',
'is_page' => true,
'comment_status' => 'closed'
) );
}
public function directory_content() {
return bp_buffer_template_part( 'members/testo', null, false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment