在WordPress中上载图像时自动添加图像标题、标题、替代文本和描述

时间:2013-12-09 作者:On Secret Hunt

有人能告诉我如何在我的WordPress帖子中上传任何图像时,在标题、标题、alt文本和描述中自动填充/添加相同的图像标题吗。

desired screenshot

3 个回复
SO网友:jgraup

added_post_meta 看来是时候换个新形象了。不仅已经设置了默认元,而且该函数还为您提供$post_id 随着$meta_value 它保存附件元数据。从那里,您可以获取所有字段并设置所需的字段。

add_action(\'added_post_meta\', \'wpse_20151219_after_post_meta\', 10, 4);

function wpse_20151219_after_post_meta($meta_id, $post_id, $meta_key, $meta_value) {

    // _wp_attachment_metadata added
    if($meta_key === \'_wp_attachment_metadata\') {

        // ----------------------------------------------------------------------
        // POST
        // ----------------------------------------------------------------------

        // Change basic fields on attachment post
        wp_update_post(array(
                           \'ID\'           => $post_id,
                           \'post_title\'   => "This is a TITLE for $post_id",
                           \'post_content\' => "This is the DESCRIPTION for $post_id",
                           \'post_excerpt\' => "This is the CAPTION for $post_id",
                       ));

        // ----------------------------------------------------------------------
        // POST META
        // ----------------------------------------------------------------------

        // Change ALT Text
        update_post_meta($post_id, \'_wp_attachment_image_alt\', "This is the ALT Text for $post_id");

        // Add Custom Field
        update_post_meta($post_id, \'_wpse_20121219_my_custom_meta\', \'MyCustomMetaValue\');

        // ----------------------------------------------------------------------
        // POST META ( ATTACHMENT METADATA )
        // ----------------------------------------------------------------------

        // Change Image Metadata
        $meta_value[ \'image_meta\' ] = array_merge($meta_value[ \'image_meta\' ], array(
            \'credit\'    => \'https://black-buddha.com\',
            \'camera\'    => \'The Best Camera EVER!\',
            \'copyright\' => date(\'Y\'),
            \'title\'     => "This is a META TITLE for $post_id",
            \'caption\'   => "This is a META CAPTION for $post_id",
        ));

        // Update The Image Metadata
        wp_update_attachment_metadata($post_id, $meta_value);

        // _wp_attached_file
        // _wp_attachment_metadata (serialized)
        // _wp_attachment_image_alt
        // _wpse_20121219_my_custom_meta

        $attachment_meta = get_post_meta($post_id);

        // width
        // height
        // file
        // sizes
        // image_meta
        //      aperture
        //      credit
        //      camera
        //      caption
        //      created_timestamp
        //      copyright
        //      focal_length
        //      iso
        //      shutter_speed
        //      title
        //      orientation
        //      title
        //      keywords

        $attachment_metadata = wp_get_attachment_metadata($post_id);
    }
}

SO网友:Webloper

您可以从wp includes/post连接到“add\\u attachment”操作。php行:3332。(Version 4.4) 它传入post\\u id,从那里您可以获得文件名,然后使用所需的任何内容更新post meta。

Reference taken from

add_action( \'add_attachment\', \'wpse_125805_add_image_meta_data\' );

function wpse_125805_add_image_meta_data( $attachment_ID ) {

    $filename   =   $_REQUEST[\'name\']; // or get_post by ID
    $withoutExt =   preg_replace(\'/\\\\.[^.\\\\s]{3,4}$/\', \'\', $filename);
    $withoutExt =   str_replace(array(\'-\',\'_\'), \' \', $withoutExt);

    $my_post = array(
        \'ID\'           => $attachment_ID,
        \'post_excerpt\' => $withoutExt,  // caption
        \'post_content\' => $withoutExt,  // description
    );
    wp_update_post( $my_post );

    // update alt text for post
    update_post_meta($attachment_ID, \'_wp_attachment_image_alt\', $withoutExt );
}

SO网友:Arun Basil Lal

要获得更简单的解决方案,可以使用this 我不久前制作的WordPress插件。

该插件还附带了一个批量更新程序,如果您愿意的话,它可以更新媒体库中已有图像的图像属性。

结束

相关推荐

将wp_title()从页面ID获取到变量

我很确定这是不可能的,但我能得到wp_title() 如果我知道post的ID,就可以将其转换为变量?例如,我在一个“blog”页面上,我想在一个变量中包含“about”页面的标题(而不是在<title> 标记如下问题:Setting title using wp_title filter).