duan19740319 2018-12-19 18:44
浏览 60

Wordpress 5.0+打破了自定义字段保存

Sorry about the code format, CTRL+K is said to move things 4 spaces. But it doesnt work in Chrome:(

Update: Amazing, I tried this 4 times. EAch time it triggered a Chrome Shortcut. I gave up and submitted this. It gave an error saying that I had code and did not properly format it. Then amazingly, Ctrl+K works. Not sure why this is so hard...

ANYWAYS,

I upgraded to WP 5.0 on a customer site and now all of the custom fields do not save.... I was using custom code and now the fields do not show in wordpress. It seems that everything I try today is not working. StackOverflow is now the latest to join this...

Id appreciate some help with this if anyone can read the last result.

-------------------------
$meta_box = array(
    'id' => 'tunebot-meta-marketing-box',
    'title' => 'Enable Marketing Links?',
    'page' => 'page',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Hide Title?',
            'id' => 'tunebot_hide_titler',
            'type' => 'checkbox'
        ),
        array(
            'name' => 'Display Image?',
            'id' => 'tunebot_display_image',
            'type' => 'checkbox'
        ),
        array(
            'name' => 'Enable Top Banner?',
            'id' => 'tunebot_banner',
            'type' => 'checkbox'
        ),
        array(
            'name' => 'Enable Marketing Links?',
            'id' => 'tunebot_marketing_links',
            'type' => 'checkbox'
        ),

    )
);

add_action('admin_init', 'tunebot_add_box');
// Add meta box
function tunebot_add_box() {
    global $meta_box;
    add_action('post_submitbox_misc_actions', 'tunebot_show_box');
    //add_meta_box($meta_box['id'], $meta_box['title'], 'tunebot_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}


/**
 * Shows a box with options bellow it, under the text of the body.
 */
function tunebot_show_box() {
    global $meta_box, $post;

    // Use nonce for verification
    $output = '<input type="hidden" name="tunebot_meta_box_nonce" value="' . wp_create_nonce(basename(__FILE__)) . '"/>';

    foreach($meta_box['fields'] as $field) {
        // get current post meta data
        $postId = $post->ID;
        $key = 'tb_' . $field['id'];
        $meta = get_post_meta($postId, $field['id'], TRUE);

        $output .= '<div class="misc-pub-section misc-pub-section-last">';

        switch($field['type']) {
            case 'text':
                $output .= '<input type="text" name="' . $key . '" id="' . $key . '"
                               value="' . $meta ? $meta : $field['std'] . '" size="30" style="width:97%"/>' . '<br/>' .
                    $field['desc'];
                break;
            case 'textarea':
                $output .= '<textarea name="' . $key . '" id="' . $key . '" cols="60" rows="4"
                                  style="width:97%">' . $meta ? $meta : $field['std'] . '</textarea>' . '<br/>' .
                    $field['desc'];
                break;
            case 'select':
                $output .= '<select name="' . $key . '" id="' . $key . '">';
                foreach($field['options'] as $option) {
                    $output .= '
                <option
                ' . $meta == $option ? ' selected="selected"' : '' . '>' . $option . '</option>';
                }
                $output .= '</select>';
                break;
            case 'radio':
                foreach($field['options'] as $option) {
                    $output .= '<input type="radio" name="' . $key . '" value="' . $option['value'] . '"' . (($meta == $option['value']) ? ' checked="checked"' : '' . ' />' . $option['name']);
                }
                break;
            case 'checkbox':
                $output .= '<input type="checkbox" name="' . $key . '" id="' . $key . '"' . ($meta == "on" ? ' checked="checked"' : '') . ' />';
                break;
        }

        $output .= '<label for="' . $key . '">' . $field['name'] . '</label></div>';
    }

    echo $output;
}

add_action('save_post', 'tunebot_save_data');

function tunebot_save_data($post_id) {
    global $meta_box;

// verify nonce
    if(isset($_POST['tunebot_meta_box_nonce']) && !wp_verify_nonce($_POST['tunebot_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

// check autosave
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

// check permissions
    if(isset($_POST['post_type']) && '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;
    }

    foreach($meta_box['fields'] as $field) {
        if(isset($field['id'])) {
            $key = 'tb_' . $field['id'];

            if(isset($_POST[$key])) {
                $new = $_POST[$key];
            } else {
                $new = FALSE;
            }

            $old = get_post_meta($post_id, $field['id'], TRUE);

            if($new && $new != $old) {

                update_post_meta($post_id, $field['id'], $new);
            } elseif('' == $new && $old) {
                delete_post_meta($post_id, $field['id'], $old);
            }

        }
    }
    return $post_id;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题