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

admin

Administrator
Staff member
<br>
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>

<ol>
<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>
</ol>

<hr>

I've tried to resolve my problem by using:

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

   _code_goes_here_

}

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.

<hr>

<strong>UPDATE</strong><br><br>
I've tried this:<br>
Code:
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' );
}
<br>
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>
Code:
add_filter( 'image_make_intermediate_size-my-plugin-name_page_bpc-add-products', 'bpc_rename_prod_images' );
<br>
And again nothing :( I run out of ideas...

<hr>

<strong>UPDATE #2</strong><br><br>
I have managed to solve upload directory part of my problem with help of two hooks: <br><br>
Code:
add_filter('wp_handle_upload_prefilter', 'pre_upload_function', 2);
<br>
<br>- used to add upload_dir filter connected with function which changes destination folder for uploaded files<br><br>
Code:
add_filter('wp_handle_upload', 'post_upload_function', 2);
<br>
<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>