How to count media attachments?

Use this code if you’re in the loop: $attachments = get_children( array( ‘post_parent’ => $post->ID ) ); $count = count( $attachments ); If you’re not in the loop, substitute $post->ID with the ID of the specific post. But that should count all attachments.

Get attachments by user

Use a custom query for this. $user_id = 1; $the_query = new WP_Query( array( ‘post_type’ => ‘attachment’, ‘post_status’ => ‘inherit’, ‘author’ => $user_id) ); if ( $the_query->have_posts() ) while ( $the_query->have_posts() ) : $the_query->the_post(); the_title(); endwhile; This will show all the attachment titles for user with user id 1. You can use get_the_ID() in the … Read more

Add a term to an attachment submitted from front end

media handle sideload returns an attachment ID, and attachments are normal posts with the post type ‘attachment’. All the same things apply, featured images parents taxonomies post meta etc albeit with a few attachment oriented functions like wp_get_attachment_image So use wp_set_object_terms as you normally would, e.g.: $id = media_handle_sideload(…. if(!is_wp_error($id)){ wp_set_object_terms( $id, array(terms…), $taxonomy, $append … Read more

the_content() behavior on attachment.php versus single.php

This is correct behavior, if your attachment doesn’t actually have anything in post_content field (which is quite common). When post–centric templates run, prepend_attachment() is used as filter to “emulate” post content. This is not the case with template “intended” for attachments. If you look at template-loader.php: elseif ( is_attachment() && $template = get_attachment_template() ) : … Read more

Is it possible to modify the media library admin screen?

That area of all admin screens is where admin notices are displayed, so you could generate an admin notice. Here’s a suitable function and hook: function wpse_233031_admin_notice() { ?> <div class=”notice notice-info”> <p>Your message here!</p> </div> <?php } function wpse_233031_hook_admin_notice() { add_action( ‘admin_notices’, ‘wpse_233031_admin_notice’ ); } add_action( ‘load-upload.php’, ‘wpse_233031_hook_admin_notice’ ); /* Edited out anonymous function … Read more

How can i change an image’s author?

You can use wp_update_post() $my_post = array( ‘ID’ => $post_id, ‘post_author’ => $user_id, ); wp_update_post( $my_post ); Or change image’s author via a function using GravityForms uploader add_action(“gform_user_registered”, “image_author”, 10, 4); function image_author($user_id, $config, $entry, $user_pass) { $post_id = $entry[“post_id”]; $args = array( ‘post_parent’ => $post_id, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’ ); $attachments = … Read more

sanitize attachment filename

Following on from question comments. You can run a filter on sanitize_file_name_chars and add the degree symbol to the array of invalid chars, but it won’t halt the upload it will simply strip the file extension. However you can add another filter stop the upload, in a hacky kind of way by additionally hooking on … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)