Skip Navigation

[Resolved] Save/Update the featured image title with the post title

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to set the uploaded image title in the form same as the created post title.

Solution:

Add the custom code below:

function update_featured_image_title_foto( $post_id, $form_data ) {
    // Check form being used, else do nothing  
    if ( $form_data[ 'id' ] == 485 ) {
        // get post title
        $custom_image_title = esc_html( get_the_title($post_id) );
        // get featured image ID
        $featured_image_id = get_post_thumbnail_id($post_id);
 
        // update post_title 
        $updated_image_data = array(
           'ID' => $featured_image_id,
           'post_title' => $custom_image_title,
        );
       
      // Update the field(s)
      wp_update_post($updated_image_data);
 
    }
}
add_action( 'cred_save_data', 'update_featured_image_title_foto', 15, 2 );

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 2 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Fred Konings 2 years, 9 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2279429

I would like to save/update the featured image title with the post title. The featured image will be used to display in a lightbox which is able to show the image_title and other image_caption fields.

I have an ADD_post form (form-id=485) for CPT called "Foto" where user needs to fill in the title, content and upload a featured image.
I have an EDIT_post form (form-id=487) for CPT called "Foto" where user is able to edit the title, content and uploaded featured image.

Now I would like the featured image title to be the same as the post_title.

I have tried something myself, but don't get it to work.

function update_featured_image_title_foto( $post_id, $form_data ) {
    if ( $form_data[ 'id' ] == 485, 487 ) {
        // get post title
        $custom_image_title = esc_html( get_the_title($post_id) );
        // get featured image ID
        $featured_image_id = get_post_thumbnail_id($post_id);
        // get featured image metadata
        $featured_image_metadata = wp_get_attachment_metadata( $featured_image_id );
        // get featured image title from metadata
        $featured_image_title = $featured_image_metadata['image_meta']['image_title'];

        $updated_image_data = array(
            'ID' => $featured_image_id,
            'image_title' => $custom_image_title,
            'image_name' => sanitize_title($custom_image_title),
        );
        update_post_meta($featured_image_id, 'image_title', $updated_image_data);
	}
}
add_action( 'cred_save_data', 'update_featured_image_title_foto', 10, 2 );

Could you help me to find a solution?

Thank you, Fred

#2279711

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You have two issues at hand:

1) Retrieving the featured image post title.

2) Updating the post title.

The retrieving featured image title part is not related to Toolset and you need to create a debug strategy by echoing the result with another hook to make sure you retrieve such info correctly.

The second part is where you want to update the title of the post. Try to test that by adding a sample title and see if it works. I suggest that you check this code sample to know how to update the post title:

https://toolset.com/forums/topic/change-title-from-cred/#post-1213002

So basically, what I suggest is that you check and see which portion of the code is the issue and work your way up from there ...

Thanks.

#2279729

Hi Christopher,

The post-title is already correct. My problem is, I want the title of the featured image to be the same as the post_title. How could I update the image_title ?

Thank you.

#2280667

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I thought you need something other way around.

We are unable to provide you with such a code as it is a pure WordPress development issue.

The only thing that we can give is some pointers to make sure you have the details to go forward.

From what I undestand the wp_get_attachment_metadata hook does not return the image title, so instead of using the cred_save_data hook take a vanilla WordPress approach and add your code on the WordPress add_attachment hook.

I searched and found this article that might help:

hidden link

Please consider that this is not a Toolset related issue and if you need help with the custom development you can consider hiring a contractor:
https://toolset.com/contractors/

Thanks.

#2281467

To give something back to the community and to help others,

I have found a solution:

function update_featured_image_title_foto( $post_id, $form_data ) {
	// Check form being used, else do nothing  
    if ( $form_data[ 'id' ] == 485 ) {
        // get post title
        $custom_image_title = esc_html( get_the_title($post_id) );
        // get featured image ID
        $featured_image_id = get_post_thumbnail_id($post_id);

		// update post_title 
        $updated_image_data = array(
           'ID' => $featured_image_id,
           'post_title' => $custom_image_title,
        );
      
      // Update the field(s)
      wp_update_post($updated_image_data);

	}
}
add_action( 'cred_save_data', 'update_featured_image_title_foto', 15, 2 );

Hopefully code is useful for other users.

Regards Fred

#2281841

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Thank you for sharing the code in the forum.

#2282699

My issue is resolved now. Thank you!