Hi there,
I’m using `acf_form’ to create a front end form where users can create posts. To do so, I have a WYSIWYG fields, which accordingly I have set to show media upload buttons.
What I would like to do is restrict the media library to only files either 1) files uploaded by the current user – or – 2) files already attached to a post.
In other words, make the WYSIWYG field work like the standard ACF image field type where there is a ‘Library / Limit the media library choice’ option.
Thanks in advance!
Hi @ryandorn
You would probably need to modify the query yourself.
Here’s an example that limits the query when visiting the uploader in admin. You need to do something like this but for when it’s triggered on your frontend form.
//Manage Your Media Only
function mymo_parse_query_useronly( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
if ( !current_user_can( 'update_core' ) ) {
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
add_filter('parse_query', 'mymo_parse_query_useronly' );
Hi Jonathan,
Thanks for the response!
That code didn’t work for me straightaway so I keep digging and happened to stumble across this, which is working for me straight out of the box.
add_filter( 'posts_where', 'devplus_attachments_wpquery_where' );
function devplus_attachments_wpquery_where( $where ){
global $current_user;
if( is_user_logged_in() ){
// we spreken over een ingelogde user
if( isset( $_POST['action'] ) ){
// library query
if( $_POST['action'] == 'query-attachments' ){
$where .= ' AND post_author='.$current_user->data->ID;
}
}
}
return $where;
}
Thanks again!
The topic ‘Restrict Media Library acf_form’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.