4
我正在使用WordPress的帖子和頁面的視覺作曲家實際上爲所有。但是我想在帖子編輯器的屏幕下製作一些自定義元框。其實我已經做了田野。但現在我想讓視覺作曲家可以使用這些字段。其實我想在視覺編輯器中添加這些字段。我怎樣才能做到這一點?請幫助我提供寶貴的知識。如何在WordPress中爲視覺作曲家製作自定義元框?
這裏是我的元盒
<?php
function myplugin_add_meta_box() {
$screens = array('post', 'page');
foreach ($screens as $screen) {
add_meta_box(
'myplugin_sectionid',
__('My Post Section Title', 'myplugin_textdomain'),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action('add_meta_boxes', 'myplugin_add_meta_box');
function myplugin_meta_box_callback($post) {
wp_nonce_field('myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta($post->ID, '_my_meta_value_key', true);
echo '<label for="myplugin_new_field">';
_e('Description for this field', 'myplugin_textdomain');
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr($value) . '" size="25" />';
}
function myplugin_save_meta_box_data($post_id) {
if (! isset($_POST['myplugin_meta_box_nonce'])) {
return;
}
// Verify that the nonce is valid.
if (! wp_verify_nonce($_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check the user's permissions.
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;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if (! isset($_POST['myplugin_new_field'])) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field($_POST['myplugin_new_field']);
// Update the meta field in the database.
update_post_meta($post_id, '_my_meta_value_key', $my_data);
}
add_action('save_post', 'myplugin_save_meta_box_data');