Stopwords are those small words that should be removed from URLs. You know like ‘a’ and ‘and’ or ‘or’ and so on and so forth. They make for ungainly and long URLs and really you should remove them.
If you happen to use Yoast SEO for WordPress, and you want to disable stopwords, there’s a simple way about that. Go to SEO -> Advanced and disable the feature for stopwords.
If you want to kill it with fire and prevent everyone on your site from being able to activate them ever, you can toss this into an MU plugin.
add_filter( 'wpseo_stopwords', '__return_empty_array' ); remove_action( 'get_sample_permalink', 'wpseo_remove_stopwords_sample_permalink' );
The first filter makes the stopwords kick back nothing, and the remove action stops the process from running. You probably only need the second one, but better safe than sorry, I always say.
But … what if you want stop words removed, but you don’t want them removed on certain custom post types? Welcome to my world! I wanted to remove them from two post types only.
Enter my frankencode:
<?php /* Plugin Name: Yoast SEO Customizations Description: Some tweaks I have for Yoast SEO Version: 2.0 */ // Unless we're on a post or a post editing related page, shut up global $pagenow; $pagenow_array = array( 'post.php', 'edit.php', 'post-new.php' ); if ( !in_array( $pagenow , $pagenow_array ) ) { return; } // Since we are, we need to know exactly what we're on and this is a hassle. global $typenow; // when editing pages, $typenow isn't set until later! if ( empty($typenow) ) { // try to pick it up from the query string if (!empty($_GET['post'])) { $post = get_post($_GET['post']); $typenow = $post->post_type; } // try to pick it up from the query string elseif ( !empty($_GET['post_type']) ) { $typenow = $_GET['post_type']; } // try to pick it up from the quick edit AJAX post elseif (!empty($_POST['post_ID'])) { $post = get_post($_POST['post_ID']); $typenow = $post->post_type; } else { $typenow = 'nopostfound'; } } $typenow_array = array( 'post_type_shows', 'post_type_characters' ); if ( !in_array( $typenow , $typenow_array ) ) { return; } add_filter( 'wpseo_stopwords', '__return_empty_array' ); remove_action( 'get_sample_permalink', 'wpseo_remove_stopwords_sample_permalink', 10 );
There was something funny to this, by the way. Originally I didn’t have the $pagenow
code. Didn’t need it. But when I left it out, Yoast SEO broke with a weird error. It refused to load any of the sub-screens for the admin settings!
After some backpacking of “Okay, was it working before…?” I determined it was the call for global $typenow;
– a global that isn’t used at all in the Yoast SEO source code that I could find. Still, by making my code bail early if it’s not even on a page it should be on, I made the rest of the WP Admin faster, and that’s a win for everyone.