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; } |