[wp-trac] [WordPress Trac] #16303: Improve documentation and usability of WP_Rewrite Endpoint support

WordPress Trac wp-trac at lists.automattic.com
Wed Jan 19 16:27:46 UTC 2011


#16303: Improve documentation and usability of WP_Rewrite Endpoint support
---------------------------------+-----------------------------
 Reporter:  westi                |       Owner:  westi
     Type:  defect (bug)         |      Status:  new
 Priority:  normal               |   Milestone:  Future Release
Component:  Rewrite Rules        |     Version:  3.1
 Severity:  normal               |  Resolution:
 Keywords:  3.2-early has-patch  |
---------------------------------+-----------------------------

Comment (by westi):

 A json endpoint example:

 {{{
 function json_init() {
         add_rewrite_endpoint( 'json', EP_ROOT ^ EP_PERMALINK ^
 EP_ALL_ARCHIVES );
 }

 add_action( 'init', 'json_init' );

 function json_request( $query_vars ) {
         if ( isset( $query_vars['json'] ) )
                 $query_vars['json'] = true;

         return $query_vars;
 }

 add_filter( 'request', 'json_request' );

 function json_template_redirect() {
         global $wp_query, $post, $comment;

         if ( ! get_query_var( 'json' ) )
                 return true;

         $data = array();
         $post_keys = array( 'ID', 'post_author', 'post_title',
 'post_content', 'post_date_gmt', 'post_status', 'post_name', 'post_type',
 'comment_count' );
         $comment_keys = array( 'comment_ID', 'comment_author',
 'comment_content', 'comment_date_gmt', 'comment_type' );

         while ( have_posts() ) {
                 the_post();

                 foreach ( $post_keys as $_key )
                         $_post[$_key] = $post->$_key;

                 if ( post_type_supports( $_post['post_type'], 'comments' )
 ) {
                         $wp_query->comments = get_comments(
 array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
                         $wp_query->comment_count =
 count($wp_query->comments);
                         update_comment_cache($wp_query->comments);

                         $_post['comments'] = array();

                         while ( have_comments() ) {
                                 the_comment();
                                 foreach ( $comment_keys as $_key )
                                         $_comment[$_key] =
 $comment->$_key;

                                 $_post['comments'][
 $_comment['comment_type'] . '-' . $_comment['comment_ID'] ] = $_comment;
                         }
                 }

                 $data[ $_post['post_type'] . '-' . $_post['ID'] ] =
 $_post;
         }

         die( json_encode( $data ) );
 }
 add_action( 'template_redirect', 'json_template_redirect' );
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/16303#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list