I want to automatically add caption for each picture appears in the name of the image or the alternative text
example Caption add automatic
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI want to automatically add caption for each picture appears in the name of the image or the alternative text
example Caption add automatic
You can add programmatically caption, alt, description every time you upload image at backend.
https://brutalbusiness.com/automatically-set-the-wordpress-image-title-alt-text-other-meta/
I tried myself this and worked nice as your expectation. This is for all your future upload images.
Try this and let me know.
/* Automatically set the image Title, Alt-Text, Caption & Description upon upload
--------------------------------------------------------------------------------------*/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Sanitize the title: remove hyphens, underscores & extra spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );
// Sanitize the title: capitalize first letter of every word (other letters lower case):
$my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
'ID' => $post_ID, // Specify the image (ID) to be updated
'post_title' => $my_image_title, // Set image Title to sanitized title
'post_excerpt' => $my_image_title, // Set image Caption (Excerpt) to sanitized title
'post_content' => $my_image_title, // Set image Description (Content) to sanitized title
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}
To put in a plugin, these solutions came from eskimo and sam.. we simply select all the attachment where the post_excpert is empty and udpate it , put the title of it in description and caption when they are empty
$s_query = "SELECT ID, post_title FROM `wp_posts` WHERE NOT post_content <=> post_title AND post_type='attachment'";
$r_result = mysql_query($s_query);
if($r_result)
{
while($row = mysql_fetch_row($r_result)){
$s_update_query = 'UPDATE `wp_posts` SET post_content="'.$row[1].'", post_excerpt="'.$row[1].'" WHERE ID="'.$row[0].'"';
mysql_query($s_update_query);
}
}
Here another similar query for the same result:
mysql_select_db(DB_NAME)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM wp_posts WHERE post_type= 'attachment' AND post_excerpt= ''";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
$query = "UPDATE wp_posts SET post_excerpt=post_title WHERE post_type='attachment' AND post_excerpt=''";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
So with these solution we can import(SELECT) many image in wp gallery.Firstly the caption doesn't appear, we need to update (UPDATE)the post to see it in front end.
{}
symbol above the code to add it in a code block like in the edit I've done
Jun 4, 2014 at 13:44
wpdb
class.