0

I wanted to load a custom sidebar when doing an Ajax call, so I found the way with:

add_filter( 'loop_start', 'my_sidebar_widget', 25 );

but it is showing the sidebar even in media admin panel, so I thought about doing:

if( !is_admin() ) {
    add_filter( 'loop_start', 'my_sidebar_widget', 25 );
}

but it was still loaded in the media admin panel, so I tried:

add_filter( 'loop_start', 'my_sidebar_widget', 25 );

if( is_admin() ) {
    remove_filter( 'loop_start', 'my_sidebar_widget', 25 );
}

but in this way, the sidebar is not loaded on the Ajax call. Any idea of how to solve it?

1 Answer 1

0

is_admin() returns true if you're doing an Ajax request. So this is why my code didn't work. Instead I've done this:

add_filter( 'loop_start', 'my_sidebar_widget', 25 );

function my_sidebar_widget() {
    if ( is_active_sidebar( 'my_sidebar' ) && (defined( 'DOING_AJAX' ) && DOING_AJAX ) || !is_admin() ) { 
        echo '<div id="my_sidebar">';
        dynamic_sidebar('my_sidebar');
        echo '</div>';

    }
}

defined( 'DOING_AJAX' ) && DOING_AJAX returns true if you're doing an Ajax call, and !is_admin will work if you're not doing an Ajax request. In this way, it is not shown in the Media Admin Panel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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