[wp-trac] [WordPress Trac] #25143: Appending registered query vars to home URL sets is_home to true when should be false

WordPress Trac noreply at wordpress.org
Fri Apr 3 14:30:10 UTC 2015


#25143: Appending registered query vars to home URL sets is_home to true when
should be false
-------------------------------------+-----------------------------
 Reporter:  mordauk                  |       Owner:
     Type:  defect (bug)             |      Status:  reopened
 Priority:  normal                   |   Milestone:  Future Release
Component:  Query                    |     Version:  3.6
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:
-------------------------------------+-----------------------------

Comment (by mordauk):

 Allowing `add_rewrite_endpoint()` to work without registering a query var
 would also work for this. I hadn't thought of that approach!

 I do still think, however, that the behavior of setting `is_home` to
 `true` when any customer query var is present is wrong, but at least
 having a way around it would be a significant improvement.

 Right now, to get around it, we have to do things like this:


 {{{
 add_action( 'init', array( $this, 'rewrites' ) );
 add_action( 'pre_get_posts', array( $this, 'unset_query_arg' ) );
 add_action( 'redirect_canonical', array( $this,
 'prevent_canonical_redirect' ), 0, 2 );

 /**
  * Registers the rewrite rules for pretty affiliate links
  *
  * @since 1.3
  */
 public function rewrites() {
         add_rewrite_endpoint( $this->get_referral_var(), EP_ALL );
 }
 /**
  * Removes our tracking query arg so as not to interfere with the WP
 query, see https://core.trac.wordpress.org/ticket/25143
  *
  * @since 1.3.1
  */
 public function unset_query_arg( $query ) {
         if ( is_admin() || ! $query->is_main_query() ) {
                 return;
         }
         $key = affiliate_wp()->tracking->get_referral_var();
         $ref = $query->get( $key );
         if ( ! empty( $ref ) ) {
                 $this->referral = $ref;
                 // unset ref var from $wp_query
                 $query->set( $key, null );
                 global $wp;
                 // unset ref var from $wp
                 unset( $wp->query_vars[ $key ] );
                 // if in home (because $wp->query_vars is empty) and
 'show_on_front' is page
                 if ( empty( $wp->query_vars ) && get_option(
 'show_on_front' ) === 'page' ) {
                         // reset and re-parse query vars
                         $wp->query_vars['page_id'] = get_option(
 'page_on_front' );
                         $query->parse_query( $wp->query_vars );
                 }
         }
 }
 /**
  * Filters on canonical redirects
  *
  * @since 1.4
  * @return string
  */
 public function prevent_canonical_redirect( $redirect_url, $requested_url
 ) {
         if( ! is_front_page() ) {
                 return $redirect_url;
         }
         $key = affiliate_wp()->tracking->get_referral_var();
         $ref = get_query_var( $key );
         if( ! empty( $ref ) || false !== strpos( $requested_url, $key ) )
 {
                 $redirect_url = $requested_url;
         }
         return $redirect_url;
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/25143#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list