Created
March 4, 2022 21:39
-
-
Save Genyus/f9b53cf3f10996b99e1aaf3fa94fb090 to your computer and use it in GitHub Desktop.
Fix for broken Elementor landing pages when using custom permalinks (v3.4.3+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fixes landing page 404 when non-standard permalinks are enabled. | |
* | |
* @param \WP_Query $query | |
*/ | |
function elementor_pre_get_posts( \WP_Query $query ) { | |
if ( | |
// If the post type includes the Elementor landing page CPT. | |
class_exists( '\Elementor\Modules\LandingPages\Module' ) | |
&& is_array( $query->get( 'post_type' ) ) | |
&& in_array( \Elementor\Modules\LandingPages\Module::CPT, $query->get( 'post_type' ), true ) | |
// If custom permalinks are enabled. | |
&& '' !== get_option( 'permalink_structure' ) | |
// If the query is for a front-end page. | |
&& ( ! is_admin() || wp_doing_ajax() ) | |
&& $query->is_main_query() | |
// If the query is for a page. | |
&& isset( $query->query['page'] ) | |
// If the query is not for a static home/blog page. | |
&& ! is_home() | |
// If the page name has been set and is not for a path. | |
&& ! empty( $query->query['pagename'] ) | |
&& false === strpos( $query->query['pagename'], '/' ) | |
// If the name has not already been set. | |
&& empty( $query->query['name'] ) ) { | |
$query->set( 'name', $query->query['pagename'] ); | |
} | |
} | |
add_action( 'pre_get_posts', 'elementor_pre_get_posts', 100 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure if anyone else is still having this issue but I got the following to work:
Step 1 - return to standard URL structure
Step 2 - add the following to functions.php - this is for /blog/ to be added before posts only - based on this post: https://wordpress.stackexchange.com/questions/300806/custom-permalink-for-post-only-not-custom-post-types
// Elementor Landing Page Custom URL Structure Fix
/**
/
function create_new_url_querystring() {
add_rewrite_rule(
'blog/([^/])$',
'index.php?name=$matches[1]',
'top'
);
add_rewrite_tag('%recipes%','([^/]*)');
}
add_action('init', 'create_new_url_querystring', 999 );
/**
*/
function append_query_string( $url, $post, $leavename ) {
if ( $post->post_type == 'post' ) {
$url = home_url( user_trailingslashit( "blog/$post->post_name" ) );
}
return $url;
}
add_filter( 'post_link', 'append_query_string', 10, 3 );
/**
*/
function redirect_old_urls() {
if ( is_singular('post') ) {
global $post;
if ( strpos( $_SERVER['REQUEST_URI'], '/blog/') === false) {
wp_redirect( home_url( user_trailingslashit( "blog/$post->post_name" ) ), 301 );
exit();
}
}
}
add_filter( 'template_redirect', 'redirect_old_urls' );