WordPress: Function Reference/add meta box:修订间差异
(新页面: == Description == The <tt>add_meta_box()</tt> function was introduced in WordPress 2.5. It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing ...) |
无编辑摘要 |
||
第1行: | 第1行: | ||
== Description == | == Description == | ||
==描述== | |||
The <tt>add_meta_box()</tt> function was introduced in WordPress 2.5. It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing pages. | The <tt>add_meta_box()</tt> function was introduced in WordPress 2.5. It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing pages. | ||
<tt>add_meta_box()</tt>函数是WordPress2.5版本引进的,能够使得插件开发人员,给编写文章,编写网页,和编写链接,之列的编辑网页,添加一些部分。 | |||
== Usage == | |||
==用法== | |||
%%% <?php add_meta_box('id', 'title', 'callback', 'page', 'context'); ?> %%% | |||
%%% <?php add_meta_box('id', 'title', 'callback', 'page', 'context'); ?> %%% | %%% <?php add_meta_box('id', 'title', 'callback', 'page', 'context'); ?> %%% | ||
== Example == | == Example == | ||
==例子== | |||
Here is an example that adds a custom section to the post and page editing screens. It will will work with WordPress 2.5 and also with earlier versions of WordPress (when the <tt>add_meta_box</tt> function did not exist). | Here is an example that adds a custom section to the post and page editing screens. It will will work with WordPress 2.5 and also with earlier versions of WordPress (when the <tt>add_meta_box</tt> function did not exist). | ||
下面有一个例子,给文章和网页编辑界面添加了一个自定义的部分,在WordPress2.5版本以及其它更早发行的WordPress版本中能够运行(<tt>add_meta_box</tt>函数不存在的时候)。 | |||
<pre> | <pre> | ||
<?php | <?php | ||
/* Use the admin_menu action to define the custom boxes */ | /* Use the admin_menu action to define the custom boxes */ | ||
add_action('admin_menu', 'myplugin_add_custom_box'); | |||
<pre> | |||
<?php | |||
/*使用 admin_menu action 定义自定义框*/ | |||
add_action('admin_menu', 'myplugin_add_custom_box'); | add_action('admin_menu', 'myplugin_add_custom_box'); | ||
/* Use the save_post action to do something with the data entered */ | /* Use the save_post action to do something with the data entered */ | ||
add_action('save_post', 'myplugin_save_postdata'); | |||
/* 使用 save_post action 处理输入的数据*/ | |||
add_action('save_post', 'myplugin_save_postdata'); | add_action('save_post', 'myplugin_save_postdata'); | ||
/* Adds a custom section to the "advanced" Post and Page edit screens */ | /* Adds a custom section to the "advanced" Post and Page edit screens */ | ||
function myplugin_add_custom_box() { | |||
/* 向 "advanced"文章和网页编辑界面添加一个自定义的部分*/ | |||
function myplugin_add_custom_box() { | function myplugin_add_custom_box() { | ||
第91行: | 第115行: | ||
} | } | ||
</pre> | </pre> | ||
if( function_exists( 'add_meta_box' )) { | |||
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ), | |||
'myplugin_inner_custom_box', 'post', 'advanced' ); | |||
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ), | |||
'myplugin_inner_custom_box', 'page', 'advanced' ); | |||
} else { | |||
add_action('dbx_post_advanced', 'myplugin_old_custom_box' ); | |||
add_action('dbx_page_advanced', 'myplugin_old_custom_box' ); | |||
} | |||
} | |||
/*为自定义文章/网页部分输出inner fields */ | |||
function myplugin_inner_custom_box() { | |||
// 使用现时确认 | |||
echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . | |||
wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; | |||
// 数据登记项的真正的fields | |||
echo '<label for="myplugin_new_field">' . __("Description for this field", 'myplugin_textdomain' ) . '</label> '; | |||
echo '<input type="text" name="myplugin_new_field" value="whatever" size="25" />'; | |||
} | |||
/*为WordPress 2.5 版本之前的文章/网页输出编辑形式*/ | |||
function myplugin_old_custom_box() { | |||
echo '<div class="dbx-b-ox-wrapper">' . "\n"; | |||
echo '<fieldset id="myplugin_fieldsetid" class="dbx-box">' . "\n"; | |||
echo '<div class="dbx-h-andle-wrapper"><h3 class="dbx-handle">' . | |||
__( 'My Post Section Title', 'myplugin_textdomain' ) . "</h3></div>"; | |||
echo '<div class="dbx-c-ontent-wrapper"><div class="dbx-content">'; | |||
// 输出编辑形式 | |||
myplugin_inner_custom_box(); | |||
// end wrapper | |||
echo "</div></div></fieldset></div>\n"; | |||
} | |||
/* 文章保存后,保存我们的自定义数据、 */ | |||
function myplugin_save_postdata( $post_id ) { | |||
// 使用适当的权限确认界面内容 | |||
// 因为 save_post 可能在其它时候触发 | |||
if ( !wp_verify_nonce( $_POST[myplugin_noncename], plugin_basename(__FILE__) )) { | |||
return $post_id; | |||
} | |||
if ( 'page' == $_POST['post_type'] ) { | |||
if ( !current_user_can( 'edit_page', $post_id )) | |||
return $post_id; | |||
} else { | |||
if ( !current_user_can( 'edit_post', $post_id )) | |||
return $post_id; | |||
} | |||
// 我们已经得到了鉴别;我们需要找到并且保存数据 | |||
$mydata = $_POST['myplugin_new_field']; | |||
// TODO:用 $mydata执行一些步骤 | |||
return $mydata; | |||
} | |||
</pre> | |||
== Parameters == | == Parameters == | ||
==参数== | |||
;$id : (''string'') HTML 'id' attribute of the edit screen section | ;$id : (''string'') HTML 'id' attribute of the edit screen section | ||
;$id : (''string'')编辑界面部分的HTML 'id'属性 | |||
;title : (''string'') Title of the edit screen section, visible to user | ;title : (''string'') Title of the edit screen section, visible to user | ||
;title : (''string'')编辑界面部分的标题,用户可以看到 | |||
;callback : (''string'') Function that prints out the HTML for the edit screen section. | ;callback : (''string'') Function that prints out the HTML for the edit screen section. | ||
;callback : (''string'')为编辑界面部分输出HTML的函数 | |||
;page : (''string'') The type of Write screen on which to show the edit screen section (<tt>'post'</tt>, <tt>'page'</tt>, or <tt>'link'</tt>) | ;page : (''string'') The type of Write screen on which to show the edit screen section (<tt>'post'</tt>, <tt>'page'</tt>, or <tt>'link'</tt>) | ||
;page : (''string'')编写界面的类型,现时了编辑界面部分(<tt>'post'</tt>, <tt>'page'</tt>, or <tt>'link'</tt>) | |||
;context : (''string'') The part of the page where the edit screen section should be shown (<tt>'normal'</tt> or <tt>'advanced'</tt>) | ;context : (''string'') The part of the page where the edit screen section should be shown (<tt>'normal'</tt> or <tt>'advanced'</tt>) | ||
;context : (''string'')显示编辑界面的网页的那个部分(<tt>'normal'</tt> 或者<tt>'advanced'</tt>) | |||
== Further Reading == | == Further Reading == | ||
== 深入阅读 == | |||
* [[WordPress:Migrating Plugins and Themes]] | * [[WordPress:Migrating Plugins and Themes]] | ||
* [[WordPress:Function Reference]] | * [[WordPress:Function Reference]] | ||
* [[WordPress:Migrating Plugins and Themes|转移插件和主题]] | |||
* [[WordPress:Function Reference|函数参考]] |
2008年7月19日 (六) 13:57的版本
Description
描述
The add_meta_box() function was introduced in WordPress 2.5. It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing pages. add_meta_box()函数是WordPress2.5版本引进的,能够使得插件开发人员,给编写文章,编写网页,和编写链接,之列的编辑网页,添加一些部分。
Usage
用法
%%% <?php add_meta_box('id', 'title', 'callback', 'page', 'context'); ?> %%%
%%% <?php add_meta_box('id', 'title', 'callback', 'page', 'context'); ?> %%%
Example
例子
Here is an example that adds a custom section to the post and page editing screens. It will will work with WordPress 2.5 and also with earlier versions of WordPress (when the add_meta_box function did not exist).
下面有一个例子,给文章和网页编辑界面添加了一个自定义的部分,在WordPress2.5版本以及其它更早发行的WordPress版本中能够运行(add_meta_box函数不存在的时候)。
<?php /* Use the admin_menu action to define the custom boxes */ add_action('admin_menu', 'myplugin_add_custom_box'); <pre> <?php /*使用 admin_menu action 定义自定义框*/ add_action('admin_menu', 'myplugin_add_custom_box'); /* Use the save_post action to do something with the data entered */ add_action('save_post', 'myplugin_save_postdata'); /* 使用 save_post action 处理输入的数据*/ add_action('save_post', 'myplugin_save_postdata'); /* Adds a custom section to the "advanced" Post and Page edit screens */ function myplugin_add_custom_box() { /* 向 "advanced"文章和网页编辑界面添加一个自定义的部分*/ function myplugin_add_custom_box() { if( function_exists( 'add_meta_box' )) { add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'post', 'advanced' ); add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'page', 'advanced' ); } else { add_action('dbx_post_advanced', 'myplugin_old_custom_box' ); add_action('dbx_page_advanced', 'myplugin_old_custom_box' ); } } /* Prints the inner fields for the custom post/page section */ function myplugin_inner_custom_box() { // Use nonce for verification echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; // The actual fields for data entry echo '<label for="myplugin_new_field">' . __("Description for this field", 'myplugin_textdomain' ) . '</label> '; echo '<input type="text" name="myplugin_new_field" value="whatever" size="25" />'; } /* Prints the edit form for pre-WordPress 2.5 post/page */ function myplugin_old_custom_box() { echo '<div class="dbx-b-ox-wrapper">' . "\n"; echo '<fieldset id="myplugin_fieldsetid" class="dbx-box">' . "\n"; echo '<div class="dbx-h-andle-wrapper"><h3 class="dbx-handle">' . __( 'My Post Section Title', 'myplugin_textdomain' ) . "</h3></div>"; echo '<div class="dbx-c-ontent-wrapper"><div class="dbx-content">'; // output editing form myplugin_inner_custom_box(); // end wrapper echo "</div></div></fieldset></div>\n"; } /* When the post is saved, saves our custom data */ function myplugin_save_postdata( $post_id ) { // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST[myplugin_noncename], plugin_basename(__FILE__) )) { return $post_id; } if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) return $post_id; } else { if ( !current_user_can( 'edit_post', $post_id )) return $post_id; } // OK, we're authenticated: we need to find and save the data $mydata = $_POST['myplugin_new_field']; // TODO: Do something with $mydata return $mydata; }
if( function_exists( 'add_meta_box' )) {
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'post', 'advanced' ); add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'page', 'advanced' ); } else { add_action('dbx_post_advanced', 'myplugin_old_custom_box' ); add_action('dbx_page_advanced', 'myplugin_old_custom_box' ); }
}
/*为自定义文章/网页部分输出inner fields */ function myplugin_inner_custom_box() {
// 使用现时确认
echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// 数据登记项的真正的fields echo ' '; echo '<input type="text" name="myplugin_new_field" value="whatever" size="25" />';
}
/*为WordPress 2.5 版本之前的文章/网页输出编辑形式*/ function myplugin_old_custom_box() {
echo '
echo '<fieldset id="myplugin_fieldsetid" class="dbx-box">' . "\n";echo '
' . __( 'My Post Section Title', 'myplugin_textdomain' ) . "
// 输出编辑形式
myplugin_inner_custom_box();
// end wrapperecho "
\n";
}
/* 文章保存后,保存我们的自定义数据、 */ function myplugin_save_postdata( $post_id ) {
// 使用适当的权限确认界面内容 // 因为 save_post 可能在其它时候触发
if ( !wp_verify_nonce( $_POST[myplugin_noncename], plugin_basename(__FILE__) )) { return $post_id; }
if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) return $post_id; } else { if ( !current_user_can( 'edit_post', $post_id )) return $post_id; }
// 我们已经得到了鉴别;我们需要找到并且保存数据 $mydata = $_POST['myplugin_new_field'];
// TODO:用 $mydata执行一些步骤
return $mydata;
}
Parameters
参数
- $id
- (string) HTML 'id' attribute of the edit screen section
- $id
- (string)编辑界面部分的HTML 'id'属性
- title
- (string) Title of the edit screen section, visible to user
- title
- (string)编辑界面部分的标题,用户可以看到
- callback
- (string) Function that prints out the HTML for the edit screen section.
- callback
- (string)为编辑界面部分输出HTML的函数
- page
- (string) The type of Write screen on which to show the edit screen section ('post', 'page', or 'link')
- page
- (string)编写界面的类型,现时了编辑界面部分('post', 'page', or 'link')
- context
- (string) The part of the page where the edit screen section should be shown ('normal' or 'advanced')
- context
- (string)显示编辑界面的网页的那个部分('normal' 或者'advanced')