使用新值保存metabox_wp_page_模板

时间:2015-02-02 作者:Interactive

我创建了一个元框来显示页面模板,并使用单选按钮选择另一个模板。这是当前page attributes 代谢箱。

我在保存部分遗漏了一些东西。这个_wp_page_template 无法更新。以下是我目前拥有的:

<?php 
// Add the Meta Box
function add_meta_box_template_picker() {
add_meta_box(
    \'meta_box_template_picker\', // $id
    \'Template Picker\', // $title
    \'show_meta_box_template_picker\', // $callback
    \'page\', // $page
    \'normal\', // $context
    \'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_meta_box_template_picker\');

// The Callback
function show_meta_box_template_picker() {
$templates = get_page_templates(); 
foreach ( $templates as $template_name => $template_filename ) {
   if ($template_filename == \'template1.php\' || 
       $template_filename == \'template2.php\' || 
       $template_filename == \'template3.php\' ){
        echo "<input type=\'radio\' name=\'_wp_page_template\' value=\'$template_filename\' />$template_name<br />";         
   }else{
        echo \'\';
   }
}
}
// Save the Data
function save_custom_meta($post_id) {
global $templates, $post; // post of template
 $post_id = $post->ID;
// verify nonce
if (!wp_verify_nonce($_POST[\'meta_box_template_picker_nonce\'], basename(__FILE__)))
    return $post_id;
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
    return $post_id;
// check permissions
if (\'page\' == $_POST[\'post_type\']) {
    if (!current_user_can(\'edit_page\', $post_id))
        return $post_id;
    } elseif (!current_user_can(\'edit_post\', $post_id)) {
        return $post_id;
}

// loop through fields and save the data
    $old = get_post_meta($post_id, \'_wp_page_template\', true);
    $new = $_POST[\'_wp_page_template\'];
    if ($new && $new != $old) {
        update_post_meta($post_id, \'_wp_page_template\', $new);
    } elseif (\'\' == $new && $old) {
        delete_post_meta($post_id, \'_wp_page_template\', $old);
    }
} // end foreach

add_action(\'save_post\', \'save_custom_meta\');

?>
希望有人看到我做错了什么。

1 个回复
SO网友:Interactive

我向上帝发誓。。。。。!!!每次我在这里问问题,我似乎都能找到答案。。。。

我错过了验证:
echo \'<input type="hidden" name="meta_box_template_picker_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

下面是完整的代码。如果你认为你可以使用这个,请这样做。

如果您想使用它,但不知道如何使用:
1。创建单独的.php 文件和名称,例如。custom_template_picker.php
2。在下面添加完整的代码,并将其保存在主题文件夹中
3。打开functions.php 并将此添加到之前的任何位置?> =>
include \'custom_template_picker.php\';
4。在里面functions.php 还要添加以下内容以禁用当前属性元框:

function my_remove_meta_boxes() {
remove_meta_box( \'pageparentdiv\' , \'page\' , \'normal\' );
}
add_action( \'admin_menu\', \'my_remove_meta_boxes\' );
第19、20和21行代码中的模板文件名是您希望看到的。如果只希望看到一些模板,请更改这些模板
  • 现在,您可以看到的模板输出是radio 按钮。您可以更改echo 在第22行,输入您希望输出的内容
  • 将这些文件上载到服务器,您应该进行设置。

    <?php 
    // Add the Meta Box
    function add_meta_box_template_picker() {
    add_meta_box(
    \'meta_box_template_picker\', // $id
    \'Template Picker\', // $title
    \'show_meta_box_template_picker\', // $callback
    \'page\', // $page
    \'normal\', // $context
    \'high\'); // $priority
    }
    add_action(\'add_meta_boxes\', \'add_meta_box_template_picker\');
    
    // The Callback
    function show_meta_box_template_picker() {
    echo \'<input type="hidden" name="meta_box_template_picker_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
    $templates = get_page_templates(); 
    foreach ( $templates as $template_name => $template_filename ) {
    if ($template_filename == \'template1.php\' || 
       $template_filename == \'template2.php\' || 
       $template_filename == \'template3.php\' ){
        echo "<input type=\'radio\' name=\'_wp_page_template\' value=\'$template_filename\' />$template_name<br />";         
    }else{
        echo \'\';
    }
    }
    echo get_post_meta(249, \'_wp_page_template\', true);
    }
    // Save the Data
    function save_custom_meta($post_id) {
    global $templates, $post; // post of template
    $post_id = $post->ID;
    // verify nonce
    if (!wp_verify_nonce($_POST[\'meta_box_template_picker_nonce\'], basename(__FILE__)))
    return $post_id;
    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
    return $post_id;
     // check permissions
    if (\'page\' == $_POST[\'post_type\']) {
    if (!current_user_can(\'edit_page\', $post_id))
        return $post_id;
    } elseif (!current_user_can(\'edit_post\', $post_id)) {
        return $post_id;
    }
    
    // loop through fields and save the data
    $old = get_post_meta($post_id, \'_wp_page_template\', true);
    $new = $_POST[\'_wp_page_template\'];
    if ($new && $new != $old) {
        update_post_meta($post_id, \'_wp_page_template\', $new);
    } elseif (\'\' == $new && $old) {
        delete_post_meta($post_id, \'_wp_page_template\', $old);
    }
    } // end foreach
    
    add_action(\'save_post\', \'save_custom_meta\');
    
    ?>
    
  • **********欢迎提出任何建议/改进***************

    我希望我能在这方面帮助别人

    结束

    相关推荐

    如何用PHP取代URL协议?

    我在我的网站上到处都使用HTTPS,但我对Pinterest和Tumblr的共享按钮有问题。例如,当我尝试在Pinterest上共享图像时,会出现以下错误:“很抱歉,我们无法获取图像。”为了让它工作,我想我需要将图像源URL协议更改为HTTP,仅用于我的共享按钮。这是我获取缩略图源的方式:$share_image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), \'large\' ); 我需要更改此选项:https:// w