get_media_item ( $attachment_id, $args = null )
get_media_item函数用于通过其ID检索一个媒体项目(例如,图片或视频): 这个函数可以用来在一个文章或页面上显示一个特定的媒体项目。
检索用于修改图片附件的HTML表格。
function get_media_item( $attachment_id, $args = null ) {
global $redir_tab;
$thumb_url = false;
$attachment_id = (int) $attachment_id;
if ( $attachment_id ) {
$thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true );
if ( $thumb_url ) {
$thumb_url = $thumb_url[0];
}
}
$post = get_post( $attachment_id );
$current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
$default_args = array(
'errors' => null,
'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
'delete' => true,
'toggle' => true,
'show_title' => true,
);
$parsed_args = wp_parse_args( $args, $default_args );
/**
* Filters the arguments used to retrieve an image for the edit image form.
*
* @since 3.1.0
*
* @see get_media_item
*
* @param array $parsed_args An array of arguments.
*/
$parsed_args = apply_filters( 'get_media_item_args', $parsed_args );
$toggle_on = __( 'Show' );
$toggle_off = __( 'Hide' );
$file = get_attached_file( $post->ID );
$filename = esc_html( wp_basename( $file ) );
$title = esc_attr( $post->post_title );
$post_mime_types = get_post_mime_types();
$keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
$type = reset( $keys );
$type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
$form_fields = get_attachment_fields_to_edit( $post, $parsed_args['errors'] );
if ( $parsed_args['toggle'] ) {
$class = empty( $parsed_args['errors'] ) ? 'startclosed' : 'startopen';
$toggle_links = "
<a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
<a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
} else {
$class = '';
$toggle_links = '';
}
$display_title = ( ! empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case.
$display_title = $parsed_args['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '…' ) . '</span></div>' : '';
$gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' === $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' === $redir_tab ) );
$order = '';
foreach ( $form_fields as $key => $val ) {
if ( 'menu_order' === $key ) {
if ( $gallery ) {
$order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' /></div>";
} else {
$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
}
unset( $form_fields['menu_order'] );
break;
}
}
$media_dims = '';
$meta = wp_get_attachment_metadata( $post->ID );
if ( isset( $meta['width'], $meta['height'] ) ) {
$media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']} × {$meta['height']}</span> ";
}
/**
* Filters the media metadata.
*
* @since 2.5.0
*
* @param string $media_dims The HTML markup containing the media dimensions.
* @param WP_Post $post The WP_Post attachment object.
*/
$media_dims = apply_filters( 'media_meta', $media_dims, $post );
$image_edit_button = '';
if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
$nonce = wp_create_nonce( "image_editor-$post->ID" );
$image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, "$nonce" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>";
}
$attachment_url = get_permalink( $attachment_id );
$item = "
$type_html
$toggle_links
$order
$display_title
<table class='slidetoggle describe $class'>
<thead class='media-item-info' id='media-head-$post->ID'>
<tr>
<td class='A1B1' id='thumbnail-head-$post->ID'>
<p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
<p>$image_edit_button</p>
</td>
<td>
<p><strong>" . __( 'File name:' ) . "</strong> $filename</p>
<p><strong>" . __( 'File type:' ) . "</strong> $post->post_mime_type</p>
<p><strong>" . __( 'Upload date:' ) . '</strong> ' . mysql2date( __( 'F j, Y' ), $post->post_date ) . '</p>';
if ( ! empty( $media_dims ) ) {
$item .= '<p><strong>' . __( 'Dimensions:' ) . "</strong> $media_dims</p>n";
}
$item .= "</td></tr>n";
$item .= "
</thead>
<tbody>
<tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>n
<tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>n
<tr><td colspan='2'><p class='media-types media-types-required-info'>" .
wp_required_field_message() .
"</p></td></tr>n";
$defaults = array(
'input' => 'text',
'required' => false,
'value' => '',
'extra_rows' => array(),
);
if ( $parsed_args['send'] ) {
$parsed_args['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false );
}
$delete = empty( $parsed_args['delete'] ) ? '' : $parsed_args['delete'];
if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
if ( ! EMPTY_TRASH_DAYS ) {
$delete = "<a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>';
} elseif ( ! MEDIA_TRASH ) {
$delete = "<a href='#' class='del-link' onclick="document.getElementById('del_attachment_$attachment_id').style.display='block';return false;">" . __( 'Delete' ) . "</a>
<div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" .
/* translators: %s: File name. */
'<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p>
<a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
<a href='#' class='button' onclick="this.parentNode.style.display='none';return false;">" . __( 'Cancel' ) . '</a>
</div>';
} else {
$delete = "<a href='" . wp_nonce_url( "post.php?action=trash&post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
<a href='" . wp_nonce_url( "post.php?action=untrash&post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . '</a>';
}
} else {
$delete = '';
}
$thumbnail = '';
$calling_post_id = 0;
if ( isset( $_GET['post_id'] ) ) {
$calling_post_id = absint( $_GET['post_id'] );
} elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set.
$calling_post_id = $post->post_parent;
}
if ( 'image' === $type && $calling_post_id
&& current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
&& post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' )
&& get_post_thumbnail_id( $calling_post_id ) != $attachment_id
) {
$calling_post = get_post( $calling_post_id );
$calling_post_type_object = get_post_type_object( $calling_post->post_type );
$ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
$thumbnail = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail("$attachment_id", "$ajax_nonce");return false;'>" . esc_html( $calling_post_type_object->labels->use_featured_image ) . '</a>';
}
if ( ( $parsed_args['send'] || $thumbnail || $delete ) && ! isset( $form_fields['buttons'] ) ) {
$form_fields['buttons'] = array( 'tr' => "tt<tr class='submit'><td></td><td class='savesend'>" . $parsed_args['send'] . " $thumbnail $delete</td></tr>n" );
}
$hidden_fields = array();
foreach ( $form_fields as $id => $field ) {
if ( '_' === $id[0] ) {
continue;
}
if ( ! empty( $field['tr'] ) ) {
$item .= $field['tr'];
continue;
}
$field = array_merge( $defaults, $field );
$name = "attachments[$attachment_id][$id]";
if ( 'hidden' === $field['input'] ) {
$hidden_fields[ $name ] = $field['value'];
continue;
}
$required = $field['required'] ? ' ' . wp_required_field_indicator() : '';
$required_attr = $field['required'] ? ' required' : '';
$class = $id;
$class .= $field['required'] ? ' form-required' : '';
$item .= "tt<tr class='$class'>nttt<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>nttt<td class='field'>";
if ( ! empty( $field[ $field['input'] ] ) ) {
$item .= $field[ $field['input'] ];
} elseif ( 'textarea' === $field['input'] ) {
if ( 'post_content' === $id && user_can_richedit() ) {
// Sanitize_post() skips the post_content when user_can_richedit.
$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
}
// Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
$item .= "<textarea id='$name' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
} else {
$item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr} />";
}
if ( ! empty( $field['helps'] ) ) {
$item .= "<p class='help'>" . implode( "</p>n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
}
$item .= "</td>ntt</tr>n";
$extra_rows = array();
if ( ! empty( $field['errors'] ) ) {
foreach ( array_unique( (array) $field['errors'] ) as $error ) {
$extra_rows['error'][] = $error;
}
}
if ( ! empty( $field['extra_rows'] ) ) {
foreach ( $field['extra_rows'] as $class => $rows ) {
foreach ( (array) $rows as $html ) {
$extra_rows[ $class ][] = $html;
}
}
}
foreach ( $extra_rows as $class => $rows ) {
foreach ( $rows as $html ) {
$item .= "tt<tr><td></td><td class='$class'>$html</td></tr>n";
}
}
}
if ( ! empty( $form_fields['_final'] ) ) {
$item .= "tt<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>n";
}
$item .= "t</tbody>n";
$item .= "t</table>n";
foreach ( $hidden_fields as $name => $value ) {
$item .= "t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />n";
}
if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
$parent = (int) $_REQUEST['post_id'];
$parent_name = "attachments[$attachment_id][post_parent]";
$item .= "t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />n";
}
return $item;
}
要使用` get_users `函数获取所有用户列表,可以按照以下步骤进行:
1. 使用` get_users `函数调用获取用户列表:
$users = get_users();
2. 您可以按需使用参数来过滤结果。例如,您可以通过角色、用户ID、用户登录名等过滤用户列表。以下是一个根据用户角色为过滤条件的示例:
$users = get_users( array(
'role' => 'subscriber' // 将角色名称替换为您要过滤的角色
) );
在上述示例中,将` role `参数设置为所需的角色名称来过滤用户列表。
3. 您可以使用循环遍历获取的用户列表,并访问每个用户的属性。例如,以下示例将显示每个用户的用户名和电子邮件地址:
foreach( $users as $user ) {
echo '用户名:' . $user->user_login . ', 电子邮件:' . $user->user_email . ;
}
在上述示例中,通过` $user->user_login `和` $user->user_email `访问每个用户的用户名和电子邮件地址。
请注意,` get_users `函数默认返回所有用户,并可以根据需要使用更多参数进行过滤。您可以参阅WordPress官方文档中的` get_users `函数文档,了解更多可用参数和用法示例。
总结起来,使用` get_users `函数获取所有用户列表的步骤是:
get_users `函数获取用户列表。在WordPress中,可以使用WP_PLUGIN_DIR和WP_PLUGIN_URL常量来定义插件的目录路径和URL。
1. `WP_PLUGIN_DIR`:这是一个常量,用于定义插件的目录路径(文件系统路径)。您可以使用以下代码在插件文件中访问该常量:
$plugin_dir = WP_PLUGIN_DIR . '/your-plugin-folder/';
在上述代码中,将"your-plugin-folder"替换为您插件的实际文件夹名称。使用该常量,您可以获取插件文件的完整路径。
2. `WP_PLUGIN_URL`:这是一个常量,用于定义插件的URL(用于在网页上访问插件文件)。以下是一个使用该常量的示例:
$plugin_url = WP_PLUGIN_URL . '/your-plugin-folder/';
同样,请将"your-plugin-folder"替换为您插件的实际文件夹名称。使用该常量,您可以获取插件在网页上的完整URL。
请注意,`WP_PLUGIN_DIR`和`WP_PLUGIN_URL`常量在WordPress版本2.6之后引入。从WordPress 5.5版本开始,这两个常量被标记为过时(deprecated),因为WordPress更倾向于使用新的插件文件结构。如果您正在开发新插件,建议使用新的插件文件结构和相关函数。
在新的插件文件结构中,可以使用以下函数来获取插件的目录路径和URL:
- `plugin_dir_path()`:获取插件目录路径。
- `plugin_dir_url()`:获取插件URL。
这些函数会自动将插件的版本、多站点和SSL等考虑因素纳入计算。
总结起来,使用`WP_PLUGIN_DIR`和`WP_PLUGIN_URL`常量定义插件的目录和URL的方法是:
$plugin_dir = WP_PLUGIN_DIR . '/your-plugin-folder/'; $plugin_url = WP_PLUGIN_URL . '/your-plugin-folder/';
但请注意,这两个常量已被标记为过时,建议使用新的插件文件结构和相关函数来获取插件的路径和URL。
使用PHP在WordPress中添加自定义功能可以通过以下方式实现:
下面是一个实操示例。
要在WordPress中添加自定义功能,可以按照以下步骤使用PHP编写并添加自定义功能:
// 添加自定义功能示例
// 1. 创建自定义短代码
function custom_shortcode() {
return '这是我的自定义短代码内容';
}
add_shortcode('custom', 'custom_shortcode');
// 2. 自定义小工具
function custom_widget() {
echo '这是我的自定义小工具内容';
}
register_widget('custom_widget');
// 3. 自定义菜单
function custom_menu() {
register_nav_menu('custom-menu', '自定义菜单');
}
add_action('after_setup_theme', 'custom_menu');
// 4. 自定义页面模板
function custom_page_template() {
/* Template Name: 自定义模板 */
// 自定义模板的内容和样式
}
请注意,修改主题文件可以在主题更新时丢失,因此建议在进行任何更改之前备份functions.php文件。此外,为避免不必要的错误和冲突,建议在添加自定义功能前先了解WordPress开发文档和最佳实践,以确保正确、安全地实现所需的自定义功能。
使用 do_action 函数可以触发一个钩子函数。do_action 函数的参数与要触发的钩子函数的参数相同。
例如,触发save_post钩子函数的代码如下:
do_action( 'save_post', $post_ID, $post );
这里,$post_ID 和 $post 是传递给钩子函数的参数。
使用 wp_get_current_user 获取当前登录用户的信息:
$current_user = wp_get_current_user(); // 获取当前用户的ID $user_id = $current_user->ID; // 获取当前用户的用户名 $user_login = $current_user->user_login; // 获取当前用户的邮箱 $user_email = $current_user->user_email; // 获取当前用户的显示名称 $display_name = $current_user->display_name;
(工作日 10:00 - 18:30 为您服务)