Password protect some uploaded files, so only logged-in users can view them

See my question and answer again, you linked it already in your question: simple solution for restricting access to (some) uploads/downloads The solution isn’t that hairy the only necessity – besides configuring the plugin correct – is to create a .htaccess, which you could do programmatically. For that you have to automate the creation of … Read more

Detect if is image unattached

For unattached images, the post_parent column in the wp_posts table, has the value of 0. Your gallery shortcode in that case is: which means you are calling get_children() with post_parent as 0. You can e.g. use the gallery shortcode callback gallery_shortcode() directly: if( $parent_id = $post->post_parent ) { echo gallery_shortcode( [ ‘id’ => (int) $parent_id, … Read more

How to add classes to images based on their categories?

This should hopefully do the trick: /** * Append the image categories to the current image class. * * @see http://wordpress.stackexchange.com/a/156576/26350 */ add_filter( ‘get_image_tag_class’, function( $class, $id, $align, $size ) { foreach( (array) get_the_category( $id ) as $cat ) { $class .= ‘ category-‘ . $cat->slug; } return $class; } , 10, 4 ); Testing … Read more

Insert an image into a post by API

You can set the uploaded image as the post thumbnail via, update_post_meta($post_id,’_thumbnail_id’,$attach_id); Example… if ($_FILES) { foreach ($_FILES as $file => $array) { if ($_FILES[$file][‘error’] !== UPLOAD_ERR_OK) { return “upload error : ” . $_FILES[$file][‘error’]; } $attach_id = media_handle_upload( $file, $post_id ); } } update_post_meta($post_id,’_thumbnail_id’,$attach_id); If you are uploading an array of images, then you … Read more

delete post also attachments

Maybe this works function remove_post() { if(isset($_POST[‘post_id’]) && is_numeric($_POST[‘post_id’])) { $post = get_post($_POST[‘post_id’]); if(get_current_user_id() == $post->post_author) { $args = array( ‘post_parent’ => $_POST[‘post_id’] ); $post_attachments = get_children($args); if($post_attachments) { foreach ($post_attachments as $attachment) { wp_delete_attachment($attachment->ID, true); } } wp_delete_post($_POST[‘post_id’], true); } } exit; } The code added function get_attachment_id_from_src ($image_src) { global $wpdb; $query = … Read more

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