Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: List User Posts
- Plugin URI: http://en.bainternet.info
- Description: lists user posts on the front end
- Version: 0.1
- Author: Bainternet
- Author URI: http://en.bainternet.info
- */
- if (!class_exists('list_user_posts')){
- /**
- * list_user_posts
- * @author Ohad Raz
- */
- class list_user_posts
- {
- /**
- * __construct class constructor
- *
- * @author Ohad Raz
- * @param array $args
- */
- function __construct($args = array())
- {
- add_shortcode('user_posts', array($this,list_user_posts));
- }
- /**
- * list_user_posts shortcode handler
- *
- * @author Ohad Raz
- * @param array $attr shortcode attributes
- * @param string $content shortcode content
- * @return string
- */
- public function list_user_posts($attr = array(), $content = null)
- {
- extract(shortcode_atts(array(
- 'post_type' => 'event',
- 'number' => 10,
- ), $attr));
- //if the user is not logged in the give him a link to log in
- if (!is_user_logged_in()){
- return sprintf(__('You Need to <a href="%s">Login</a> to see your events'),wp_login_url(get_permalink()));
- }
- //this is for pagination
- $pagenum = isset( $_GET['pagenum'] ) ? intval( $_GET['pagenum'] ) : 1;
- //get user's posts
- $args = array(
- 'author' => get_current_user_id(), //this makes the query pull post form the current user only
- 'post_status' => array('draft', 'future', 'pending', 'publish'),
- 'post_type' => $post_type,
- 'posts_per_page' => $number,
- 'paged' => $pagenum
- );
- $user_posts = new WP_Query( $args );
- $retVal = '';
- if ( $user_posts->have_posts() ) {
- //set table headers
- $retVal = '
- <table class="user-posts-table" cellpadding="0" cellspacing="0">
- <thead>
- <tr>
- <th>'.__( 'Title', 'lup' ).'</th>
- <th>'.__( 'Status', 'lup' ).'</th>
- <th>'.__( 'Action', 'lup' ).'</th>
- </tr>
- </thead>
- <tbody>';
- //loop over and add each post to the table
- global $post;
- $temp = $post;
- while ($user_posts->have_posts()){
- $user_posts->the_post();
- $title = $post->post_title;
- $link = '<a href="'.get_permalink().'" title="'.sprintf( esc_attr__( 'Permalink to %s', 'lup' ), the_title_attribute( 'echo=0' ) ).'" rel="bookmark">'.$title.'</a>';
- $retVal .=
- '<tr>
- <td>
- '.( in_array( $post->post_status, array('published', 'pending') ) ? $title : $link).'
- </td>
- <td>
- '.$post->post_status .'
- </td>
- <td>
- <a href="'.get_permalink().'edit-event/"><span style="color: green;">'. __( 'Edit', 'lup' ).'</span></a>
- </td>
- </tr>';
- }
- $retVal .= '</tbody></table>';
- //create pagination (if needed)
- if ($user_posts->found_posts > $number ){
- $pagination = paginate_links( array(
- 'base' => add_query_arg( 'pagenum', '%#%' ),
- 'format' => '',
- 'prev_text' => __( '«', 'lup' ),
- 'next_text' => __( '»', 'lup' ),
- 'total' => $user_posts->max_num_pages,
- 'current' => $pagenum
- )
- );
- if ( $pagination ) {
- $retVal .= '<div class="pagination">'.$pagination .'</div>';
- }
- }
- //return table of posts
- return $retVal;
- }else{
- // no posts for this users found
- return __("No Events Found");
- }
- }
- }//end list_user_posts class
- }//end if
- new list_user_posts();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement