如何在图片上传对话框中设置“Alt Text”的默认值?

时间:2015-03-24 作者:gvanto

我正在使用WP 4.1.1,并试图在上载图像时为“Alt text”创建一个默认值(将其直接放到帖子中并显示对话框)。

我试过用这个钩子https://codex.wordpress.org/Plugin_API/Filter_Reference/attachment_fields_to_edit

虽然我似乎可以添加一个新字段(它出现在对话框窗体上),但我无法修改“Alt Text”字段的值。因此,以下方法不起作用(但根据文档,应该如此!)。我也尝试过将优先级从高更改为非常低,没有区别。

add_filter(\'attachment_fields_to_edit\', \'dpcontent_attachment_fields_to_edit\', 999, 2 );

function dpcontent_attachment_fields_to_edit($form_fields, $post) {

    if ( substr( $post->post_mime_type, 0, 5 ) == \'image\' ) {        
        _log("DBG: 1 in here ... form_fields=" . arr_to_string($form_fields)); // its empty :-(

        $form_fields[\'image_alt\'] = array(
            \'value\' => \'Hello world!\',
            \'label\' => __(\'Alternative Text\'),
            \'show_in_model\' => true
        );
    }

    return $form_fields;
}
如果您有任何帮助,我们将不胜感激!

格万托

2 个回复
SO网友:vralle

好的,答案不会很短

首先,不能使用更改alt属性attachment_fields_to_edit, 因为您无法修改默认字段。要了解其工作原理,请参阅的源代码get_compat_media_markup.

具有attachment_fields_to_edit 只能添加其他输入字段。

示例:

/**
 * Add custom field for Images
 *
 * @param array   $fields An array of attachment form fields.
 *                field arguments:
 *                    array(
 *                      \'show_in_edit\'  => (bool)  Show in Edit Screen. Default true,
 *                      \'show_in_modal\' => (bool)  Show in Modal Screen. Default true,
 *                      \'label\'         => string  label text,
 *                      \'input\'         => string  input type: text, textarea, etc or \'html\' key with custom input html callback. Default \'text\'
 *                      \'required\'      => (bool)  Input attributte \'required\'. Default false,
 *                      \'html\'          => string  custom input html or callback name,
 *                      \'extra_rows\'    => array(),
 *                      \'helps\'         => string  help text,
 *                    )
 * @param WP_Post $post        The WP_Post attachment object.
 */
function add_attachment_fields($fields, $post)
{
  if (substr($post->post_mime_type, 0, 5) == \'image\') {
    $meta = wp_get_attachment_metadata($post->ID);
    $fields[\'meta_credit\'] = array(
      \'label\'       => __(\'Credit\'),
      \'input\'       => \'text\',
      \'value\'       => $meta[\'image_meta\'][\'credit\'],
      \'helps\'       => __(\'Only text. Max length 40 characters\'),
      \'error_text\'  => __(\'Error credit meta\')
    );
  } 
  return $fields;
}
add_filter(\'attachment_fields_to_edit\', \'add_attachment_fields\', 10, 2);
要保存值,请使用attachment_fields_to_save:

/**
 * Filters the attachment fields to be saved.
 * @param array $post       An array of post data.
 * @param array $attachment An array of attachment metadata.
 */
function update_attachment_fields($post, $attachment)
{
  if (isset($attachment[\'meta_credit\'])) {
    $credit = $attachment[\'meta_credit\'];
    $meta = wp_get_attachment_metadata($post[\'ID\']);
    if ($credit !== $meta[\'image_meta\'][\'credit\']) {
        $meta[\'image_meta\'][\'credit\'] = $credit;
        wp_update_attachment_metadata($post[\'ID\'], $meta);
    }
  }
  return $post;
}
add_filter(\'attachment_fields_to_save\', \'update_attachment_fields\', 10, 2);
如何处理alt属性?没什么好主意,虽然在数据库中保留垃圾不太好,但可以:

add_attachmentadd_post_meta 添加附件时,变体会触发一次:

add_action(\'add_attachment\', \'add_alt_to_attachment\');
function add_alt_to_attachment($post_ID)
{
    add_post_meta($post_ID, \'_wp_attachment_image_alt\', \'MY TEXT\');
}
任何字段更改时激发:

function update_alt_field($post, $attachment)
{
  if (empty($attachment[\'image_alt\'])) {
    $image_alt = wp_unslash(\'My Alt is good\');
    $image_alt = wp_strip_all_tags($image_alt, true);
    // Update_meta expects slashed.
    update_post_meta($attachment_id, \'_wp_attachment_image_alt\', wp_slash( $image_alt));
  }
  return $post;
}
add_filter(\'attachment_fields_to_save\', \'update_alt_field\', 10, 2);
和其他方式-在post中插入图像时添加alt文本:

/**
 * Filter the list of attachment image attributes.
 *
 * @param array            $attrs        Attributes for the image markup.
 * @param WP_Post        $attachment    Image attachment post.
 * @param string|array    $size        Requested size. Image size or array of width and height values
 *                                 (in that order). Default \'thumbnail\'.
 * @return array        $attrs        Attributes for the image markup.
 */
function my_images_attr($attrs, $attachment, $size)
{
    if (empty($attrs[\'alt\'])) {
        $attrs[\'alt\']) = \'MY TEXT\';
    }
    return $attrs;
}
add_filter(\'wp_get_attachment_image_attributes\', \'my_images_attr\', 10, 3);

SO网友:bubblez

我找到了一个解决方案,可以为所有新上载的附件添加默认标题:

function add_caption_to_attachment($data, $postarr){
  if(empty(trim($data[\'post_excerpt\']))){
    $data[\'post_excerpt\'] = \'«<a href="">TITEL</a>» - <a href="">NAME</a>, CC <a href="">XXX</a>\';
  }
  return $data;
}
add_filter(\'wp_insert_attachment_data\', \'add_caption_to_attachment\');
@vralle提出的解决方案对实际的附件元数据很有效,但标题却不行。所以我不得不找到另一种方法,add\\u附件挂钩不起作用。

结束

相关推荐

如何在图片上传对话框中设置“Alt Text”的默认值? - 小码农CODE - 行之有效找到问题解决它

如何在图片上传对话框中设置“Alt Text”的默认值?

时间:2015-03-24 作者:gvanto

我正在使用WP 4.1.1,并试图在上载图像时为“Alt text”创建一个默认值(将其直接放到帖子中并显示对话框)。

我试过用这个钩子https://codex.wordpress.org/Plugin_API/Filter_Reference/attachment_fields_to_edit

虽然我似乎可以添加一个新字段(它出现在对话框窗体上),但我无法修改“Alt Text”字段的值。因此,以下方法不起作用(但根据文档,应该如此!)。我也尝试过将优先级从高更改为非常低,没有区别。

add_filter(\'attachment_fields_to_edit\', \'dpcontent_attachment_fields_to_edit\', 999, 2 );

function dpcontent_attachment_fields_to_edit($form_fields, $post) {

    if ( substr( $post->post_mime_type, 0, 5 ) == \'image\' ) {        
        _log("DBG: 1 in here ... form_fields=" . arr_to_string($form_fields)); // its empty :-(

        $form_fields[\'image_alt\'] = array(
            \'value\' => \'Hello world!\',
            \'label\' => __(\'Alternative Text\'),
            \'show_in_model\' => true
        );
    }

    return $form_fields;
}
如果您有任何帮助,我们将不胜感激!

格万托

2 个回复
SO网友:vralle

好的,答案不会很短

首先,不能使用更改alt属性attachment_fields_to_edit, 因为您无法修改默认字段。要了解其工作原理,请参阅的源代码get_compat_media_markup.

具有attachment_fields_to_edit 只能添加其他输入字段。

示例:

/**
 * Add custom field for Images
 *
 * @param array   $fields An array of attachment form fields.
 *                field arguments:
 *                    array(
 *                      \'show_in_edit\'  => (bool)  Show in Edit Screen. Default true,
 *                      \'show_in_modal\' => (bool)  Show in Modal Screen. Default true,
 *                      \'label\'         => string  label text,
 *                      \'input\'         => string  input type: text, textarea, etc or \'html\' key with custom input html callback. Default \'text\'
 *                      \'required\'      => (bool)  Input attributte \'required\'. Default false,
 *                      \'html\'          => string  custom input html or callback name,
 *                      \'extra_rows\'    => array(),
 *                      \'helps\'         => string  help text,
 *                    )
 * @param WP_Post $post        The WP_Post attachment object.
 */
function add_attachment_fields($fields, $post)
{
  if (substr($post->post_mime_type, 0, 5) == \'image\') {
    $meta = wp_get_attachment_metadata($post->ID);
    $fields[\'meta_credit\'] = array(
      \'label\'       => __(\'Credit\'),
      \'input\'       => \'text\',
      \'value\'       => $meta[\'image_meta\'][\'credit\'],
      \'helps\'       => __(\'Only text. Max length 40 characters\'),
      \'error_text\'  => __(\'Error credit meta\')
    );
  } 
  return $fields;
}
add_filter(\'attachment_fields_to_edit\', \'add_attachment_fields\', 10, 2);
要保存值,请使用attachment_fields_to_save:

/**
 * Filters the attachment fields to be saved.
 * @param array $post       An array of post data.
 * @param array $attachment An array of attachment metadata.
 */
function update_attachment_fields($post, $attachment)
{
  if (isset($attachment[\'meta_credit\'])) {
    $credit = $attachment[\'meta_credit\'];
    $meta = wp_get_attachment_metadata($post[\'ID\']);
    if ($credit !== $meta[\'image_meta\'][\'credit\']) {
        $meta[\'image_meta\'][\'credit\'] = $credit;
        wp_update_attachment_metadata($post[\'ID\'], $meta);
    }
  }
  return $post;
}
add_filter(\'attachment_fields_to_save\', \'update_attachment_fields\', 10, 2);
如何处理alt属性?没什么好主意,虽然在数据库中保留垃圾不太好,但可以:

add_attachmentadd_post_meta 添加附件时,变体会触发一次:

add_action(\'add_attachment\', \'add_alt_to_attachment\');
function add_alt_to_attachment($post_ID)
{
    add_post_meta($post_ID, \'_wp_attachment_image_alt\', \'MY TEXT\');
}
任何字段更改时激发:

function update_alt_field($post, $attachment)
{
  if (empty($attachment[\'image_alt\'])) {
    $image_alt = wp_unslash(\'My Alt is good\');
    $image_alt = wp_strip_all_tags($image_alt, true);
    // Update_meta expects slashed.
    update_post_meta($attachment_id, \'_wp_attachment_image_alt\', wp_slash( $image_alt));
  }
  return $post;
}
add_filter(\'attachment_fields_to_save\', \'update_alt_field\', 10, 2);
和其他方式-在post中插入图像时添加alt文本:

/**
 * Filter the list of attachment image attributes.
 *
 * @param array            $attrs        Attributes for the image markup.
 * @param WP_Post        $attachment    Image attachment post.
 * @param string|array    $size        Requested size. Image size or array of width and height values
 *                                 (in that order). Default \'thumbnail\'.
 * @return array        $attrs        Attributes for the image markup.
 */
function my_images_attr($attrs, $attachment, $size)
{
    if (empty($attrs[\'alt\'])) {
        $attrs[\'alt\']) = \'MY TEXT\';
    }
    return $attrs;
}
add_filter(\'wp_get_attachment_image_attributes\', \'my_images_attr\', 10, 3);

SO网友:bubblez

我找到了一个解决方案,可以为所有新上载的附件添加默认标题:

function add_caption_to_attachment($data, $postarr){
  if(empty(trim($data[\'post_excerpt\']))){
    $data[\'post_excerpt\'] = \'«<a href="">TITEL</a>» - <a href="">NAME</a>, CC <a href="">XXX</a>\';
  }
  return $data;
}
add_filter(\'wp_insert_attachment_data\', \'add_caption_to_attachment\');
@vralle提出的解决方案对实际的附件元数据很有效,但标题却不行。所以我不得不找到另一种方法,add\\u附件挂钩不起作用。

相关推荐