Is there a way to restrict wordpress actions and filters to my plugin admin submenu page?


Staff member
I'm working on a plugin, where part of functionality is image upload. I've managed to add actions and filters, which change upload destination folder, rename uploaded files, add two new sizes, and unsets default wordpress image sizes.<br><br>
Now, everything works great here, but I have one really big issue:<br>
All those settings remain active even when I upload image with help of WP Media Library.<br><br> I want those actions and filters to apply ONLY when user is using uploader on my plugin admin page. I want to avoid polluting web space with unnecesary image versions (and my plugin is going to use only sizes defined by me and original one) <strike>and I want to keep files in uploads subfolder created by my plugin, but only when user is uploading from plugin admin page.</strike><br>
In other words:<br>

<li>If user uploads image from my plugin admin menu -> apply actions and filters. </li>
<li>If user uploads image from standard wordpress Media menu page -> do not apply actions/filters.</li>


I've tried to resolve my problem by using:

$screen = get_current_screen();
if ( in_array( $screen-&gt;id, array( 'my-admin-submenu-slug' ))) {



But it failed miserably. Result was "false" in my uploader form input, so I guess slugs are not proper info to compare here or I'm missing something.<br><br>

Is there any solution to this problem? I've spent last 3 days googling and trying to come up with solution, and nothing.


I've tried this:<br>
if ($_REQUEST['page'] == 'bpc-add-products'){
    echo '&lt;p&gt;Blebleblebleble&lt;/p&gt;';
    add_filter( 'image_make_intermediate_size', 'bpc_rename_prod_images' );
    add_action( 'init', 'bpc_addimagesizes' );
    add_filter( 'intermediate_image_sizes_advanced', 'bpc_filter_image_sizes' );
And the result: echo is printed only on bpc-add-products page - ok, that means my IF works. BUT filters and action are completely ignored.<br><br>
I've also tried adding page suffix to hook names, like that:<br>
add_filter( 'image_make_intermediate_size-my-plugin-name_page_bpc-add-products', 'bpc_rename_prod_images' );
And again nothing :( I run out of ideas...


<strong>UPDATE #2</strong><br><br>
I have managed to solve upload directory part of my problem with help of two hooks: <br><br>
add_filter('wp_handle_upload_prefilter', 'pre_upload_function', 2);
<br>- used to add upload_dir filter connected with function which changes destination folder for uploaded files<br><br>
add_filter('wp_handle_upload', 'post_upload_function', 2);
<br>- used to remove upload_dir filter after upload, so when you use Wordpress Media menu, your uploads will land in default folder<br><br>