WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Strange behavior manage_posts_custom_column in WP 3.5-alpha-21751 (4 posts)

  1. helgatheviking
    Member
    Posted 1 year ago #

    I have attempted to add a custom column, which was working just fine in WP 3.4.

    here is the following simplified code:

    add_filter('manage_posts_columns', 'add_post_columns' );
    
    function add_post_columns($columns){
        $columns['bacon'] = "Bacon";
        return $columns;
    }
    
    add_action('manage_posts_custom_column', 'render_post_columns', 10, 2);
    
    function render_post_columns($column_name, $id) {
        switch ($column_name) {
        case 'bacon':
            echo "we like bacon";
            break;
        }
    }

    What I am seeing is a column titled "Bacon" but with the list of post tags as the column content.

    here's a screenshot:
    http://www.diigo.com/item/image/1xt6v/jgnd

    If I change the add_actions and add_filters to display on pages instead of posts:

    add_filter('manage_pages_columns', 'add_post_columns' );
    add_action('manage_pages_custom_column', 'render_post_columns', 10, 2);

    The column displays as expected. Same if I add it to any other post type. There is clearly an update to /wp-admin/includes/class-wp-posts-list-table.php but I can't see why it would be behaving this way for strictly the post columns.

    This was driving me crazy, because I thought something was broken in my code, but I have confirmed that it works in 3.4.2.

  2. helgatheviking
    Member
    Posted 1 year ago #

    I found the problem.... how typical that I would post about it only to find it myself 5 minutes later.

    Anyway, in the single_row method of WP_Posts_List_Table class there is a foreach loop that iterates over each $column_name and generates the markup for the column's content. Inside that foreach is switch statement.... which now has a default case.

    The first few lines of the default are are clearly set to interpret every extra column as a taxonomy:

    if ( 'categories' == $column_name )
      $taxonomy = 'category';
    elseif ( 'tags' == $column_name )
      $taxonomy = 'post_tag';
    elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
       $taxonomy = substr( $column_name, 9 );

    but when the code is iterating through the foreach loop, if $taxonomy does not match any of those 3 IF/ELSEIF statements, there is no ELSE statement so the $taxonomy variable carries the value of whatever it was the last time through the loop. This is incorrect and resulted in my 'Bacon' column being mistaken for the post_tags column which preceded it in the loop.

    This can be fixed either by:

    $taxonomy = '';
    if ( 'categories' == $column_name )
      $taxonomy = 'category';
    elseif ( 'tags' == $column_name )
      $taxonomy = 'post_tag';
    elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
       $taxonomy = substr( $column_name, 9 );

    or:

    if ( 'categories' == $column_name )
      $taxonomy = 'category';
    elseif ( 'tags' == $column_name )
      $taxonomy = 'post_tag';
    elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
       $taxonomy = substr( $column_name, 9 );
    else
       $taxonomy = '';

    which both accomplish the same thing.

  3. helgatheviking
    Member
    Posted 1 year ago #

    Cool!

Topic Closed

This topic has been closed to new replies.

About this Topic