edit_form_image_editor()
云策文档标注
概述
edit_form_image_editor() 函数用于在文章编辑器中显示图像和编辑器界面。它根据查询参数控制图像编辑器的打开状态,并生成相关的HTML输出。
关键要点
- 函数接受一个必需的 $post 参数,类型为 WP_Post 对象。
- 通过检查 $_GET['image-editor'] 参数来决定是否加载图像编辑功能。
- 使用 wp_get_attachment_image_src() 获取缩略图URL,get_post_meta() 获取替代文本。
- 根据 wp_image_editor_supports() 检查结果,有条件地生成“编辑图像”按钮。
- 输出包含图像显示和编辑器容器的HTML结构。
代码示例
function edit_form_image_editor( $post ) {
$open = isset( $_GET['image-editor'] );
if ( $open ) {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
}
$thumb_url = false;
$attachment_id = (int) $post->ID;
if ( $attachment_id ) {
$thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
}
$alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
$att_url = wp_get_attachment_url( $post->ID );
?>
ID ) ) :
$image_edit_button = '';
if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
$nonce = wp_create_nonce( "image_editor-$post->ID" );
$image_edit_button = "ID' onclick='imageEdit.open( $post->ID, "$nonce" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> ";
}
$open_style = '';
$not_open_style = '';
if ( $open ) {
$open_style = ' style="display:none"';
} else {
$not_open_style = ' style="display:none"';
}
?>
">
class="wp_attachment_image wp-clearfix" id="media-head-">
">" style="max-width:100%" alt="" />
class="image-editor" id="image-editor-">
原文内容
Displays the image and editor in the post editor
Parameters
$postWP_Postrequired-
A post object.
Source
function edit_form_image_editor( $post ) {
$open = isset( $_GET['image-editor'] );
if ( $open ) {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
}
$thumb_url = false;
$attachment_id = (int) $post->ID;
if ( $attachment_id ) {
$thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
}
$alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
$att_url = wp_get_attachment_url( $post->ID );
?>
<div class="wp_attachment_image wp-clearfix" id="media-head-">
<p id="thumbnail-head-"><img class="thumbnail" src="" style="max-width:100%" alt="" />
<div class="image-editor" id="image-editor-">