The code snippet provided below helps resolve conflicts between Elementor and Yoast SEO plugins in WordPress. This conflict often occurs when the Yoast SEO meta box is not displayed on the Elementor editor page. The code snippet ensures that the Yoast SEO meta box is displayed correctly within the Elementor editor.
function wpsnippets_elementor_yoast_seo_conflict() {
if ( class_exists( 'ElementorPlugin' ) && class_exists( 'WPSEO_Meta' ) ) {
add_action( 'elementor/element/wpseo_metabox/before_section_end', function() {
echo '<script>jQuery( document ).trigger( "YoastSEO:metaboxReloaded" );</script>';
});
}
}
add_action( 'elementor/init', 'wpsnippets_elementor_yoast_seo_conflict' );
This code snippet checks if both Elementor and Yoast SEO plugins are active. If they are, it adds an action hook to the elementor/element/wpseo_metabox/before_section_end
hook. Within this action, a JavaScript snippet is echoed that triggers the YoastSEO:metaboxReloaded
event. This event ensures that the Yoast SEO meta box is reloaded and displayed correctly within the Elementor editor.
This code snippet can be useful for WordPress developers who use both Elementor and Yoast SEO plugins on their websites. By resolving the conflict between these plugins, it ensures that the Yoast SEO meta box is properly displayed within the Elementor editor, allowing users to optimize their content for search engines without any issues.
Examples
Example 1: Disable Yoast SEO analysis on Elementor pages
This use case demonstrates how to disable the Yoast SEO analysis on pages created with Elementor. By default, Yoast SEO analyzes the content of each page and provides suggestions for improving SEO. However, when using Elementor, this analysis can interfere with the page builder’s functionality. To disable the analysis, you can add the following code to your theme’s functions.php
file:
function wpsnippets_disable_yoast_seo_analysis() {
if ( class_exists( 'ElementorPlugin' ) && ElementorPlugin::$instance->preview->is_preview_mode() ) {
add_filter( 'wpseo_frontend_presentations', '__return_false' );
}
}
add_action( 'init', 'wpsnippets_disable_yoast_seo_analysis' );
This code checks if Elementor is active and if the current page is being previewed in Elementor’s editor. If both conditions are met, it adds a filter to disable the Yoast SEO analysis on the frontend.
Example 2: Exclude Elementor pages from Yoast SEO sitemap
In this use case, we’ll exclude pages created with Elementor from the sitemap generated by Yoast SEO. Elementor pages are typically built using dynamic content and may not require inclusion in the sitemap. To exclude Elementor pages, you can use the following code:
function wpsnippets_exclude_elementor_pages_from_sitemap( $excluded ) {
if ( class_exists( 'ElementorPlugin' ) && ElementorPlugin::$instance->preview->is_preview_mode() ) {
$excluded[] = get_the_ID();
}
return $excluded;
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'wpsnippets_exclude_elementor_pages_from_sitemap' );
This code checks if Elementor is active and if the current page is being previewed in Elementor’s editor. If both conditions are met, it adds the current page ID to the list of excluded post types for the Yoast SEO sitemap.
Example 3: Disable Yoast SEO meta box on Elementor pages
In this use case, we’ll disable the Yoast SEO meta box on pages created with Elementor. The Yoast SEO meta box provides options for optimizing the SEO of a page, but it may not be necessary or desired on Elementor pages. To disable the meta box, you can use the following code:
function wpsnippets_disable_yoast_seo_metabox() {
if ( class_exists( 'ElementorPlugin' ) && ElementorPlugin::$instance->preview->is_preview_mode() ) {
remove_meta_box( 'wpseo_meta', 'page', 'normal' );
}
}
add_action( 'add_meta_boxes', 'wpsnippets_disable_yoast_seo_metabox', 99 );
This code checks if Elementor is active and if the current page is being previewed in Elementor’s editor. If both conditions are met, it removes the Yoast SEO meta box from the page editor.