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
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.
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.
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.
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
Hi there,
Thank you for sharing the code in the forum.
My issue is resolved now. Thank you!