0

I have a plugin which has code in it which has broken the new WP 4.0 media library grid view. The grid view itself (the rest of the page is fine) won't display if my plugin is active and this option is checked. My code is getting the current screen and displaying a bar with info about it (just below the admin bar) if the user has this option checked. Here is the code that was working properly prior to WP 4.0 release:

function sbah_show_current_screen() {
    $screenoptions = get_option('sbah_options');
    if(isset($screenoptions['sbah_view_screen'][0])) {
        $takenscreen = $screenoptions['sbah_view_screen'][0];
    }
    if(isset($takenscreen)) { 
        function my_current_screen($screen) {
            if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return $screen;
            $idofscreen = $screen->id;
            echo "<div id='screennotice' style='border:1px solid black;background:white;padding:5px;'><strong>This is your current screen ID ( You can turn this off <a href='/wp-admin/edit.php?post_type=sbah_helptab&page=sbah_options'>here</a> ):</strong>  <span style='color:red;'>".$idofscreen. "</span></div>";
            return $screen;
        }
        add_action('current_screen', 'my_current_screen' );
    }
}
add_action( 'admin_menu', 'sbah_show_current_screen' );

My bar still displays just fine, the only thing that seems to be broken is the grid view of the media library. Even the list view of the media library still works just fine.

  • 1
    current_screen is obviously way too early to print content. Just use a proper action. – fuxia Sep 8 '14 at 12:47
  • Thanks, I was just figuring that out as you left this and have modified my code accordingly. – Stephen Sep 8 '14 at 12:49
0

Ok, I reworked the whole thing because I was obviously printing out my info prior to where I should have. It now looks like this:

//SHOW SCREEN IF OPTION CHECKED

add_action( 'admin_notices', 'sbah_show_current_screen' );
function sbah_show_current_screen() {
    $screenoptions = get_option('sbah_options');
    if(isset($screenoptions['sbah_view_screen'][0])) {
        $takenscreen = $screenoptions['sbah_view_screen'][0];
    }
    if(isset($takenscreen)) { 
        if( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
        global $current_screen;
        echo "<div id='screennotice' style='border:1px solid black;background:white;padding:5px;margin-top:15px;float:left;'><strong>This is your current screen ID ( You can turn this off <a href='/wp-admin/edit.php?post_type=sbah_helptab&page=sbah_options'>here</a> ) :</strong>  <span style='color:red;'>".$current_screen->id. "</span></div><div style='clear:both;'></div>";
    }
}

I also took the opportunity to combine this into one action, much better. Live and learn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.