Guest User

Untitled

a guest
Apr 16th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. I had created a custom taxonomy "group", and new post type named "topic". there are some terms under custom taxonomy "group".
  2.  
  3. Use the following code, i can create a new entry from frontend in category. But it doesn't work in my custom taxonomy.
  4.  
  5. Code:
  6.  
  7. <?php
  8. /**
  9. * Display the create new topic link on category page.
  10. */
  11. function my_new_topic_link( $text = '', $before = '', $after = '') {
  12. if ( ! is_user_logged_in() )
  13. return;
  14.  
  15. global $wp_query;
  16. $node_id = $wp_query->get_queried_object_id();
  17.  
  18. if ( is_category() ) {
  19. $url = my_get_page_url_by_slug( 'new', 'node=' . $node_id );
  20. } else {
  21. $url = my_get_page_url_by_slug( 'new' );
  22. }
  23.  
  24. if ( '' == $text )
  25. $text = __( 'Create New Topic' );
  26.  
  27. $link = $before . '<a rel="nofollow" class="btn" href="' . $url . '">' . $text . '</a>' . $after;
  28.  
  29. echo $link;
  30. }
  31.  
  32. /**
  33. * The create new topic form.
  34. */
  35. function my_new_topic_form() {
  36. ob_start();
  37.  
  38. my_error_messages();
  39. ?>
  40. <form id="vp-new-topic" action="" method="post">
  41. <fieldset>
  42. <?php if ( empty( $_GET['node'] ) ) { ?>
  43. <p><?php wp_dropdown_categories( array( 'show_option_all' => __( 'Select Node' ) ) ); ?></p>
  44. <?php } ?>
  45. <p><input type="text" name="topic_title" placeholder="<?php _e( 'Topic title here' ); ?>" id="topic_title" class="form-field wider" value="" /></p>
  46. <p><textarea name="topic_content_filtered" id="topic_content_filtered" class="form-field wider" rows="10" cols="20" ></textarea></p>
  47. <p>
  48. <input type="hidden" name="action" value="new_topic" />
  49. <input type="hidden" name="new_topic_nonce" value="<?php echo wp_create_nonce( 'new-topic-nonce' ); ?>" />
  50. <input type="submit" name="submit" class="btn" value="<?php _e( 'Create Topic' ); ?>" />
  51. </p>
  52. </fieldset>
  53. </form>
  54. <?php
  55. echo ob_get_clean();
  56. }
  57.  
  58. /**
  59. * Do the create new topic form process.
  60. */
  61. function my_do_create_new_topic() {
  62. if ( empty( $_POST['action'] ) || ( 'new_topic' != $_POST['action'] ) )
  63. return;
  64.  
  65. if ( isset( $_POST['topic_title'] ) && wp_verify_nonce( $_POST['new_topic_nonce'], 'new-topic-nonce' ) ) {
  66. if ( !empty( $_GET['node'] ) )
  67. $node_id = (int) $_GET['node'];
  68. else
  69. $node_id = $_POST['cat'];
  70.  
  71. $title = $_POST['topic_title'];
  72. $content_filtered = strip_tags( $_POST['topic_content_filtered'] );
  73. $user_id = get_current_user_id();
  74.  
  75. // Node empty
  76. if ( empty( $node_id ) || ( 0 == $node_id ) ) {
  77. my_errors()->add( 'node_empty', __( 'Please select a node' ) );
  78. }
  79.  
  80. // Node id invalid
  81. if ( !empty( $node_id ) ) {
  82. $node = get_term_by( 'id', $node_id, 'category');
  83. if ( !$node ) {
  84. my_errors()->add( 'node_invalid', 'Please do not cheating me.' );
  85. }
  86. }
  87.  
  88. // Title empty
  89. if ( empty( $title ) ) {
  90. my_errors()->add( 'topic_title_empty', __( 'Please enter the topic title' ) );
  91. }
  92.  
  93. // Content empty
  94. if ( empty( $content_filtered ) ) {
  95. my_errors()->add( 'topic_content_empty', __( 'Please write something useful' ) );
  96. }
  97.  
  98. $errors = my_errors()->get_error_messages();
  99. if( empty( $errors ) ) {
  100. $data = array(
  101. 'post_author' => $user_id,
  102. 'post_category' => (array) $node_id,
  103. 'post_title' => $title,
  104. 'post_content_filtered' => $content_filtered,
  105. 'post_status' => 'publish'
  106. );
  107.  
  108. $data['post_content'] = $content_filtered;
  109.  
  110. $topic = wp_insert_post( $data );
  111.  
  112. if ( $topic ) {
  113. $topic_url = get_permalink( $topic );
  114. wp_redirect( $topic_url );
  115. exit;
  116. }
  117. }
  118. }
  119. }
  120. add_action( 'template_redirect', 'my_do_create_new_topic' );
  121.  
  122. /**
  123. * The topic edit links.
  124. */
  125. function my_edit_topic_links() {
  126. $topic_id = get_the_ID();
  127.  
  128. if ( !is_user_logged_in() || !current_user_can( 'edit_post', $topic_id ) )
  129. return;
  130.  
  131. $pub_time = get_the_time( 'U' );
  132. $time_diff = abs( current_time( 'timestamp' ) - $pub_time );
  133.  
  134. // Cannot edit topic 15mins after created
  135. if ( 900 < $time_diff )
  136. return;
  137.  
  138. $edit_url = add_query_arg( 'edit', 'true', get_permalink() );
  139. $link = '<span class="topic-edit">';
  140. $link .= '<a rel="nofollow" href="' . $edit_url . '">' . __( 'edit' ) . '</a>';
  141. $link .= '</span>';
  142.  
  143. echo $link;
  144. }
  145.  
  146. /**
  147. * If the current page the edit topic page.
  148. */
  149. function my_is_edit() {
  150. if ( is_single() && isset( $_GET['edit'] ) && 'true' == $_GET['edit'] )
  151. return true;
  152.  
  153. return false;
  154. }
  155.  
  156. /**
  157. * The edit topic form.
  158. */
  159. function my_edit_topic_form() {
  160. $pub_time = get_the_time( 'U' );
  161. $time_diff = abs( current_time( 'timestamp' ) - $pub_time );
  162. // Cannot edit topic 15mins after created
  163. if ( 900 < $time_diff ) {
  164. echo '<p>' . __( 'You cannot edt this topic 15 minites after created.' ) . '</p>';
  165. return;
  166. }
  167.  
  168. ob_start();
  169. $topic_id = get_query_var( 'p' );
  170. $topic = get_post( $topic_id );
  171.  
  172. my_error_messages();
  173. ?>
  174. <form id="vp-edit-topic" action="" method="post">
  175. <fieldset>
  176. <p><input type="text" name="topic_title" id="topic_title" class="form-field wider" value="<?php echo $topic->post_title ?>" /></p>
  177. <p><textarea name="topic_content_filtered" id="topic_content_filtered" class="form-field wider" rows="10" cols="20" ><?php echo $topic->post_content_filtered; ?></textarea></p>
  178. <p>
  179. <input type="hidden" name="action" value="edit_topic" />
  180. <input type="hidden" name="edit_topic_nonce" value="<?php echo wp_create_nonce( 'edit-topic-nonce' ); ?>" />
  181. <input type="submit" name="submit" class="btn" value="<?php _e( 'Submit Changes' ); ?>" />
  182. </p>
  183. </fieldset>
  184. </form>
  185. <?php
  186. echo ob_get_clean();
  187. }
  188.  
  189. /**
  190. * Do the edit topic form process.
  191. */
  192. function my_do_edit_topic() {
  193. if ( empty( $_POST['action'] ) || ( 'edit_topic' != $_POST['action'] ) )
  194. return;
  195.  
  196. if ( isset( $_POST['topic_title'] ) && wp_verify_nonce( $_POST['edit_topic_nonce'], 'edit-topic-nonce' ) ) {
  197. $topic_id = get_query_var( 'p' );
  198. $title = $_POST['topic_title'];
  199. $content_filtered = strip_tags( $_POST['topic_content_filtered'] );
  200.  
  201. // Title empty
  202. if ( empty( $title ) ) {
  203. my_errors()->add( 'topic_title_empty', __( 'Please enter the topic title' ) );
  204. }
  205.  
  206. // Content empty
  207. if ( empty( $content_filtered ) ) {
  208. my_errors()->add( 'topic_content_empty', __( 'Please write something useful' ) );
  209. }
  210.  
  211. $errors = my_errors()->get_error_messages();
  212. if( empty( $errors ) ) {
  213. $data = array(
  214. 'ID' => $topic_id,
  215. 'post_title' => $title,
  216. 'post_content_filtered' => $content_filtered,
  217. );
  218.  
  219. $data['post_content'] = $content_filtered;
  220.  
  221. $topic = wp_update_post( $data );
  222.  
  223. if ( $topic ) {
  224. $topic_url = get_permalink( $topic );
  225. wp_redirect( $topic_url );
  226. exit;
  227. }
  228. }
  229. }
  230. }
  231. add_action( 'template_redirect', 'my_do_edit_topic' );
Advertisement
Add Comment
Please, Sign In to add comment