[wp-trac] [WordPress Trac] #22110: Check for current conditional

WordPress Trac wp-trac at lists.automattic.com
Fri Oct 5 17:12:19 UTC 2012


#22110: Check for current conditional
-----------------------------+-------------------------
 Reporter:  mintindeed       |       Type:  enhancement
   Status:  new              |   Priority:  normal
Milestone:  Awaiting Review  |  Component:  General
  Version:  trunk            |   Severity:  normal
 Keywords:                   |
-----------------------------+-------------------------
 More than once I've needed a programmatic way to determine what page is
 being displayed, for example when implementing event tracking recently:


 {{{
 add_action( 'wp_enqueue_scripts', function() {
         wp_enqueue_script( 'my-site', get_template_directory_uri() .
 '/js/mysite.js', array( 'jquery' ), '1', true );

         // Figure out what type of page we're on
         $is = '';
         foreach ( $GLOBALS['wp_query'] as $key => $value ) {
                 if ( true === $value && 'is_' === substr( $key, 0, 3 ) ) {
                         $is = substr( $key, 3, strlen($key) );
                         break;
                 }
         }
         wp_localize_script( 'my-site', 'event_tracking', array(
                 'category' => $is,
         ) );
 } );

 }}}
 And then used like:

 {{{
 <a onclick="_gaq.push(['_trackEvent', event_tracking.category, 'more-
 stories', 'click']);">Action</a>
 }}}

 Now granted the above way to determine the current page is a little janky,
 and has at least one flaw (on custom post type archives because both
 "is_archive" and "is_post_type_archive" are set, and it will find
 "is_archive" first, every time).  TBH this was me with a bowl of chili in
 one hand trying to avoid typing out "if ( is_* ) { } elseif ( is_* ) { }
 elseif ( is_* ) {} ...".

 A saner function would probably explicitly check each conditional, which
 would make it a little more tedious but also more maintainable:

 {{{
 function where_am_i( $return = 'simple' ) {
         global $wp_query;

         if ( ! isset( $wp_query ) ) {
                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags
 do not work before the query is run. Before then, they always return
 false.' ), '3.5' );
                 return false;
         }

         $primary = null;
         $secondary = null;
         $tertiary = null;

         // Tried to maintain the hierarchy from query.php
         if ( true === $wp_query->is_robots ) {
                 $primary = 'robots';
         } elseif ( true === $wp_query->is_attachment ) {
                 $primary = 'attachment';
                 $secondary = 'single';
                 $tertiary = 'singular';
         } elseif ( true === $wp_query->is_page ) {
                 $primary = 'page';
                 $tertiary = 'singular';
         } elseif ( true === $wp_query->is_single ) {
                 $primary = 'single';
                 $secondary = 'singular';
         } elseif ( true === $wp_query->is_search ) {
                 $primary = 'search';
         } elseif ( true === $wp_query->is_time ) {
                 $primary = 'time';
                 $secondary = 'date';
                 $tertiary = 'archive';
         } elseif ( true === $wp_query->is_day ) {
                 $primary = 'day';
                 $secondary = 'date';
                 $tertiary = 'archive';
         } elseif ( true === $wp_query->is_month ) {
                 $primary = 'month';
                 $secondary = 'date';
                 $tertiary = 'archive';
         } elseif ( true === $wp_query->is_year ) {
                 $primary = 'year';
                 $secondary = 'date';
                 $tertiary = 'archive';
         } elseif ( true === $wp_query->is_date ) {
                 $primary = 'date';
                 $secondary = 'archive';
         } elseif ( true === $wp_query->is_category ) {
                 $primary = 'category';
                 $secondary = 'archive';
         } elseif ( true === $wp_query->is_tag ) {
                 $primary = 'tag';
                 $secondary = 'archive';
         } elseif ( true === $wp_query->is_tax ) {
                 $primary = 'custom_taxonomy';
                 $secondary = 'archive';
         } elseif ( true === $wp_query->is_author ) {
                 $primary = 'author';
                 $secondary = 'archive';
         } elseif ( true === $wp_query->is_post_type_archive ) {
                 $primary = 'post_type';
                 $secondary = 'archive';
         } elseif ( true === $wp_query->is_feed ) {
                 $primary = 'feed';
         } elseif ( true === $wp_query->is_trackback ) {
                 $primary = 'trackback';
         } elseif ( true === $wp_query->is_comments_popup ) {
                 $primary = 'comments_popup';
                 $secondary = 'comments';
         } elseif ( true === $wp_query->is_preview ) {
                 $primary = 'preview';
                 $secondary = 'singular';
         } elseif ( true === $wp_query->is_admin ) {
                 $primary = 'admin';
         } elseif ( true === $wp_query->is_comment_feed ) {
                 $primary = 'comment_feed';
                 $secondary = 'comments';
                 $tertiary = 'feed';
         } elseif ( true === $wp_query->is_404 ) {
                 $primary = '404';
                 $secondary = 'singular';
         } elseif ( true === $wp_query->is_home ) {
                 $primary = 'home';
                 if ( true === $wp_query->is_posts_page ) {
                         $secondary = 'posts_page';
                         $tertiary = 'page';
                 } else {
                         $secondary = 'static_page';
                 }
         }

         if ( 'detailed' === $return ) {
                 $details = compact( $primary, $secondary, $tertiary );
                 $details = array_filter( $details ); // Remove empty
                 return $details;
         }

         // 'simple' === $return
         return $primary;
 }
 }}}

 Should it use $wp_the_query so that it always acts on the main query?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/22110>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list