Skip to content

Instantly share code, notes, and snippets.

@danbp
Created September 29, 2015 09:21
Show Gist options
  • Save danbp/342c08eb8be9432ae97f to your computer and use it in GitHub Desktop.
Save danbp/342c08eb8be9432ae97f to your computer and use it in GitHub Desktop.
BuddyPress - Add posts and comments on separate profile tabs
/**
* this goes into bp-custom.php or can be changed to a plugin
* tested with 2012 , BP 2.3.3 and WP 4.3.3
*/
// building navigation tabs
function bpfr_post_profile_setup_nav() {
global $bp;
$parent_slug = 'contributions';
$child_slug = 'posts';
// main nav
bp_core_new_nav_item( array(
'name' => __( 'Contributions' ),
'slug' => $parent_slug,
'screen_function' => 'bpfr_profile_post_screen',
'position' => 40,
'default_subnav_slug' => $child_slug
) );
// Add subnav item 1
bp_core_new_subnav_item( array(
'name' => __( 'My posts' ),
'slug' => $child_slug,
'parent_url' => $bp->displayed_user->domain . $parent_slug.'/',
'parent_slug' => $parent_slug,
'screen_function' => 'bpfr_profile_post_screen'
) );
// Add subnav item 2
bp_core_new_subnav_item( array(
'name' => __( 'My Comments' ),
'slug' => 'comments',
'parent_url' => $bp->displayed_user->domain . $parent_slug.'/',
'parent_slug' => $parent_slug,
'screen_function' => 'bpfr_profile_comment_screen'
) );
}
// calling templates
function bpfr_profile_post_screen() {
add_action( 'bp_template_content', 'bpfr_profile_post_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function bpfr_profile_comment_screen() {
add_action( 'bp_template_content', 'bpfr_profile_comment_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
// adding action hooks to template
function bpfr_profile_post_screen_content() {
do_action( my_profile_post);
}
function bpfr_profile_comment_screen_content() {
do_action( my_profile_comments);
}
add_action( 'bp_setup_nav', 'bpfr_post_profile_setup_nav' );
// fetching content
function bpfr_get_post() {
global $bp, $post;
$paged = ( isset( $_GET['pubs'] ) ) ? $_GET['pubs'] : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'author' => bp_displayed_user_id(),
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
?><ul style="list-style-type: none;">
<?php
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
?></ul><?php
echo ggsalas_profile_pagination( $wp_query );
}
add_action ( 'my_profile_post', 'bpfr_get_post' );
function ggsalas_profile_pagination( $wp_query ) {
$profile_page_links = paginate_links( array(
'base' => @add_query_arg('pubs','%#%'),
'format' => '?pubs=%#%',
'total' => ceil( (int) $wp_query->found_posts / (int) get_query_var('posts_per_page') ),
'current' => (int) get_query_var('paged'),
'show_all' => true,
'type' => 'plain'
));
// Output pagination
return $profile_page_links;
}
function bpfr_get_comments() {
// number of comments to show per page
define( 'DEFAULT_SHOW_PER_PAGE', 3 );
$page = (get_query_var('page')) ? get_query_var('page') : 1;
$limit = DEFAULT_SHOW_PER_PAGE;
$offset = ($page * $limit) - $limit;
$param = array(
'user_id' => bp_displayed_user_id(),
'status' => 'approve',
'offset' => $offset,
'number' => $limit
);
$total_comments = get_comments(array(
'orderby' => 'comment_date',
'order' => 'ASC',
'status' => 'approve',
'parent' => 0
));
$pages = ceil(count($total_comments)/DEFAULT_SHOW_PER_PAGE);
$comments = get_comments( $param );
$args = array(
'base' => @add_query_arg('page','%#%'),
'format' => '?page=%#%',
'total' => $pages,
'current' => $page,
'show_all' => false,
'mid_size' => 4,
'prev_next' => true,
'prev_text' => __('&laquo; backward'),
'next_text' => __('onward &raquo;'),
'type' => 'plain'
);
// Output pagination
echo paginate_links( $args );
?>
<ul>
<?php
foreach($comments as $comment) {
// short version
//echo '<li>'. ($comment->comment_content .'&nbsp;<a href="'. esc_url( get_permalink( $comment->comment_post_ID ) ) ).'">View post</a></li>';
// long version
echo '<li>'. ($comment->comment_content .'&nbsp;<a href="'. esc_url( get_permalink( $comment->comment_post_ID ) ) ).'">'. get_the_title( $comment->comment_post_ID ) .'</a> Comment: '. get_comment_date( '', $comment ) .'</li>';
}
?>
</ul>
<?php
}
add_action ( 'my_profile_comments', 'bpfr_get_comments' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment