senlin/so-clean-up-wp-seo

[Feature Request] remove admin columns instead of hiding them

senlin opened this issue ยท 24 comments

Originally reported by @Dibbyo456 as issue #64

Currently if we hide Admin columns such as SEO score column, Readability score column etc the data actually gets hidden but the options still shows up "Screen Options" settings. I'm guessing the columns is not unhooking the proper way. The proper way to hide those would be this: https://gist.github.com/amboutwe/18558a7e681e36c6bfe6e4fb647265ce

I'm not sure about this one; in the readme there is no talk about removing the columns, instead it clearly says that they're hidden:

hide the SEO Score, Readability, Title, Meta Description and Outgoing Internal Links admin columns on the Posts/Pages screens; Focus keyword column can be hidden too

(emphasis mine)

I'll leave this open as a Feature Request and if we get a lot of votes on it, I might change it.

Thanks for posting @Dibbyo456!

I don't know which method you're using but why not use the official wordpress method?

I'm hiding the columns (thus through CSS).

Ah, that's exactly why the options were still visible on "Screen Options".
I think CSS patch should be last resort in any case.

I will post a proper patch (along with CPT).

// Function to remove from admin column.
function yoast_seo_admin_remove_columns( $columns ) {
	unset($columns['wpseo-score']);
	unset($columns['wpseo-score-readability']);
	unset($columns['wpseo-title']);
	unset($columns['wpseo-metadesc']);
	unset($columns['wpseo-focuskw']);
	unset($columns['wpseo-links']);
	unset($columns['wpseo-linked']);
	return $columns;
}

// Remove from Post.
add_filter( 'manage_edit-post_columns', 'yoast_seo_admin_remove_columns' );
// Remove from Page.
add_filter( 'manage_edit-page_columns', 'yoast_seo_admin_remove_columns' );
// Remove from Custom Post Types.
add_action( 'admin_menu', function() {
	// get all available custom post types as array.
	$custom_post_types = get_post_types( array( '_builtin' => false ) );
	// loop through each one of them.
	foreach( $custom_post_types as $post_type ) {
		add_filter( 'manage_edit-'. $post_type .'_columns', 'yoast_seo_admin_remove_columns' );
	}
});

@Dibbyo456
Thanks Harry, it seems you're burning the midnight oil! ;)
I will add this over the weekend or early next week.

okay, so the function works, but now the problem is how to add it to the includes/class-so-clean-up-wp-seo.php file. Been trying and so far the result is an empty post screen.

Added the following to the __construct:

// Remove from Post.
add_filter( 'manage_edit-post_columns', array( $this, 'so_cuws_remove_admin_columns' ), 10, 1  );
// Remove from Page.
add_filter( 'manage_edit-page_columns', array( $this, 'so_cuws_remove_admin_columns' ), 10, 1  );
// Remove from Custom Post Types.
add_action( 'admin_menu', function() {
	// get all available custom post types as array.
	$custom_post_types = get_post_types( array( '_builtin' => false ) );
	// loop through each one of them.
	foreach( $custom_post_types as $post_type ) {
		add_filter( 'manage_edit-'. $post_type .'_columns', array( $this, 'so_cuws_remove_admin_columns' ), 10, 1  );
	}
});

and then below the __construct, where also all the other functions are located:

	public function so_cuws_remove_admin_columns( $columns ) {

		if ( ! empty( $this->options['hide_admincolumns'] ) ) {

			// seo score column
			if ( in_array( 'seoscore', $this->options['hide_admincolumns'] ) ) {

				unset( $columns['wpseo-score'] );

			}

			// readability column
			if ( in_array( 'readability', $this->options['hide_admincolumns'] ) ) {

				unset( $columns['wpseo-score-readability'] );

			}
			// title column
			if ( in_array( 'title', $this->options['hide_admincolumns'] ) ) {

				unset( $columns['wpseo-title'] );

			}
			// meta description column
			if ( in_array( 'metadescr', $this->options['hide_admincolumns'] ) ) {

				unset( $columns['wpseo-metadesc'] );

			}
			// focus keyword column
			if ( in_array( 'focuskw', $this->options['hide_admincolumns'] ) ) {

				unset( $columns['wpseo-focuskw'] );

			}
			// outgoing internal links column
			if ( in_array( 'outgoing_internal_links', $this->options['hide_admincolumns'] ) ) {

				unset( $columns['wpseo-links'] );
				unset( $columns['wpseo-linked'] );

			}

			return $columns;

		}

	}

Any ideas welcome!

I tested your given codes and it's works perfectly fine for me...!
I added codes to __construct and below it. Then I tested everything seems perfectly fine.
There is nothing wrong with your code.

I tested your given codes and it's works perfectly fine for me...!

Hmmm, will do a fresh install then, because this is getting frustrating...

I think it's better to have all of those filters inside single admin_menu hook for better maintenance and readability.

// Remove columns from admin bar.
add_action( 'admin_menu', function() {
	// post, page and custom post types
	$all_post_types = array_merge( array( 'post', 'page' ), get_post_types( array( '_builtin' => false ) ) );

	foreach( $all_post_types as $post_type ) {
		add_filter( 'manage_edit-'. $post_type .'_columns', array( $this, 'so_cuws_remove_admin_columns' ), 10, 1  );
	}
}, 999 );

short and sweet.

But don't push and update yet the codes does not seem to remove this fields.
I know the plugin does it via css patch but i'm working on a filter patch.

2019-08-28_20-47-59

I'm not pushing anything yet, don't worry :)

There is a problem with the code: try enabling all the columns in the Hide bloat settings. If all the checkboxes are open, then there are errors all over the place.

There is a problem with the code: try enabling all the columns in the Hide bloat settings. If all the checkboxes are open, then there are errors all over the place.

That's because $columns was not returning when $this->options['hide_admincolumns'] was empty. I fixed the codes.

Inside __construct

// Remove columns from admin bar.
add_action( 'admin_menu', function() {
	// post, page and custom post types
	$all_post_types = array_merge( array( 'post', 'page' ), get_post_types( array( '_builtin' => false ) ) );

	foreach( $all_post_types as $post_type ) {
		add_filter( 'manage_edit-'. $post_type .'_columns', array( $this, 'so_cuws_remove_admin_columns' ), 10, 1  );
	}
}, 999 );

below it.

public function so_cuws_remove_admin_columns( $columns ) {

	// if empty return columns right away.
	if ( empty( $this->options['hide_admincolumns'] ) ) {
		return $columns;
	}

	// seo score column
	if ( in_array( 'seoscore', $this->options['hide_admincolumns'] ) ) {
		unset( $columns['wpseo-score'] );
	}

	// readability column
	if ( in_array( 'readability', $this->options['hide_admincolumns'] ) ) {
		unset( $columns['wpseo-score-readability'] );
	}
		
	// title column
	if ( in_array( 'title', $this->options['hide_admincolumns'] ) ) {
		unset( $columns['wpseo-title'] );
	}
		
	// meta description column
	if ( in_array( 'metadescr', $this->options['hide_admincolumns'] ) ) {
		unset( $columns['wpseo-metadesc'] );
	}

	// focus keyword column
	if ( in_array( 'focuskw', $this->options['hide_admincolumns'] ) ) {
		unset( $columns['wpseo-focuskw'] );
	}

	// outgoing internal links column
	if ( in_array( 'outgoing_internal_links', $this->options['hide_admincolumns'] ) ) {
		unset( $columns['wpseo-links'] );
		unset( $columns['wpseo-linked'] );
	}

	return $columns;
}

Let me know, if everything works now.

Yes, now it indeed works, thank you.

One thing though, the dropdowns you referred to earlier are no longer present, even if the seo score or readability score is left enabled.

Remove this from function so_cuws_hide_visibility_css()

// hide SEO scores dropdown filters on edit Post/Page screen
if ( ! empty( $this->options['hide_seo_scores_dropdown_filters'] ) ) {
	echo '#wpseo-filter, #wpseo-readability-filter{display:none;}'; // @since v3.10.0 hide SEO scores dropdown filters
}

Add on __construct

// Remove SEO scores dropdown filters on edit Post/Page screen
add_action( 'admin_init', function() {
	if ( ! empty( $this->options['hide_seo_scores_dropdown_filters'] ) ) {
	    global $wpseo_meta_columns ;
	    if ( $wpseo_meta_columns  ) {
	        remove_action( 'restrict_manage_posts', array( $wpseo_meta_columns , 'posts_filter_dropdown' ) );
	        remove_action( 'restrict_manage_posts', array( $wpseo_meta_columns , 'posts_filter_dropdown_readability' ) );
	    }
	}
}, 20 );

It remove instead of hide. Let me know if everything checks out.

hahaha, yeah sorry I had forgotten that it is a feature of the plugin to hide those ;)

regarding your snippet, thanks I will have to refactor it to make it follow the same style as the rest of the plugin. consistency, you know :)

regarding your snippet, thanks I will have to refactor it to make it follow the same style as the rest of the plugin. consistency, you know :)

Sure. No problem.
Just make sure to remove obsolete codes from plugin before pushing update.

Yup, that works, thanks again for all the initiative on improving the plugin!

Don't want to turn this issue into a chatbox but I can't help but share a very interesting thing I learned today.

When accessing class using global the class name needs to be in lowercase even if the class declared name was something like WPSEO_Meta_Columns

For example:

// does not work.
global $WPSEO_Meta_Columns;
remove_action( 'restrict_manage_posts', array( $WPSEO_Meta_Columns, 'posts_filter_dropdown' ) );

// works.
global $wpseo_meta_columns;
remove_action( 'restrict_manage_posts', array( $wpseo_meta_columns, 'posts_filter_dropdown' ) );

I have no idea why is that. ยฏ_(ใƒ„)_/ยฏ
Do you know something about it?

That indeed is interesting. Can't say I know something about that. Perhaps @afragen knows? Otherwise a good resource is the stackexchange

From what I can tell the class name is in camelCase and the variable name is lowercase. What we are doing here is getting the variable.

https://github.com/Yoast/wordpress-seo/blob/ca1b44d82443ef3cc273c1c6a779e915540182b6/admin/class-admin-init.php#L543

Thanks for your input Andy!

@Dibbyo456 - Hi how are you?, Can you please have a look at this new issue #75 that has been opened?

fixed in new release which will go live on WP in a minute