admin管理员组文章数量:1134246
I'm creating a front-end based project where users can make posts and add images to them. It's a network of sites that users have access to.
For a better user experience, I want users to be able to see all their images posted on every site on the network, in case they want to reuse them. Uploads are handled by WP media library, and users can only see media posted on the current site.
With the code below, users are restricted to seeing only their own uploads within a subsite.
add_filter( 'ajax_query_attachments_args', 'show_this_user_attachments_only', 10, 1 );
function show_this_user_attachments_only( $query = array() ) {
if ( ! current_user_can( 'manage_options' ) ) {
$query[ 'author' ] = get_current_user_id();
}
return $query;
}
I've tried hooking into pre_get_posts
but it doesn't do a thing.
I know it would surely involve switch_to_blog()
to achieve this, but I just can't figure out how.
Several hours of searching hasn't yielded any help.
EDIT:
I created a simple form for creating and editing posts and I use the WP Editor as below, the values for the variables are worked out in the said form accordingly:
wp_editor( $content, $editor_id, array(
'tabindex' => 1,
'textarea_rows' => 10,
'editor_class' => 'flds',
'drag_drop_upload' => true
) );
And for the media library script, I have added:
wp_enqueue_media();
So, the media library loads the user's images as expected (I believe that's done via Ajax), given the restrictions by the first code.
THE MAIN ISSUE: Getting all the user's images from across the network to load among those displayed inside the media library.
So the problem area is in the media library's output.
Everything else about the form and the posting/editing process is working perfectly well.
I'm creating a front-end based project where users can make posts and add images to them. It's a network of sites that users have access to.
For a better user experience, I want users to be able to see all their images posted on every site on the network, in case they want to reuse them. Uploads are handled by WP media library, and users can only see media posted on the current site.
With the code below, users are restricted to seeing only their own uploads within a subsite.
add_filter( 'ajax_query_attachments_args', 'show_this_user_attachments_only', 10, 1 );
function show_this_user_attachments_only( $query = array() ) {
if ( ! current_user_can( 'manage_options' ) ) {
$query[ 'author' ] = get_current_user_id();
}
return $query;
}
I've tried hooking into pre_get_posts
but it doesn't do a thing.
I know it would surely involve switch_to_blog()
to achieve this, but I just can't figure out how.
Several hours of searching hasn't yielded any help.
EDIT:
I created a simple form for creating and editing posts and I use the WP Editor as below, the values for the variables are worked out in the said form accordingly:
wp_editor( $content, $editor_id, array(
'tabindex' => 1,
'textarea_rows' => 10,
'editor_class' => 'flds',
'drag_drop_upload' => true
) );
And for the media library script, I have added:
wp_enqueue_media();
So, the media library loads the user's images as expected (I believe that's done via Ajax), given the restrictions by the first code.
THE MAIN ISSUE: Getting all the user's images from across the network to load among those displayed inside the media library.
So the problem area is in the media library's output.
Everything else about the form and the posting/editing process is working perfectly well.
Share Improve this question edited Aug 17, 2023 at 15:19 ToongeePrime asked Aug 17, 2023 at 13:22 ToongeePrimeToongeePrime 418 bronze badges 5 |1 Answer
Reset to default 0You could create a cron job that gathers all the data and caches the results in a usermeta value for each user. It'd be slow to generate, but that wouldn't matter because it'd run in the background. The data would then be fast to retrieve on the page where you display the user's images.
The basic code would be something like:
add_action( 'init', 'schedule_cron_jobs' );
add_action( 'cache_user_images', 'cache_user_images' );
function schedule_cron_jobs() : void {
if ( ! wp_next_scheduled( 'cache_user_images' ) ) {
wp_schedule_event( time(), 'twicedaily', 'cache_user_images' );
}
};
function cache_user_images() : void {
$user_images = array();
$users = get_users(
array(
'number' => -1,
'fields' => 'ID',
)
);
$sites = get_sites(
array(
'number' => false,
'fields' => 'ids',
)
);
foreach ( $sites as $site_id ) {
switch_to_blog( $site_id );
foreach ( $users as $user_id ) {
$attachments = get_posts(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'author' => (int) $user_id,
'posts_per_page' => -1,
)
);
// Prune the cached data to only what's needed, to save disk space.
array_walk(
$attachments,
function( &$attachment ) use ( $site_id ) {
$attachment = array(
'site_id' => $site_id,
'post_id' => $attachment->ID,
'post_title' => $attachment->post_title,
'url' => wp_get_attachment_url( $attachment->ID ),
);
}
);
if ( $attachments ) {
$user_images[ $user_id ] = array_merge( $user_images[ $user_id ] ?? array(), $attachments );
}
}
restore_current_blog();
}
foreach ( $users as $user_id ) {
if ( ! empty( $user_images[ $user_id ] ) ) {
update_user_meta( $user_id, 'user_images', $user_images[ $user_id ] );
}
}
}
There's all kinds of optimizations you could make if needed, but it'll depend on number of sites/users, specific app needs, etc. For example:
- Run the cron every 10 minutes, but have it only work on 100 users during each execution.
- Restrict to users/sites queries to a subset, if only some users/sites apply.
- Set
count_total
andcount
tofalse
in the user and site queries, respectively, to avoid the extra query to get the total count.
One downside to this approach is that once a user uploads an image, it won't show up in the cache until the next cron execution. To solve that, you'll need to add some more code that will register a save_post
callback. That callback would append the attachment being uploaded to the user's cache, so that it's available immediately.
本文标签: uploadsHow can I get all attachments by a user on a WP multisite network
版权声明:本文标题:uploads - How can I get all attachments by a user on a WP multisite network? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736835760a1954887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_enqueue_media();
function as I've added. – ToongeePrime Commented Aug 17, 2023 at 15:23