• Hi,

    I have some trouble with images uploaded programmatically to my WordPress site. When I am trying to get the attachment metadata, there isn’t any available.

    So I tried creating my own function, which checks if wp_get_attachment_metadata is empty and then tries to generate it!

    Unfortunately, my code does nothing. It does not give any errors, what should I do?

    My function:

    function get_attachment_meta_data_sem($attch_id) {
    	$meta_data = wp_get_attachment_metadata($attch_id);
    	if($meta_data == ''){
    		$attachment_path = get_attached_file($attch_id);
    		$attach_data = wp_generate_attachment_metadata( $attch_id, $attachment_path);
    		wp_update_attachment_metadata( $attach_id,  $attach_data );
    		return $attach_data;
    	}else{
    		return wp_get_attachment_metadata($attch_id);
    	}
    }

    But in this example, $attach_data stays empty.

    Thank you in advance!

    • This topic was modified 8 years, 2 months ago by xvilo. Reason: Some typo's
    • This topic was modified 8 years, 2 months ago by xvilo.
    • This topic was modified 8 years, 2 months ago by xvilo. Reason: Better title
Viewing 1 replies (of 1 total)
  • Thread Starter xvilo

    (@xvilo)

    So I have figured it what was happening. For some reason I needed to include image.php. It seems a bit off to me, but well, yeah…

    New function code is:

    function get_attachment_meta_data_sem($attch_id) {
    	$meta_data = wp_get_attachment_metadata($attch_id);
    	if($meta_data == ''){
    		$attachment_path = get_attached_file($attch_id);
    		require_once( ABSPATH . 'wp-admin/includes/image.php' );
    		$attach_data = wp_generate_attachment_metadata( $attch_id, $attachment_path);
    		wp_update_attachment_metadata( $attach_id,  $attach_data );
    		return $attach_data;
    	}else{
    		return wp_get_attachment_metadata($attch_id);
    	}
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Generating attaachment metadata does not work’ is closed to new replies.