rwmb_{$field_id}_after_save_field for Image (multiple)

Support General rwmb_{$field_id}_after_save_field for Image (multiple)

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #48844

    Hello,

    I'm trying to update the alt text as described here:

    https://metabox.io/add-alt-text-automatically/

    But I'm using the Image field type with max_file_uploads set to 10. I'd like to use the rwmb_{$field_id}_after_save_field hook (or similar) to update the alt text for each image as saved. This hook appears to return the $object_id for the post, rather than the specific image saved.

    Thanks in advance.

    $prefix = 'my_';
    $meta_boxes[] = [
    		'id'         => 'image_id',
    		'title'      => __( 'My Image', 'my_plugin' ),
    		'post_types' => ['my_cpt'],
    		'context'    => 'normal',
    		'priority'   => 'high',
    		'fields'     => [
    				[
    					'id'               => $prefix . 'image',
    					'type'             => 'image',
    					'max_file_uploads' => 10,
    					'force_delete'     => false
    				],
    		],
    ];
    #48845

    If anyone else would like to update the title & alt text from a dashed/underscored file name for an Image field with multiple images, this worked for me (in a class) pulled together from a few different examples:

    
    add_action( 'rwmb_my_image_field_name_after_save_field', array( $this, 'image_title_alt' ), 10, 5 );
    public function image_title_alt( $null, $field, $new, $old, $object_id ) {
    	$images = rwmb_meta( 'my_image_field_name', array(), $object_id );
    	foreach ( $images as $image ) {
    		$title = $image['title'];
    		$title = str_replace( array( '-', '_' ), ' ', $title );
    		$title = ucwords( strtolower( $title ) );
    		update_post_meta( $image['ID'], '_wp_attachment_image_alt', $title );
    		wp_update_post(
    			array(
    				'ID'         => $image['ID'],
    				'post_title' => $title,
    			)
    		);
    	}
    }
    

    Cheers.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.