Skip to content

Instantly share code, notes, and snippets.

@mzdebo
Created October 21, 2020 03:03
Show Gist options
  • Save mzdebo/7eabc9ed8d1237bc350b2d006af9201e to your computer and use it in GitHub Desktop.
Save mzdebo/7eabc9ed8d1237bc350b2d006af9201e to your computer and use it in GitHub Desktop.
Youzer - Add shortcode to display current user posts in a custom tab
<?php
/**
* Author: Chanel via https://thehorsebc.world
*/
/**
* How to Use this
*
* Adapted for use with Youzer plugin via https://www.kainelabs.com
* Use any front-end post submission form, like WPForms via https://wpforms.com/addons/post-submissions-addon
* Follow instructions for adding new tab via http://kainelabs.com/docs/youzer
* Name Tab Articles
* Choose shortcode tab type
* Please go to your FTP/Cpanel, and go to folder "wp-content/plugins"
* Create a new PHP file and name it "bp-custom.php"
* Put this snippet there and save the file
*/
/**
* Let's add a link to all posts or custom posts for user that will show using the wp standard archive page
*/
function my_custom_shortcode( $atts ) {
global $user_ID;
$user_info = get_userdata($user_ID);
$current_link = get_author_posts_url($user_id,$user_info->user_nicename);
echo '<center><a href="/user-articles" rel="nofollow">View All Member Articles</a></center>';
}
add_shortcode( 'all_userarticles_output', 'all_userarticles_shortcode');
/**
* Here we will add the shortcode to display the user posts in the Youzer custom tab
*/
function members_articles_shortcode($atts, $content = null) {
global $post;
extract(shortcode_atts(array(
'cat' => '',
'num' => '5',
'order' => 'DESC',
'orderby' => 'post_date',
), $atts));
$args = array(
'author' => bp_displayed_user_id(),
'post_status' => 'publish',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'post_type' => '', //Can create a custom post type to display or use standard posts
);
/**
* use theme template parts or standard to output posts
*/
$output = '';
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
$output = get_template_part( 'template-parts/loop', 'video' );
}
/**
* restores original post data
*/
wp_reset_postdata();
return $output ;
}
/**
* shortcode to use in new created Tab Articles is [members_articles]
*/
add_shortcode('members_articles', 'members_articles_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment