duanan1228 2013-08-29 18:26
浏览 29
已采纳

Wordpress元数据如何设法找到保存功能

I am making a wordpress metabox and i was wondering how the html part of the metabox manages to find the save function.Here is the entire code i am using which works

<?php
function true_add_a_metabox() {
    add_meta_box(
        'true_metabox', // metabox ID, it also will be it id HTML attribute
        'The Detailed Custom Meta Box', // title
        'true_display_metabox', // this is a callback functions, which will be print HTML of our metabox
        'post', // post type
        'normal', // position of the screen where metabox shoul be displayed (normal, side, advanced)
        'default' // priority over another metaboxes on this page (default, low, high, core)
    );
}

add_action( 'admin_menu', 'true_add_a_metabox' );

function true_display_metabox($post) {
    /*
     * needs for security checks
     */
    wp_nonce_field( basename( __FILE__ ), 'true_metabox_nonce' );
    /*
     * lets add a simple textarea field
     */
    $html .= '<p><label>SEO title <input type="text" name="seotitle" value="' . get_post_meta($post->ID, 'true_title',true) . '" /></label></p>';
    /*
     * add a checkbox
     */
    $html .= '<p><label><input type="checkbox" name="noindex"';
    $html .= (get_post_meta($post->ID, 'true_noindex',true) == 'on') ? ' checked="checked"' : '';
    $html .= ' /> Turn of page visibility for search engines</label></p>';
    /*
     * print all of this
     */
    echo $html;
}

function true_save_post_meta( $post_id, $post ) {
    /* 
     * Security checks
     */
    if ( !isset( $_POST['true_metabox_nonce'] ) || !wp_verify_nonce( $_POST['true_metabox_nonce'], basename( __FILE__ ) ) )
        return $post_id;
    /* 
     * Check current user permissions
     */
    $post_type = get_post_type_object( $post->post_type );
    if ( !current_user_can( $post_type->can->edit_post, $post_id ) )
        return $post_id;
    /*
     * Check if the autosave
     */
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    if ($post->post_type == 'post') { // define your own post type here
        update_post_meta($post_id, 'true_title', esc_attr($_POST['seotitle']));
        update_post_meta($post_id, 'true_noindex', $_POST['noindex']);
    }
    return $post_id;
}

add_action( 'save_post', 'true_save_post_meta', 10, 2 );

?>

In the function that produces the html true_display_metabox there is no mention of true_save_post_meta which saves the options.Can anyone explain how this metabox manages to the save the data?.

  • 写回答

1条回答 默认 最新

  • download20151010 2013-08-29 18:45
    关注

    You are calling true_save_post_meta on the save_post action (in the last line of your code). This means that every time the post is saved the true_save_post_meta function will run. The data from your meta box will be included in the $_POST object, which true_save_post_meta then uses to save those values in the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装