Advertisement
Guest User

Untitled

a guest
Aug 10th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: List User Posts
  4. Plugin URI: http://en.bainternet.info
  5. Description: lists user posts on the front end
  6. Version: 0.1
  7. Author: Bainternet
  8. Author URI: http://en.bainternet.info
  9. */
  10.  
  11. if (!class_exists('list_user_posts')){
  12. /**
  13. * list_user_posts
  14. * @author Ohad Raz
  15. */
  16. class list_user_posts
  17. {
  18. /**
  19. * __construct class constructor
  20. *
  21. * @author Ohad Raz
  22. * @param array $args
  23. */
  24. function __construct($args = array())
  25. {
  26. add_shortcode('user_posts', array($this,list_user_posts));
  27. }
  28.  
  29. /**
  30. * list_user_posts shortcode handler
  31. *
  32. * @author Ohad Raz
  33. * @param array $attr shortcode attributes
  34. * @param string $content shortcode content
  35. * @return string
  36. */
  37. public function list_user_posts($attr = array(), $content = null)
  38. {
  39. extract(shortcode_atts(array(
  40. 'post_type' => 'event',
  41. 'number' => 10,
  42. ), $attr));
  43.  
  44. //if the user is not logged in the give him a link to log in
  45. if (!is_user_logged_in()){
  46. return sprintf(__('You Need to <a href="%s">Login</a> to see your events'),wp_login_url(get_permalink()));
  47. }
  48. //this is for pagination
  49. $pagenum = isset( $_GET['pagenum'] ) ? intval( $_GET['pagenum'] ) : 1;
  50.  
  51. //get user's posts
  52. $args = array(
  53. 'author' => get_current_user_id(), //this makes the query pull post form the current user only
  54. 'post_status' => array('draft', 'future', 'pending', 'publish'),
  55. 'post_type' => $post_type,
  56. 'posts_per_page' => $number,
  57. 'paged' => $pagenum
  58. );
  59. $user_posts = new WP_Query( $args );
  60.  
  61. $retVal = '';
  62. if ( $user_posts->have_posts() ) {
  63.  
  64. //set table headers
  65. $retVal = '
  66. <table class="user-posts-table" cellpadding="0" cellspacing="0">
  67. <thead>
  68. <tr>
  69. <th>'.__( 'Title', 'lup' ).'</th>
  70. <th>'.__( 'Status', 'lup' ).'</th>
  71. <th>'.__( 'Action', 'lup' ).'</th>
  72. </tr>
  73. </thead>
  74. <tbody>';
  75. //loop over and add each post to the table
  76. global $post;
  77. $temp = $post;
  78. while ($user_posts->have_posts()){
  79. $user_posts->the_post();
  80. $title = $post->post_title;
  81. $link = '<a href="'.get_permalink().'" title="'.sprintf( esc_attr__( 'Permalink to %s', 'lup' ), the_title_attribute( 'echo=0' ) ).'" rel="bookmark">'.$title.'</a>';
  82. $retVal .=
  83. '<tr>
  84. <td>
  85. '.( in_array( $post->post_status, array('published', 'pending') ) ? $title : $link).'
  86. </td>
  87. <td>
  88. '.$post->post_status .'
  89. </td>
  90. <td>
  91. <a href="'.get_permalink().'edit-event/"><span style="color: green;">'. __( 'Edit', 'lup' ).'</span></a>
  92. </td>
  93. </tr>';
  94. }
  95. $retVal .= '</tbody></table>';
  96.  
  97. //create pagination (if needed)
  98. if ($user_posts->found_posts > $number ){
  99. $pagination = paginate_links( array(
  100. 'base' => add_query_arg( 'pagenum', '%#%' ),
  101. 'format' => '',
  102. 'prev_text' => __( '&laquo;', 'lup' ),
  103. 'next_text' => __( '&raquo;', 'lup' ),
  104. 'total' => $user_posts->max_num_pages,
  105. 'current' => $pagenum
  106. )
  107. );
  108. if ( $pagination ) {
  109. $retVal .= '<div class="pagination">'.$pagination .'</div>';
  110. }
  111. }
  112. //return table of posts
  113. return $retVal;
  114. }else{
  115. // no posts for this users found
  116. return __("No Events Found");
  117. }
  118. }
  119.  
  120. }//end list_user_posts class
  121. }//end if
  122. new list_user_posts();
  123.  
  124. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement