wordpress收费下载资源主题
wordpress付费下载资源插件

WordPress hook钩子add_meta_boxes_link的用法详解

`add_meta_boxes_link`是一个WordPress钩子,用于在"链接"编辑页中添加自定义的元框(meta box)。

使用方法如下:

1. 在主题的`functions.php`文件中添加以下代码:

function custom_meta_box_link() {
add_meta_box( 'custom-meta-box', 'Custom Meta Box', 'custom_meta_box_link_callback', 'link', 'normal', 'high' );
}
add_action( 'add_meta_boxes_link', 'custom_meta_box_link' );

2. 在`custom_meta_box_link_callback`函数中定义元框的内容和字段:

function custom_meta_box_link_callback( $post ) {
// 添加CSRF字段以提高安全性
wp_nonce_field( basename( __FILE__ ), 'custom_meta_box_nonce' );

// 获取已保存的元框值
$custom_meta_value = get_post_meta( $post->ID, 'custom_meta_box_link', true );

// 显示元框的HTML内容
echo '';
echo '';
}

3. 在保存"链接"时,将元框的值保存到数据库中:

function save_custom_meta_box_link( $post_id ) {
// 验证CSRF字段
if ( !isset( $_POST['custom_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['custom_meta_box_nonce'], basename( __FILE__ ) ) ) {
return;
}

// 检查用户权限
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}

// 保存元框的值到数据库中
if ( isset( $_POST['custom_meta_box_input'] ) ) {
update_post_meta( $post_id, 'custom_meta_box_link', sanitize_text_field( $_POST['custom_meta_box_input'] ) );
}
}
add_action( 'save_post_link', 'save_custom_meta_box_link' );

以上代码实现了在"链接"编辑页中添加一个名为"Custom Meta Box"的元框,并将元框的值保存到数据库中。您可以根据自己的需求自定义元框的名称、显示内容和保存方式。

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请直接说明需求,勿问在否
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情