4

I am developing a plugin in which i am required to allow front end users to upload media, which I am able to do. But I don't want to let them sneak into media library which comes as tab with uploader screen.

I tried the following

function remove_medialibrary_tab($tabs) {
    if ( !current_user_can( 'administrator' ) ) {
        unset($tabs['library']);
        return $tabs;
    }
    else
    {
        return $tabs;
    }
}
add_filter('media_upload_tabs','remove_medialibrary_tab');

But seems this filter is deprecated and doesn't works anymore.

  • Questions are meant to be references for later visitors as well. For future questions: format your spelling/punctuation/grammar/capitalization so it as as easy to read as possible. Thanks. – kaiser Dec 7 '13 at 15:17
  • I will keep that in mind in future. Thankyou for edit – codepixlabs Dec 7 '13 at 18:03
  • Any progress on this? – kaiser Feb 8 '15 at 18:35
7

This function will not show media library tab in upload screen

   function remove_medialibrary_tab($strings) {
        if ( !current_user_can( 'administrator' ) ) {
            unset($strings["mediaLibraryTitle"]);
        return $strings;
        }
        else
        {
            return $strings;
        }
    }
    add_filter('media_view_strings','remove_medialibrary_tab');

I found out that switching to media library tab actually call this ajax action query-attachments. So i added another callback function to this action with top priority, which checks if user is not admin , the action halts right there. This did the trick for me :)

function restrict_non_Admins(){

        if(!current_user_can('administrator')){
            exit;
        }
    }

add_action('wp_ajax_query-attachments','restrict_non_Admins',1);
add_action('wp_ajax_nopriv_query-attachments','restrict_non_Admins',1);
  • Nice to see that you found a solution. Some notes: Don't use the level_*-capabilities as those are deprecated. The last argument (4th) is the number of arguments for the callback. If somethings doesn't have an argument, you can simply leave it away. You can as well leave it away when it's only 1 argument as this is the default. – kaiser Dec 8 '13 at 3:11
  • thankyou for the suggestions, I made changes to my code. – codepixlabs Dec 8 '13 at 3:53
  • Adding the first set of code in your answer here to my theme's function.php file helped me. Thanks! – jennetcetera Apr 3 '15 at 15:32
2

The function media_upload_tabs() is not deprecated. It is called only by the_media_upload_tabs() (perfect naming, I know), which as well is only called once by media_upload_header()...

Edit - Just tested the filter with a plugin and while it's working and a var_dump( $tabs ) in an attached callback spits out the tabs, it doesn't do anything. The reason seems to be that all this now is generated by backbone and underscore templates with JavaScript. The file responsible for all the templates is ~/wp-includes/media-template.php. From a search through that, there's no filter to alter the output. Basically it's just one massive PHP function named wp_print_media_templates() that wraps all the templates and that has some actions before and after different templates.

I just tried to console.log() the wp object and found wp.media.view which seems to hold ... something. ~/wp-includes/js/media-modals.js seems to be the root of all that JavaScript generated UI. Now when I dig deeper, I find wp.template that gets "underscore-extended" into wp.media.template. Then I found the reference to ~/wp-includes/js/wp-util.js which then explains, that the argument for that JS function is an #id that is prefixed by tmpl-.

Now that only is a wrapper for _.memoize() which adds a custom callback "saves" computed results to "speed things up"... a bit. The contents are just a bunch of Regex-es that then get pushed to a function named compiled(), which just is (again) a wrapper for

_.template( $( '#tmpl-' + id ).html(), null, options )

.. ok. There's as well stuff from ~/wp-includes/js/media-views.js and ~/wp-includes/js/media-models.js involved and as I know close to nothing about Backbone anymore, I got no clue how to proceed from there. I'll leave my debugging plugin for this answer as well as the answer as starting point for others here.

<?php
defined( 'ABSPATH' ) or exit;
/**
 * Plugin Name: (#125669) Media Upload Tabs by role
 * Description: Removes media upload tabs on a by-role base.
 */

add_action( 'admin_footer', 'wpse125669MediaUploadTabsRemove', 0 );
function wpse125669MediaUploadTabsRemove()
{
    if ( "edit.php" !== get_current_screen()->parent_file )
        return;

    ?>
<script type="text/javascript">
console.log( wp.media );
</script>
    <?php
}
  • 1
    wow thats so much information, enough info to spin my head.. which shows how dedicated you are .thankyou :) .. for my problem i found out a solution(work around) which i am posting as answer below .. that might not be the best way but that worked out for me .. – codepixlabs Dec 7 '13 at 17:52

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.