• Resolved Nazrinn

    (@nazrinn)


    Hi there The SEO Framework guys.

    I was wondering if I could somehow remove the SEO column for a custom post type.
    They are listings; they will change often, so it’s unlikely my clients will need to see big red warnings if they didn’t set the page title and description “correctly”.

    Here is my code:

    add_filter( 'manage_listings_posts_columns', 'remove_seo_column' );
    function remove_seo_column( $columns ) {
      unset(
      	$columns['seo'],
      );
     
      return $columns;
    }

    I know it’s *mostly* there, but I’d just need the exact name of the SEO column. Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello,

    Use var_dump() (a debugging function) on the $columns, and you’ll know what to remove 🙂

    add_filter( 'manage_listings_posts_columns', 'remove_seo_column' );
    function remove_seo_column( $columns ) {
      var_dump( $columns ); // dump active columns on screen... WordPress's admin bar will obscure this, so you'll have to inspect the source.
      unset(
      	$columns['SEO'],
      );
     
      return $columns;
    }

    Hint: PHP’s array keys are case-sensitive. It’s SEO, not seo.

    Thread Starter Nazrinn

    (@nazrinn)

    I used var_dump before but somehow it didn’t display anything (I know for sure WP_DEBUG is true). Even with inspecting, I couldn’t find the my var_dump().

    Any idea of what might cause this?
    The function itself seem to work as if I return anything that is not $columns the admin panel for this post type becomes blank.

    • This reply was modified 3 years ago by Nazrinn.
    Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    I usually dump an identifier before I dump the rest. This helps me find the dump in the forest of code. E.g.: var_dump( 'DUMP ONE', $vartodump );.

    Anyway, I believe this a priority issue. You need to add 11 or higher when filtering the columns. Here’s the amended code considering the issue:

    add_filter( 'manage_listings_posts_columns', 'remove_seo_column', 99 ); // priority 99
    function remove_seo_column( $columns ) {
      unset(
      	$columns['SEO'],
      );
     
      return $columns;
    }
    Thread Starter Nazrinn

    (@nazrinn)

    No luck.

    Here is my full code for the admin panel of that CPT.
    SEO column code is the last block.

    
    #Adds column
    add_filter ( 'manage_listings_posts_columns', 'add_acf_columns' );
    function add_acf_columns ( $columns ) {
     return array_merge ( $columns, array (
       'status' => __ ( 'Status' )
     ) );
    }
    
    #reorder columns
    add_filter('manage_posts_columns', 'column_order');
    function column_order($columns) {
      $n_columns = array();
      $move = 'status'; // what to move
      $before = 'date'; // move before this
      foreach($columns as $key => $value) {
        if ($key==$before){
          $n_columns[$move] = $move;
        }
          $n_columns[$key] = $value;
      }
      return $n_columns;
    }
    
    #Adds value to column
    function listings_custom_column ( $column, $post_id ) {
      switch ( $column ) {
        case 'status':
          echo ucwords(get_post_meta( $post_id, 'status', true ));
          break;
      }
    }
    add_action ( 'manage_listings_posts_custom_column', 'listings_custom_column', 10, 2 );
    
    #Make column sortable
    function register_sortable_column( $columns ) {
    	$columns['status'] = 'status';
    	return $columns;
    }
    add_filter('manage_edit-listings_sortable_columns', 'register_sortable_column' );
    
    #Adds dropdown select
    add_action( 'restrict_manage_posts', 'listings_admin_posts_filter_restrict_manage_posts' );
    function listings_admin_posts_filter_restrict_manage_posts(){
        $type = 'post';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        if ('listings' == $type){
            $values = array(
                'Coming Soon' => 'coming soon',
                'Listed' => 'listed',
                'Conditionally Sold' => 'conditionally sold',
                'Sold' => 'sold',
            );
            ?>
            <select name="ADMIN_FILTER_FIELD_VALUE">
            <option value=""><?php _e('All Status ', 'wose45436'); ?></option>
            <?php
                $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
                foreach ($values as $label => $value) {
                    printf
                        (
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
            ?>
            </select>
            <?php
        }
    }
    
    add_filter( 'parse_query', 'listings_posts_filter' );
    function listings_posts_filter( $query ){
        global $pagenow;
        $type = 'post';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'listings' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
            $query->query_vars['meta_key'] = 'status';
            $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
        }
    }
    
    //TEST remove SEO column.
    add_filter( 'manage_listings_posts_columns', 'remove_seo_column', 99 ); //priority
    function remove_seo_column( $columns ) {
      var_dump( "GIVE ME MY COLUMNS AAAAA", $columns );
      //unset(
      	//$columns['SEO'],
      //);
     
      return $columns;
    }
    

    I tried to do a var_dump() in the first manage_listings_posts_columns but this one also returns me nothing (visible).

    • This reply was modified 3 years ago by Nazrinn.

    Nazrinn, I was able to remove these columns on a case by case basis by clicking the screen options tab and unchecking SEO. This only does it for you as a logged in user, so you’ll have to inform other users to do the same.

    Thread Starter Nazrinn

    (@nazrinn)

    No way to do this for every user?
    Displaying it in some cases is kinda pointless.

    I found something even better:

    https://wordpress.org/plugins/codepress-admin-columns/

    I don’t like to use a lot of plugins, but I think this one is going to become indispensable!

    Thread Starter Nazrinn

    (@nazrinn)

    Oh yeah, I know about this plugin, but it’s a bit overkill considering I just want to hide one column.

    Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    At the time of writing my snippet, I wasn’t able to test all this. The following snippet will remove the SEO column for Posts.

    add_filter( 'manage_edit-post_columns', 'remove_seo_column', 99 ); // priority 99
    function remove_seo_column( $columns ) {
      unset(
      	$columns['tsf-seo-bar-wrap'],
      );
     
      return $columns;
    }

    To apply this to other post types and terms, you’ll need to replace edit-post with the $current_screen->id. You can fetch that name using this method:

    add_action( 'current_screen', static function( $screen ) { var_dump( $screen->id ); } );
    

    Here’s the final product, that automatically fills the screen ID for the filter.

    add_action( 'current_screen', function( $screen ) { 
    	if ( ! isset( $screen->id ) ) return;
    	
    	add_filter( "manage_{$screen->id}_columns", 'remove_seo_column', 99 );
    } );
    
    function remove_seo_column( $columns ) {
    	unset(
    		$columns['tsf-seo-bar-wrap'],
    	);
    	
    	return $columns;
    }

    If you wish to be selective about the post types and terms, then I recommend filtering that in the current_screen hook.

    I hope this helps 🙂 Cheers!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude SEO column from custom post type’ is closed to new replies.