Function Reference/wp generate attachment metadata

来自WordPress中文文档
跳转至: 导航搜索

Description

This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the Settings_Media_Screen.

Usage

%%% <?php wp_generate_attachment_metadata( $attachment_id, $file ); ?> %%%

Parameters

tt$attachment_id/tt
(int) (required) Attachment Id to process.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$file/tt
(string) (required) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory. See wp_upload_dir()
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return Values

(array) 
attachment metadata in the format required by wp_update_attachment_metadata()

The elements returned in the array are:

["width"] 
(string) Horizontal size of image attachment, in pixels.
["height"] 
(string) Vertical size of image attachment, in pixels.
["file"] 
(string) Path to image attachment, relative to the currently configured uploads directory.
["hwstring_small"] 
(string) Height/width string for HTML img tag used to display the Small size of this image.
For example: height='96' width='126'
["sizes"]["thumbnail"]["file"] 
(string) File name of Thumbnail-sized copy of image attachment.
["sizes"]["thumbnail"]["width"] 
(string) Horizontal size of Thumbnail-sized copy of image attachment, in pixels.
["sizes"]["thumbnail"]["height"] 
(string) Vertical size of Thumbnail-sized copy of image attachment, in pixels.
["sizes"]["medium"] 
(array) Same three elements as ["sizes"]["thumbnail"] but for Medium-sized copy of image attachment.
["sizes"]["large"] 
(array) Same three elements as ["sizes"]["thumbnail"] but for Large-sized copy of image attachment.
["sizes"]["post-thumbnail"] 
(array) Same three elements as ["sizes"]["thumbnail"] but for Post Thumbnail-sized copy of image attachment.
["sizes"]["large-feature"] 
(array) Same three elements as ["sizes"]["thumbnail"] but for Large Feature-sized copy of image attachment.
["sizes"]["small-feature"] 
(array) Same three elements as ["sizes"]["thumbnail"] but for Small Feature-sized copy of image attachment.
["image_meta"] 
(array) Image attachment Metadata returned by wp_read_image_metadata()

Example

To generate attachment metadata for attachment with parent post ID 37:

<?php
  $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  wp_update_attachment_metadata( $attach_id,  $attach_data );
?>

Notes

This function can be used to regenerate thumbnail and intermediate sizes of the image after changes have been made on the Settings_Media_Screen but it does not check or delete intermediate sizes it previously created for the same image.

Thumbnail and intermediate sizes of the image, and ["sizes"] elements in the array returned by this function, are only generated when the intermediate size is smaller than the size of the image.

The function should be used in conjunction with wp_update_attachment_metadata().

If this function is undefined in the environment where it is to be used, such as within a Shortcode, use the include function:

<?php include( ABSPATH . 'wp-admin/includes/image.php' ); ?>

Source File

wp_generate_attachment_metadata() is located in onlyincludecodewp-admin/includes/image.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

wp_update_attachment_metadata(), wp_get_attachment_metadata(), wp_read_image_metadata(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), wp_attachment_is_image(), wp_insert_attachment(), wp_delete_attachment(), wp_get_attachment_link(), wp_get_attachment_url(), is_attachment(), wp_insert_post()