返回介绍

meta_form()

发布于 2017-09-11 01:55:57 字数 5287 浏览 913 评论 0 收藏 0

meta_form( WP_Post $post = null )

Prints the form in the Custom Fields meta box.


description


参数

$post

(WP_Post) (Optional) The post being edited.

Default value: null


源代码

File: wp-admin/includes/template.php

function meta_form( $post = null ) {
	global $wpdb;
	$post = get_post( $post );

	/**
	 * Filters values for the meta key dropdown in the Custom Fields meta box.
	 *
	 * Returning a non-null value will effectively short-circuit and avoid a
	 * potentially expensive query against postmeta.
	 *
	 * @since 4.4.0
	 *
	 * @param array|null $keys Pre-defined meta keys to be used in place of a postmeta query. Default null.
	 * @param WP_Post    $post The current post object.
	 */
	$keys = apply_filters( 'postmeta_form_keys', null, $post );

	if ( null === $keys ) {
		/**
		 * Filters the number of custom fields to retrieve for the drop-down
		 * in the Custom Fields meta box.
		 *
		 * @since 2.1.0
		 *
		 * @param int $limit Number of custom fields to retrieve. Default 30.
		 */
		$limit = apply_filters( 'postmeta_form_limit', 30 );
		$sql = "SELECT DISTINCT meta_key
			FROM $wpdb->postmeta
			WHERE meta_key NOT BETWEEN '_' AND '_z'
			HAVING meta_key NOT LIKE %s
			ORDER BY meta_key
			LIMIT %d";
		$keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) );
	}

	if ( $keys ) {
		natcasesort( $keys );
		$meta_key_input_id = 'metakeyselect';
	} else {
		$meta_key_input_id = 'metakeyinput';
	}
?>
<p><strong><?php _e( 'Add New Custom Field:' ) ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php echo $meta_key_input_id; ?>"><?php _ex( 'Name', 'meta name' ) ?></label></th>
<th><label for="metavalue"><?php _e( 'Value' ) ?></label></th>
</tr>
</thead>

<tbody>
<tr>
<td id="newmetaleft" class="left">
<?php if ( $keys ) { ?>
<select id="metakeyselect" name="metakeyselect">
<option value="#NONE#"><?php _e( '&mdash; Select &mdash;' ); ?></option>
<?php

	foreach ( $keys as $key ) {
		if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'add_post_meta', $post->ID, $key ) )
			continue;
		echo "\n<option value='" . esc_attr($key) . "'>" . esc_html($key) . "</option>";
	}
?>
</select>
<input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput,

更新日志

Versiondescription
1.2.0Introduced.

相关函数

Uses

  • wp-admin/includes/template.php: postmeta_form_keys
  • wp-includes/wp-db.php: wpdb::esc_like()
  • wp-admin/includes/template.php: submit_button()
  • wp-admin/includes/template.php: postmeta_form_limit
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/l10n.php: _e()
  • wp-includes/l10n.php: _ex()
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/functions.php: wp_nonce_field()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: get_post()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/meta.php: is_protected_meta()
  • Show 11 more uses Hide more uses

Used By

  • wp-admin/includes/meta-boxes.php: post_custom_meta_box()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by pixeline

    Note that if your setup uses a lot of metafields, the custom field metabox provided by default by WordPress triggers a very slow mysql query that can slow down the Edit Post page load time (info: https://core.trac.wordpress.org/ticket/33885). Here is an easy fix (add it to your Theme’s functions.php file):

    
    function admin_speedup_remove_post_meta_box() {
    global $post_type;
    
    if ( is_admin() && post_type_supports( $post_type, 'custom-fields' ) ) {
    remove_meta_box( 'postcustom', 'post', 'normal' );
    }
    }
    add_action( 'add_meta_boxes', 'admin_speedup_remove_post_meta_box' );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文