0

I'm getting the following error in my wordpress dashboard which correlates to a piece of code i've written, however I'm not sure how to fix the error:

Notice: Undefined index: draft in /home/sites/samskirrow.com/public_html/skizzar-test/wp-content/mu-plugins/skizzar-dashboard.php on line 331

Here is the code it's referring to:

/******************************************************************
/*  REMOVE BRACKETS AROUND PAGE COUNTS
/******************************************************************/
foreach( array( 'edit-post', 'edit-page', 'edit-movie', 'upload' ) as $hook )
    add_filter( "views_$hook" , 'wpse_30331_custom_view_count', 10, 1);

function wpse_30331_custom_view_count( $views ) 
{
    global $current_screen;
    switch( $current_screen->id ) 
    {
        case 'edit-post':
            $views = wpse_30331_manipulate_views( 'post', $views );
            break;
        case 'edit-page':
            $views = wpse_30331_manipulate_views( 'page', $views );
            break;
        case 'edit-movie':
            $views = wpse_30331_manipulate_views( 'movie', $views );
            break;
        case 'upload':
            $views = wpse_30331_manipulate_views( 'attachment', $views );
            break;
    }
    return $views;
}

function wpse_30331_manipulate_views( $what, $views )
{
    global $user_ID, $wpdb;

    /*
     * This is not working for me, 'artist' and 'administrator' are passing this condition (?)
     */
    if ( !current_user_can('artist') ) 
        return $views;

    /*
     * This needs refining, and maybe a better method
     * e.g. Attachments have completely different counts 
     */
    $total = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending') AND (post_author = '$user_ID'  AND post_type = '$what' ) ");
    $publish = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = '$user_ID' AND post_type = '$what' ");
    $draft = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = '$user_ID' AND post_type = '$what' ");
    $pending = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'pending' AND post_author = '$user_ID' AND post_type = '$what' ");
    $trash = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'trash' AND post_author = '$user_ID' AND post_type = '$what' ");

     /*
     * Only tested with Posts/Pages
     * - there are moments where Draft and Pending shouldn't return any value
     */
    $views['all'] = preg_replace( '/\(.+\)/U', ''.$total.'', $views['all'] ); 
    $views['publish'] = preg_replace( '/\(.+\)/U', ''.$publish.'', $views['publish'] ); 
    $views['draft'] = preg_replace( '/\(.+\)/U', ''.$draft.'', $views['draft'] ); 
    $views['pending'] = preg_replace( '/\(.+\)/U', ''.$pending.'', $views['pending'] ); 
    $views['trash'] = preg_replace( '/\(.+\)/U', ''.$trash.'', $views['trash'] ); 

    return $views;
} 

I know that the issue is that certain parameters aren't found in the db (for example, there may not be any "pending" posts) so it throws out an error. How can I code my way around this, for example, to say, if the parameter exists...

The goal here is to remove the '(' ')' brackets from post counts in wordpress. So when you are on the view posts, or view pages screen, there is a sub menu above the lists of posts/pages with the following items 'all', 'published', 'trash', 'pending' - these are filters that the user can click on. Next to each filter is a number, the number of posts/pages within that filter. The format of this count is ([NUMBER]) - This code is to simply get rid of those brackets.

Sam Skirrow
  • 3,647
  • 15
  • 54
  • 101
  • 4
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – u_mulder Jan 06 '16 at 15:52
  • I don't understand quite right what this code is supposed to do - could you add some more details about the goal you're trying to achieve? – vard Jan 06 '16 at 16:11
  • @vard - I've added my goals to my description – Sam Skirrow Jan 06 '16 at 16:16

2 Answers2

2

I noticed by google'ing a bit that you're using the code form this Wordpress SE answer. This doesn't match your needs, as this code is here to recalculate the number of posts after some manipulation on that by the OP.

To just remove the parenthesis, look at the following function in class-wp-list-table.php:

public function views() {
    $views = $this->get_views();
    $views = apply_filters( "views_{$this->screen->id}", $views );
    if ( empty( $views ) )
        return;
    $this->screen->render_screen_reader_content( 'heading_views' );
    echo "<ul class='subsubsub'>\n";
    foreach ( $views as $class => $view ) {
        $views[ $class ] = "\t<li class='$class'>$view";
    }
    echo implode( " |</li>\n", $views ) . "</li>\n";
    echo "</ul>";
}

This is the function that build the user menu that you try to change. If you look closer at this, you will see that the $view being echo'ed come from a $views that is being filtered previously:

$views = apply_filters( "views_{$this->screen->id}", $views )

$this->screen->id stand for the ID of the current screen, like edit-post for example.

So by adding this simple filter you will achieve what you want to do here:

add_filter('views_edit-post', 'remove_count_parenthesis', 10, 1);
function remove_count_parenthesis($views) {
    foreach($views as $key => $view) {
        $views[$key] = str_replace(array('(', ')'), '', $view);
    }
    return $views;
}
Community
  • 1
  • 1
vard
  • 4,057
  • 2
  • 26
  • 46
  • This is great @vard - although I'm getting a syntax error syntax error, unexpected ';' (referring to the ';' after $view in the foreach statement) – Sam Skirrow Jan 06 '16 at 16:37
1

You need to check if there is a value 'draft' in your array

if (isset( $views['draft'] ))  {
 $views['draft'] = preg_replace( '/\(.+\)/U', ''.$draft.'', $views['draft'] ); 
}
Simon Pollard
  • 2,476
  • 1
  • 17
  • 26
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • This is along the right lines, I do want to check if the variable even exists, however, this code returns Fatal error: Call to undefined function issset() – Sam Skirrow Jan 06 '16 at 16:13
  • Its isset (two s's) I have edited the answer - that should work - you should wrap all your code in a similar check or consider a loop if you are repeating code – Simon Pollard Jan 06 '16 at 16:22