wordpress 字段_如何将其他字段添加到WordPress媒体上传器

wordpress 字段

While working on a project where we created a very cool gallery powered totally by WordPress attachments and a custom post type, we found a need to add additional fields to the WordPress media uploader. These additional fields allowed us to give each photographer credit by adding photographer name, and their URL on each image page. WordPress stores images as posts in the attachment post type, so adding meta data is just like adding custom fields. Because the WordPress attachments does not have a custom fields UI, we have to add a custom fields to the media uploader in order to collect the meta data. In this article, we will show you how to add additional fields to the WordPress Media Uploader.

在开展一个项目时,我们创建了一个非常酷的画廊,完全由WordPress附件和自定义帖子类型提供支持,我们发现有必要向WordPress媒体上传器添加其他字段。 这些额外的字段使我们可以通过在每个图像页面上添加摄影师名称及其URL来给每个摄影师以功劳。 WordPress将图像作为帖子存储在附件帖子类型中,因此添加元数据就像添加自定义字段。 由于WordPress附件没有自定义字段UI,因此我们必须向媒体上传器添加一个自定义字段,以收集元数据。 在本文中,我们将向您展示如何向WordPress Media Uploader添加其他字段。

We will be using the following filters to make the change: attachment_fields_to_edit and attachment_fields_to_save

我们将使用以下过滤器进行更改: attachment_fields_to_editattachment_fields_to_save

For a project like this, we highly recommend that you create a site-specific plugin and add the following code. However, you can still add the codes in your theme’s functions.php file to make it work.

对于这样的项目,我们强烈建议您创建一个特定站点的插件并添加以下代码。 但是,您仍然可以在主题的functions.php文件中添加代码以使其工作。


/**
 * Add Photographer Name and URL fields to media uploader
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */
 
function be_attachment_field_credit( $form_fields, $post ) {
	$form_fields['be-photographer-name'] = array(
		'label' => 'Photographer Name',
		'input' => 'text',
		'value' => get_post_meta( $post->ID, 'be_photographer_name', true ),
		'helps' => 'If provided, photo credit will be displayed',
	);

	$form_fields['be-photographer-url'] = array(
		'label' => 'Photographer URL',
		'input' => 'text',
		'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
		'helps' => 'Add Photographer URL',
	);

	return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );

/**
 * Save values of Photographer Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

function be_attachment_field_credit_save( $post, $attachment ) {
	if( isset( $attachment['be-photographer-name'] ) )
		update_post_meta( $post['ID'], 'be_photographer_name', $attachment['be-photographer-name'] );

	if( isset( $attachment['be-photographer-url'] ) )
update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) );

	return $post;
}

add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
?>

The code above will add two text fields to the Media Uploader called Photographer Name and Photographer URL. You can see that in the screenshot below:

上面的代码将向媒体上载器添加两个文本字段,分别称为“摄影师名称”和“摄影师URL”。 您可以在下面的屏幕截图中看到:

Additional Fields in the Media Uploader

Explanation of the code: In the first function, we are simply using an array to specify the field’s label, input type, value, and help text. The second function is checking to see if a value has been set for those fields. IF the value is set, then the post metadata is updated.

代码说明:在第一个函数中,我们仅使用数组来指定字段的标签,输入类型,值和帮助文本。 第二项功能是检查是否为这些字段设置了值。 如果设置了该值,则更新帖子元数据。

If you want to display the fields in your attachments template, then simply paste the following codes inside the loop:

如果要显示附件模板中的字段,则只需将以下代码粘贴到循环中:

echo get_post_meta($post->ID, 'be_photographer_url', true);

If you want to display the fields for your featured image in your archive template or any other template, then simply use:

如果要在存档模板或任何其他模板中显示特色图像的字段,则只需使用:

echo get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true);

We hope that you enjoyed this article. For those who don’t know how to create an attachment’s template, don’t worry. In the next article, we will cover how to create an attachment’s template in WordPress.

我们希望您喜欢这篇文章。 对于那些不知道如何创建附件模板的人,请不要担心。 在下一篇文章中,我们将介绍如何在WordPress中创建附件的模板。

Hat Tip to Bill Erickson for showing us how to do this.

Bill Erickson的帽子技巧,向我们展示了如何执行此操作。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-additional-fields-to-the-wordpress-media-uploader/

wordpress 字段

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值