WordPress 5.0+打破自定义字段保存

时间:2018-12-19 18:44:11

标签: php wordpress

很抱歉,代码格式为CTRL + K,它可以将内容移动4个空格。但这在Chrome中不起作用:(

更新:太神奇了,我尝试了4次。每次触发Chrome浏览器快捷方式。我放弃并提交了这个。它给出一个错误,说我有代码并且未正确格式化它。然后令人惊讶的是,Ctrl + K起作用了。不知道为什么这么难...

无论如何

我在客户站点上升级到WP 5.0,现在所有自定义字段均未保存。...我正在使用自定义代码,并且现在这些字段也未显示在wordpress中。看来我今天尝试的所有东西都没有用。现在,StackOverflow是最新加入此计划的人...

如果有人可以阅读最后的结果,Id将对此提供帮助。

-------------------------
$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 个答案:

没有答案