Only files uploaded by the current user are displayed in the WordPress multi-media library.

Source: Internet
Author: User


Only files uploaded by the current user are displayed in the media library of wordpress. Also used in acf_form

The code is as follows: Copy code

// Wordpress show only media user has uploaded
Add_action ('pre _ get_posts ', 'ml _ restrict_media_library ');
 
Function ml_restrict_media_library ($ wp_query_obj ){
Global $ current_user, $ pagenow;
If (! Is_a ($ current_user, 'WP _ user '))
Return;
If ('admin-ajax. Php '! = $ Pagenow | $ _ REQUEST ['action']! = 'Query-attachances ')
Return;
If (! Current_user_can ('manage _ media_library '))
$ Wp_query_obj-> set ('author', $ current_user-> ID );
Return;
}

The is_a () function is obsolete. The instanceof type operator is used since PHP 5. In the above example, PHP 5 will look like this:

The code is as follows: Copy code

// Wordpress show only media user has uploaded
Add_action ('pre _ get_posts ', 'ml _ restrict_media_library ');
 
Function ml_restrict_media_library ($ wp_query_obj ){
Global $ current_user, $ pagenow;
// If (! Is_a ($ current_user, 'WP _ user '))
If ($ current_user instanceof WP_User)
Return;
If ('admin-ajax. Php '! = $ Pagenow | $ _ REQUEST ['action']! = 'Query-attachances ')
Return;
If (! Current_user_can ('manage _ media_library '))
$ Wp_query_obj-> set ('author', $ current_user-> ID );
Return;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.