WordPress的add_meta_box不会做任何事情

问题描述:

我似乎无法让WordPress的add_meta_box工作。 它正在编辑页面中添加框,但不会反映在任何地方的输入框中。WordPress的add_meta_box不会做任何事情

我该如何得到这个工作?有什么我做错了吗? I followed this tutorial

// Add support for header images 
if(function_exists('add_theme_support')){ 
    add_theme_support('post-thumbnails'); // Adds this for both pages and posts, For posts/page only, see https://codex.wordpress.org/Function_Reference/add_theme_support 
} 

// Callback for adding metaboxes 
function actionblock_add_meta_box(){ 
    $screens = array('post', 'page'); 
    foreach ($screens as $screen) { 
    add_meta_box('action_block', 'Action Block', 'actionblock_meta_box_callback', $screen, 'normal'); // Add the action block to both post and page screens 
    } 
} 

// Add meta boxes for action section on each post/page 
add_action('add_meta_boxes', 'actionblock_add_meta_box'); 

function actionblock_meta_box_callback($post){ 
    // Security check 
    wp_nonce_field('actionblock_save_meta_box_data', 'actionblock_meta_box_nonce'); 

    // Get values of the action block fields 
    $action_prompt_text = get_post_meta($post->ID, 'actionblock_meta_prompt_text', true); 
    $action_button_text = get_post_meta($post->ID, 'actionblock_meta_button_text', true); 
    $action_button_link = get_post_meta($post->ID, 'actionblock_meta_button_link', true); 


    // Display and populate fields from the database if it exists 
    ?> 
    <p> 
    <label for="action_prompt_text">Action Prompt</label><br> 
    <input class="widefat" type="text" name="action_prompt_text" id="action_prompt_text" placeholder="This should be short, to the point and less salesy." value="<?php echo esc_attr($action_prompt_text); ?>"> 
    </p> 
    <p> 
    <label for="action_button_text">Action Button Text</label><br> 
    <input class="widefat" type="text" name="action_button_text" id="action_button_text" placeholder="This should prompt the visitor to take an action." value="<?php echo esc_attr($action_button_text); ?>"> 
    </p> 
    <p> 
    <label for="action_button_link">Action Button Link</label><br> 
    <input class="widefat" type="url" name="action_button_link" id="action_button_link" placeholder="Copy and paste the link from the intended page." value="<?php echo esc_attr($action_button_link); ?>"> 
    </p> 
    <?php 

    // Callback for saving metaboxes 
    function actionblock_save_meta_box_data($post_id) { 

    // Security checks 
    if(!isset($_POST['actionblock_meta_box_nonce'])) return; 
    if(!wp_verify_nonce($_POST['actionblock_meta_box_nonce'], 'actionblock_save_meta_box_data')) return; 
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

    if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) { 
     if (! current_user_can('edit_page', $post_id)) { 
     return; 
     } 
    } else { 
     if (! current_user_can('edit_post', $post_id)) { 
     return; 
     } 
    } 

    // Do the save/update 
    if(isset($_POST['action_prompt_text'])){ 
     update_post_meta($post_id, 'actionblock_meta_prompt_text', sanitize_text_field($_POST['action_prompt_text'])); 
    } 
    if(isset($_POST['action_button_text'])){ 
     update_post_meta($post_id, 'actionblock_meta_button_text', sanitize_text_field($_POST['action_button_text'])); 
    } 
    if(isset($_POST['action_prompt_link'])){ 
     update_post_meta($post_id, 'actionblock_meta_button_link', esc_url($_POST['action_prompt_link'])); 
    } 
    } 

    // Save action block details when the post is saved/updated 
    add_action('save_post', 'actionblock_save_meta_box_data'); 
+1

它不清楚你的问题是什么?但你在上面的代码中有一个错字,在你的actionblock_meta_box_callback()函数中丢失了关闭} – David

+0

@David:O你是对的我完全搞砸了!谢啦! – maxxon15

Try Below code : 

// Add support for header images 
if(function_exists('add_theme_support')){ 
add_theme_support('post-thumbnails'); // Adds this for both pages and posts, For posts/page only, see https://codex.wordpress.org/Function_Reference/add_theme_support 
} 

// Callback for adding metaboxes 
function actionblock_add_meta_box(){ 
$screens = array('post', 'page'); 
foreach ($screens as $screen) { 
add_meta_box('action_block', 'Action Block', 'actionblock_meta_box_callback', $screen, 'normal'); // Add the action block to both post and page screens 
} 
} 

// Add meta boxes for action section on each post/page 
add_action('add_meta_boxes', 'actionblock_add_meta_box'); 

function actionblock_meta_box_callback($post){ 
// Security check 
wp_nonce_field('actionblock_save_meta_box_data', 'actionblock_meta_box_nonce'); 

// Get values of the action block fields 
$action_prompt_text = get_post_meta($post->ID, 'actionblock_meta_prompt_text', true); 
$action_button_text = get_post_meta($post->ID, 'actionblock_meta_button_text', true); 
$action_button_link = get_post_meta($post->ID, 'actionblock_meta_button_link', true); 


// Display and populate fields from the database if it exists 
?> 
<p> 
<label for="action_prompt_text">Action Prompt</label><br> 
<input class="widefat" type="text" name="action_prompt_text" id="action_prompt_text" placeholder="This should be short, to the point and less salesy." value="<?php echo esc_attr($action_prompt_text); ?>"> 
</p> 
<p> 
<label for="action_button_text">Action Button Text</label><br> 
<input class="widefat" type="text" name="action_button_text" id="action_button_text" placeholder="This should prompt the visitor to take an action." value="<?php echo esc_attr($action_button_text); ?>"> 
</p> 
<p> 
<label for="action_button_link">Action Button Link</label><br> 
<input class="widefat" type="url" name="action_button_link" id="action_button_link" placeholder="Copy and paste the link from the intended page." value="<?php echo esc_attr($action_button_link); ?>"> 
</p> 
<?php 
} 
// Callback for saving metaboxes 
function actionblock_save_meta_box_data($post_id) { 


// Security checks 
if(!isset($_POST['actionblock_meta_box_nonce'])) return; 
if(!wp_verify_nonce($_POST['actionblock_meta_box_nonce'], 'actionblock_save_meta_box_data')) return; 
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) { 
    if (! current_user_can('edit_page', $post_id)) { 
    return; 
    } 
} else { 
    if (! current_user_can('edit_post', $post_id)) { 
    return; 
    } 
} 

// Do the save/update 
if(isset($_POST['action_prompt_text'])){ 
    update_post_meta($post_id, 'actionblock_meta_prompt_text', sanitize_text_field($_POST['action_prompt_text'])); 
} 
if(isset($_POST['action_button_text'])){ 
    update_post_meta($post_id, 'actionblock_meta_button_text', sanitize_text_field($_POST['action_button_text'])); 
} 
if(isset($_POST['action_button_link'])){ 
    update_post_meta($post_id, 'actionblock_meta_button_link', esc_url($_POST['action_button_link'])); 
} 
} 

// Save action block details when the post is saved/updated 
add_action('save_post', 'actionblock_save_meta_box_data'); 
+0

非常感谢,我也搞砸了这个变量名! -_- – maxxon15

+0

你好来了maxxon15 –