Remove Yoast WordPress SEO on a page - wordpress

I am trying to remove the Yoast WordPress SEO on a certain page because it is conflicting with another plugin.
I tried adding the code below to my functions.php but it does not seem to work, any help is appreciated.
Thank You
function remove_wpseo(){
if ( is_page(944)) {
global $wpseo_front;
remove_action( 'wp_head', array($wpseo_front, 'head'), 2 );
}
}
add_action('wp_enqueue_scripts','remove_wpseo');

Just in case someone is wondering why above methods are not working after the upgrade, this is the new method of disabling Yoast SEO output as of version 14.0
add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
if ( is_page ( 944 ) ) {
$front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );
remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );
}
}
Hope this helps!

Enqueing is not the right moment to remove an action, use template_redirect instead:
add_action('template_redirect','remove_wpseo');
function remove_wpseo(){
if ( is_page(944)) {
global $wpseo_front;
remove_action( 'wp_head', array($wpseo_front, 'head'), 2 ); // <-- check priority
}
}
Check the priority that the plugin uses to add the wp_head action as the removal has to be the same and none if empty.

Just in case someone still needs this. This worked for me. Change 'page' to 'post' if it's a blog post. Instead of trying to throw out Yoast completely, just hide the meta box.
add_action( 'add_meta_boxes', 'remove_post_meta_boxes', 11 );
function remove_post_meta_boxes() {
if( isset( $_GET['post'] ) && $_GET['post'] == '22' ) {
remove_meta_box( 'wpseo_meta', 'page', 'normal' );
}
}

#jhashane answer is correct as a straight forward answer to the question as it is. However, this removes the <tite> tag as well, and other default worpdress meta, as long as Yoast is active and installed. It just kills the output handled by Yoast.
Disable Yoast components might be a better approach most of the time. Conflicts are often about Yoast taking over some meta, specially types or social open graphs.
We spend hours to understand the new 14.0 version. As of Yoast SEO 14.0 the plugin is more like Danish Lego and building up the head meta tags as "presenters".
EX: Remove all social media components output on page id 123:
function intervik_wpseo_frontend_presenters($presenters){
/* set your conditional(s) */
if(!is_singular(123)) return $presenters;
/* return all WITHOUT Open_Graph and Twitter presenters on $page_id = 123 */
if($matches = preg_grep('/Open_Graph|Twitter/', $presenters)) return array_diff($presenters, $matches);
else return $presenters;
}
add_filter('wpseo_frontend_presenter_classes', 'intervik_wpseo_frontend_presenters', 10, 1);
The filter in this example is very late. You can still use individual meta or schema etc etc filters to filter the content configuration and settings. And when you partial manually wanna disable your configuration, on page basis or so, remove "blocks" of Yoast do in your source code:
function intervik_wpseo_frontend_presenters($presenters){
/* REMOVE ONE exactly presenters (example: Canonical) */
if(($key = array_search('Yoast\WP\SEO\Presenters\Canonical_Presenter', $presenters)) !== false){
unset($presenters[$key]);
}
return $presenters;
}
add_filter('wpseo_frontend_presenter_classes', 'intervik_wpseo_frontend_presenters', 10, 1);
Remove everything except document title and description on pages:
function intervik_wpseo_frontend_presenters($presenters){
$keep[] = 'Yoast\WP\SEO\Presenters\Title_Presenter';
$keep[] = 'Yoast\WP\SEO\Presenters\Meta_Description_Presenter';
$keep[] = 'Yoast\WP\SEO\Presenters\Robots_Presenter';
/* remove ALL, except title, description and robots on PAGES */
if(is_page()) return $keep;
else return $presenters;
}
add_filter('wpseo_frontend_presenter_classes', 'intervik_wpseo_frontend_presenters', 10, 1);
In this case, you should use the wpseo_title etc etc filters to enable och disable Yoast content, and use Wordpress default content on certain selected post or pages.
Some meta still have the old filter(s) left to turn different "grouped" output on or off Like add_filter('wpseo_output_twitter_card', '__return_false' ); But Facebook for example, is part of the opengraph, shared by other social media. There is no explicit Facebook opengraph filter. And in the future, we will have more social media and other tags shared components.
Lets play with bricks and blocks instead.
Common default presenters: (dump of $presenters)
array(27) {
[0]=>
string(39) "Yoast\WP\SEO\Presenters\Title_Presenter"
[1]=>
string(50) "Yoast\WP\SEO\Presenters\Meta_Description_Presenter"
[2]=>
string(40) "Yoast\WP\SEO\Presenters\Robots_Presenter"
[3]=>
string(43) "Yoast\WP\SEO\Presenters\Googlebot_Presenter"
[4]=>
string(41) "Yoast\WP\SEO\Presenters\Bingbot_Presenter"
[5]=>
string(43) "Yoast\WP\SEO\Presenters\Canonical_Presenter"
[6]=>
string(42) "Yoast\WP\SEO\Presenters\Rel_Prev_Presenter"
[7]=>
string(42) "Yoast\WP\SEO\Presenters\Rel_Next_Presenter"
[8]=>
string(51) "Yoast\WP\SEO\Presenters\Open_Graph\Locale_Presenter"
[9]=>
string(49) "Yoast\WP\SEO\Presenters\Open_Graph\Type_Presenter"
[10]=>
string(50) "Yoast\WP\SEO\Presenters\Open_Graph\Title_Presenter"
[11]=>
string(56) "Yoast\WP\SEO\Presenters\Open_Graph\Description_Presenter"
[12]=>
string(48) "Yoast\WP\SEO\Presenters\Open_Graph\Url_Presenter"
[13]=>
string(54) "Yoast\WP\SEO\Presenters\Open_Graph\Site_Name_Presenter"
[14]=>
string(62) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Publisher_Presenter"
[15]=>
string(59) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Author_Presenter"
[16]=>
string(67) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Published_Time_Presenter"
[17]=>
string(66) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Modified_Time_Presenter"
[18]=>
string(50) "Yoast\WP\SEO\Presenters\Open_Graph\Image_Presenter"
[19]=>
string(54) "Yoast\WP\SEO\Presenters\Open_Graph\FB_App_ID_Presenter"
[20]=>
string(46) "Yoast\WP\SEO\Presenters\Twitter\Card_Presenter"
[21]=>
string(47) "Yoast\WP\SEO\Presenters\Twitter\Title_Presenter"
[22]=>
string(53) "Yoast\WP\SEO\Presenters\Twitter\Description_Presenter"
[23]=>
string(47) "Yoast\WP\SEO\Presenters\Twitter\Image_Presenter"
[24]=>
string(49) "Yoast\WP\SEO\Presenters\Twitter\Creator_Presenter"
[25]=>
string(46) "Yoast\WP\SEO\Presenters\Twitter\Site_Presenter"
[26]=>
string(40) "Yoast\WP\SEO\Presenters\Schema_Presenter"
}
Finally, here is an example of "conflicts with other plugin". We needed to remove open graphs when plugin "Groups" restricted a page. We don wanna share or give meta for sharing here. Either, Yoast should not add this page to the sitemap, and the robots must be set as noindex. All this is decided by a custom meta on the page we talking about:
/* Disable YOAST components output on selected pages by custom meta */
function intervik_wpseo_frontend_presenters($presenters){
if(is_singular()){
global $post;
$meta = get_post_meta($post->ID, 'example_custom_field', true);
if($meta){
add_filter('wpseo_json_ld_output', '__return_false');
add_filter('wpseo_robots', function(){
return 'noindex,nofollow';
}, 30, 1);
if($matches = preg_grep('/Open_Graph|Twitter/', $presenters)) $presenters = array_diff($presenters, $matches);
}
}
return $presenters;
}
add_filter('wpseo_frontend_presenter_classes', 'intervik_wpseo_frontend_presenters', 10, 1);
Sitemap case: We still need a separate filter process for the sitemap as it might be loaded by ajax or cron/ not rendered as head output:
/* -------------------- */
// DISABLE SITEMAP EXAMPLE
function intervik_wpseo_sitemap_entry_exclude($url, $type, $object){
/* Types can be = 'term', 'post', 'user' */
if($type == 'post'){
$meta = get_post_meta($object->ID, 'example_custom_field', true);
if($meta) return false;
}
return $url;
}
add_filter('wpseo_sitemap_entry', 'intervik_wpseo_sitemap_entry_exclude', 30, 3);
This example above shows how the filters and presenters works
together to control the output of Yoast. Adding modules like
YOAST woocommerce or YAOST Local gives you more presenters to play with/ add or remove. And the best part of 14 API, you can
provide your own presenter.
Please comment to add another approach or trix.

Related

how to remove excerpt on category archive page in genesis framework?

I am trying to remove the excerpt ( content ) from the category archive page so only the title shown on the page, not the content.
I am currently trying the code below with no luck?
does anybody have any suggestions?
add_action ( 'genesis_before_entry' , 'designody_remove_entry_content_archives' );
function designody_remove_entry_content_archives() {
if (in_category('5')) {
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
}
}
my second solution... also not working
function replace_content( $output) {
if (is_category('5')) {
return '' ;
}
}
add_filter( 'get_the_excerpt', 'replace_content' );
I don't have a fully satisfactory Genesis-specific solution, and I'd be curious to know what StudioPress itself says, but I do have WordPress solutions.
First, to use the Genesis hook and a condition, you need to wrap it in a function:
To quote the Codex: "Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function."
https://codex.wordpress.org/Conditional_Tags
The following works to remove the excerpt on the specified category archive page:
add_action( 'loop_start', 'remove_content_on_category_pages' ) ;
function remove_content_on_category_pages() {
if ( is_category( '5' ) ) {
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
}
}
However, the in_category() conditional - which should help with entry content when it appears in other archives (categories, blog, etc.) - doesn't seem to work.
In order to get the desired results as I understand them - no post content, in this implementation "the excerpt", for this category on any type of archive page, but preserving it for individual posts - you can revert to the trusty the_content filter, but note that the the_content can be a tricky and powerful tool subject to unexpected secondary effects. That said, on a vanilla Genesis installation, the following tests out:
add_filter( 'the_content', 'test_gen_content' ) ;
function test_gen_content( $content ) {
/**
apply to all secondary queries don't apply if single page or post, or in
admin, otherwise obliterate - can use is_single() instead of is_singular()
if only posts are at issue
**/
if ( ! is_singular() && ! is_admin() && in_category( '157' ) ) {
return '' ;
}
return $content ;
}
I must say, however, that, given the peculiarities and complications of the Genesis framework, and the peculiarity of the request, out there in the wild I'd be very tempted to use a CSS kludge, shaped to requirements - at least failing direct instruction or intervention from a Genesis maven or StudioPress itself. Whether you needed to refine the CSS further to suit your particular installation, I can't say, but it might be a lot easier, actually, then sorting out the PHP even with the correct hooks etc.
//substitute appropriate category slug for "development-forums"
.home .category-development-forums .entry-content,
.blog .category-development-forums .entry-content,
.archive .category-development-forums .entry-content
{
display: none;
}

How to disable Yoast SEO adding article:author meta tags to pages

The excellent Yoast SEO plugin is adding some unwanted meta tags.
For example, I would like article:author to appear on posts, but not on pages or other content types.
Is there a way to adjust this globally?
I'm happy to edit functions.php, but I'm just unsure what I should be hooking in to.
I would be grateful for any pointers from those more familiar with the plugin.
I tried this:
function wpseo_show_article_author_only_on_posts() {
if ( !is_single() ) {
return false;
}
}
add_filter( 'xxxxxx', 'wpseo_show_article_author_only_on_posts' );
I need to know what hook should replace xxxxxx.
You're looking for the wpseo_opengraph_author_facebook filter, which ties into the article_author_facebook() method in frontend/class-opengraph.php of the plugin.
function wpseo_show_article_author_only_on_posts( $facebook ) {
if ( ! is_single() ) {
return false;
}
return $facebook;
}
add_filter( 'wpseo_opengraph_author_facebook', 'wpseo_show_article_author_only_on_posts', 10, 1 );
The article_author_facebook() method does a check for is_singular(), which checks that we're viewing single page, post or attachment:
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
The additional filter for ( ! is_single() ) ensures that article:author is only added to posts.
If people are looking for a way to remove more yoast SEO tags, just lookup the file wordpress-seo/frontend/class-opengraph.php, and you can see which filters you can hook into to remove certain tags.
This is the code I use to remove these tags from pages: url, image, title, description and type:
function remove_yoast_og_tags( $ogTag ){
// Do a check of which type of post you want to remove the tags from
if ( is_page() ) {
return false;
}
return $ogTag;
}
$yoastTags = array(
"url",
"image",
"title",
"desc",
"type"
);
foreach ($yoastTags as $tag) {
add_filter( 'wpseo_opengraph_'.$tag, 'remove_yoast_og_tags');
}

Customize wordpress oEmbedd (soundcloud)

I am using Wordpress 4.3.1 and I noticed that it used oEmbed to automatically embed my tracks from soundcloud.
This is awesome! Except, I have no control over how the widget is displayed.
First of all I would like to display the "Classic Embed" without the artwork.
Secondly I want the widget to be wider.
How can I achieve this without editing the shortcode for each post in my wordpress page?
You can add a filter to change the way the oEmbed code is displayed. Here is something to get you started:
// this function is called on all urls surrounded by the WordPress embed shortcode.
// i.e.: [embed]https://soundcloud.com/gratefuldead/grateful-dead-box-of-rain[/embed]
function my_embed_options( $code ) {
// look for SoundCloud link:
if( strpos( $code, 'soundcloud.com') !== false ) {
$code = str_replace( 'show_artwork=true', 'show_artwork=false', $code );
}
return $code;
}
add_filter( 'embed_oembed_html', 'my_embed_options' );
You can find full parameter list for the SoundCloud player here.

Exclude specific post from WP-feed

I know that you can generate a feed using urls like: ?cat=3&feed=rss2
And you can switch it around to exclude the category 3 by putting a subtract sign in front: ?cat=-3&feed=rss2
But, it doesn't look like you can do the same for posts? I'm using the JW video Player and have loaded the related plugin. The related plugin can take an rss-feed (media rss) as the parameter so it can link to other videos/wordpress posts that are related.
My problem is that currently this means that the active video also appears in the related videos feed.
What would be the best solution for solving this problem? I aim to create my own rss feed generator in the future, but for now I just want to keep it simple and use the generated feeds that wordpress creates. Is there a simple way to add support for an url parameter named post for example? It could then take post=-7 to exclude post with id 7 from displaying in the feed.
Or is there better solutions for this?
You can use a function
function exclude_category($query){
if ( $query->is_home || $query->is_feed || $query->is_archive ) {
$query->set('cat', '-1');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
see'a
Instead of explaining the mechanisem - there are many plugins just for that ..
One that I know is :
StelthPubish
Edit I
I do not know about the URL - but you can try use the
function ok9_feed_filter($query) {
if ( !$query->is_admin && $query->is_feed) {
$query->set('post__not_in', array(15, 6) ); // page /post id
}
return $query;
}
add_filter( 'pre_get_posts', 'ok9_exclude_filter' );
or this
function ok9_feed_exlude($where, $wp_query = NULL) {
global $wpdb;
if ( !$wp_query )
global $wp_query;
if ($wp_query->is_feed) {
// exclude post id
$where .= " AND $wpdb->posts.ID NOT IN (15, 6)";
}
return $where;
}
add_filter( 'posts_where','ok9_feed_exlude', 1, 2 );
If you do not want to use a fixed id in the function - you can always add a custom field in the posts you want to exclude , and use that in the query ..

How do I make my custom WordPress meta box visible to admins only?

I am using my functions.php to add a custom meta box on my posts page in the WordPress Admin Area. However, I need to make it so its only visible to admins, and not editors, contributors, etc.
What would I do to make it visible to admins only?
function your_function() {
global $current_user;
if($current_user->roles[0] == 'administrator') {
add_meta_box(your parameters);
// fill in your parameters
}
}
add_action('admin_init','your_function');
if ( is_user_logged_in() ) {
get_currentuserinfo();
# check if current user is admin
if ( $current_user->wp_user_level >= 10 ) {
# put your admin-only function here
}
}
This snippet works for custom taxonomies. It removes / hides a custom taxonomy meta box for all non-admins, assuming no other role has the update_core capability. Similar, but opposite of the answer by #johannes-pille
function remove_tax_metaboxes() {
if (!current_user_can('update_core')) {
remove_meta_box( 'taxdiv', 'post', 'side' );
}
}
add_action( 'do_meta_boxes', 'remove_tax_metaboxes' );
Note that the third argument of remove_meta_box may differ, see https://codex.wordpress.org/Function_Reference/remove_meta_box

Resources