Are captions stored anywhere?

Yes, it stores the caption in it’s own place in the DB. I can’t quote the exact location but in WordPress, “Attachments” are a post type and it stores each attachment just like a post. For an attachment post type, it treats the Image Caption as the_excerpt the Image Description as the_content and the Image … Read more

Changing attachment urls?

You can do the following: /* add new rewrite rule */ function attachment_rewrite( $wp_rewrite ) { $rule = array( ‘media/(.+)’ => ‘index.php?attachment=” . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $rule + $wp_rewrite->rules; } add_filter( “generate_rewrite_rules’, ‘attachment_rewrite’ ); /* redirect standard wordpress attachments urls to new format */ function redirect_old_attachment() { global $wp; if( !preg_match( ‘/^media\/(.*)/’, $wp->request ) … Read more

How to cleanly delete attachment but retain original file?

Year and some later with much improved skills, behold: Keep_Deleted_Attachment_File::on_load(); /** * Keep original file when deleting attachments. */ class Keep_Deleted_Attachment_File { static private $file; static function on_load() { add_action( ‘init’, array( __CLASS__, ‘init’ ) ); } static function init() { add_action( ‘delete_attachment’, array( __CLASS__, ‘delete_attachment’ ) ); } /** * @param int $post_id attachment … Read more

Check if post has attachments (not image)

You can use the following within a loop: $files = get_attached_media( $type, $post_id ); Just define the attachment MIME type on $type. Second parameter is optional. Example from the Codex page: $media = get_attached_media( ‘audio’, 102 ); With the retrieved array, you can do something like: if( $media ) { //Show the post attachments that … Read more

getting attachement images src and add classes

If you only want to add an extra class, then you should use wp_get_attachment_image. It has few extra params, and the last one is used for setting class names. Sample usage: <?php echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’, “”, [“class” => “my-custom-class”] ); ?> The main advantage of this aproach is that you will also get the … Read more

Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails

I have checked your code, and I think you are missing the guid of the images. Please have a look at the code below: $post_id = 1234; $images = array(‘filename1.png’, ‘filename2.png’, … ‘filename10.png’); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); foreach($images as $name) { $attachment = array( ‘guid’=> $wp_upload_dir[‘url’] . “https://wordpress.stackexchange.com/” … Read more

Get all post attachments except featured image

For the first one, you can add ‘exclude’ => get_post_thumbnail_id() as a parameter (as shown here). For the second one, you can add ‘post_mime_type’ => ‘application/pdf’, but I’m not sure that would always work, afaik, pdfs have more than one mime type.

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