Skip to content

Instantly share code, notes, and snippets.

@brichards
Created February 28, 2016 23:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brichards/eb63ef260ceba3cf575a to your computer and use it in GitHub Desktop.
Save brichards/eb63ef260ceba3cf575a to your computer and use it in GitHub Desktop.
In all of the below cases permalinks have been flushed. The endpoint works perfectly fine for all scenarios _except_ the home page.
Using only this, hiting ^/bpa/whatever/ serves blog archive instead of static front page.
<?php
/**
* Create url endpoint for BPA
*
* @since 1.0.0
*/
function cfpurl_rewrite_rules() {
add_rewrite_tag( '%bpa%', '([^&]+)' );
add_rewrite_endpoint( 'bpa', EP_ALL );
}
add_action( 'init', 'cfpurl_rewrite_rules' );
Using only this, hitting ^/bpa/whatever/ redirects to ^/
<?php
/**
* Create url endpoint for BPA
*
* @since 1.0.0
*/
function cfpurl_rewrite_rules() {
add_rewrite_tag( '%bpa%', '([^&]+)' );
add_rewrite_endpoint( 'bpa', EP_ALL );
$homepage = get_option( 'page_on_front' );
add_rewrite_rule( '^bpa(/(.*))?/?', 'index.php?p=' . $homepage . '&bpa=$matches[2]', 'top' );
}
Using only this, hitting ^/bpa/whatever/ redirects to ^/
<?php
/**
* Create url endpoint for BPA
*
* @since 1.0.0
*/
function cfpurl_rewrite_rules() {
add_rewrite_tag( '%bpa%', '([^&]+)' );
add_rewrite_endpoint( 'bpa', EP_ALL );
}
add_action( 'init', 'cfpurl_rewrite_rules' );
function cfpurl_home_query_update() {
if ( is_admin() ) {
return;
}
global $wp_query, $wp_the_query;
remove_action( 'parse_query', 'cfpurl_home_query_update', 11 );
// If we have a homepage and should be showing it, we have to
// completely reset the query and make sure it's handles as
// the "main" query, for it to recognize this properly without
// a redirect.
if ( 0 === strpos( $_SERVER['REQUEST_URI'], '/bpa/' ) ) {
$homepage = get_option( 'page_on_front' );
if ( $homepage ) {
$wp_query = $wp_the_query = new WP_Query( array(
'bpa' => get_query_var( 'bpa' ),
'page_id' => $homepage,
) );
}
}
}
add_action( 'parse_query', 'cfpurl_home_query_update', 11 );
@hirasso
Copy link

hirasso commented Jul 12, 2017

Do you know anything about if this was fixed? I still have this problem...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment