• Hi

    What would be the best way to make the attachment (image) meta data required.

    The fields I’m talking about: Title, caption, alt text etc.

    Thanks
    Joren

Viewing 2 replies - 1 through 2 (of 2 total)
  • Media section is provided by default by WP and seems it didn’t have filter to make those fields required. Making those changes in core files will be bad idea.

    What we can do is that, whenever image is uploaded, based on image name, we can fill the meta data so that we can make sure they are not empty.

    Paste below code in your theme’s functions.php file

    
    add_action( 'add_attachment', 'set_image_meta_data_on_upload' );
    
    function set_image_meta_data_on_upload( $post_ID ) {
    	// Check if uploaded file is an image, else do nothing
    	if ( wp_attachment_is_image( $post_ID ) ) {
    		$image_title = get_post( $post_ID )->post_title;
    		$image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );
    		$image_title = ucwords( strtolower( $my_image_title ) );
    		$image_meta = array(
    			'ID' => $post_ID,
    			'post_title' => $image_title,
    			'post_excerpt' => $image_title,
    			'post_content' => $image_title,
    		);
    		update_post_meta( $post_ID, '_wp_attachment_image_alt', $image_title );
    		wp_update_post( $image_meta );
    	}
    }
    

    If you wish, later you can change the meta data as per your choice.

    Let me know if that works for you.

    Thread Starter jorenvh

    (@jorenvh)

    Hi

    Thanks for you reply.

    I wasn’t even considering making changes in core files ??

    The option you provided was something I found already but that’s not what I’m looking for. I need a way to make them required even when it’s only when they are added as a feature image or if they are added to the content of a post.

    If that’s not possible a way to alter the html printed their would be awesome, so that I can add the “required” value on those fields and add an extra paragraph with some extra guidelines.

    Any idea if this would be possible?

    Thx
    Joren

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Making attachment meta data required’ is closed to new replies.