admin管理员组

文章数量:1122826

I have added some custom fields to the Gutenberg editor, the purpose is getting these custom fields loaded in the custom-post-type.

Thats is all working, I was able to get the custom-fields in the editor and I am able to fill in data and when I save or update the post I am getting the notification that the post is updated and the data stays in the fields.

But if I refresh the page, the data in fields gets removed. When I check the database I can't see the custom-fields in the wp_postmeta or wp_posts been saved.

This is my code, what am I doing wrong?

<?php
// Add custom metabox for single artiesten posts
function custom_artiesten_metabox() {
    add_meta_box(
        'custom_artiesten_metabox', // Metabox ID
        'Custom Fields', // Title
        'render_custom_artiesten_metabox', // Callback function to render the metabox content
        'artiesten', // Post type
        'normal', // Context
        'high' // Priority
    );
}
add_action('add_meta_boxes', 'custom_artiesten_metabox');

// Render custom metabox content
function render_custom_artiesten_metabox($post) {

    // Retrieve existing values from post meta
    $custom_field_oa_datum = get_post_meta($post->ID, 'oa_datum', true);
    $custom_field_oa_aanvang = get_post_meta($post->ID, 'oa_aanvang', true);
    $custom_field_oa_locatie = get_post_meta($post->ID, 'oa_locatie', true);
    $custom_field_oa_toegang_prijs = get_post_meta($post->ID, 'oa_toegang_prijs', true);
    ?>
    <div class="artiesten-mb-cf">
        <div>
            <label for="oa_datum">Datum:</label>
            <input type="text" id="oa_datum" name="oa_datum" value="<?php echo esc_attr($custom_field_oa_datum); ?>">
        </div>
        <div>
            <label for="oa_aanvang">Aanvang:</label>
            <input type="text" id="oa_aanvang" name="oa_aanvang" value="<?php echo esc_attr($custom_field_oa_aanvang); ?>">
        </div>
        <div>
            <label for="oa_locatie">Locatie:</label>
            <input type="text" id="oa_locatie" name="oa_locatie" value="<?php echo esc_attr($custom_field_oa_locatie); ?>">
        </div>
        <div>
            <label for="oa_toegang_prijs">Toegang:</label>
            <input type="text" id="oa_toegang_prijs" name="oa_toegang_prijs" value="<?php echo esc_attr($custom_field_oa_toegang_prijs); ?>">
        </div>
    </div>
    <?php
}

// Save custom metabox data
function save_custom_artiesten_metabox($post_id) {
    // Check if nonce is set
    if (!isset($_POST['custom_artiesten_metabox_nonce'])) {
        return;
    }
    // Verify nonce
    if (!wp_verify_nonce($_POST['custom_artiesten_metabox_nonce'], 'custom_artiesten_metabox')) {
        return;
    }
    // Check if this is an autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check if the user has permissions to save data
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    // Save custom field data
    if (isset($_POST['oa_datum'])) {
        update_post_meta($post_id, 'oa_datum', sanitize_text_field($_POST['oa_datum']));
    }
    if (isset($_POST['oa_aanvang'])) {
        update_post_meta($post_id, 'oa_aanvang', sanitize_text_field($_POST['oa_aanvang']));
    }
    if (isset($_POST['oa_locatie'])) {
        update_post_meta($post_id, 'oa_locatie', sanitize_text_field($_POST['oa_locatie']));
    }
    if (isset($_POST['oa_toegang_prijs'])) {
        update_post_meta($post_id, 'oa_toegang_prijs', sanitize_text_field($_POST['oa_toegang_prijs']));
    }
}

add_action('save_post', 'save_custom_artiesten_metabox');

// Enqueue scripts and styles for the metabox
function enqueue_custom_artiesten_metabox_scripts($hook) {
    global $post;
    if ($hook == 'post-new.php' || $hook == 'post.php') {
        if ($post->post_type == 'artiesten') {
            wp_enqueue_style('custom-artiesten-metabox-style', get_stylesheet_directory_uri() . '/artiesten/artiesten.css');
        }
    }
}
add_action('admin_enqueue_scripts', 'enqueue_custom_artiesten_metabox_scripts');

I have added some custom fields to the Gutenberg editor, the purpose is getting these custom fields loaded in the custom-post-type.

Thats is all working, I was able to get the custom-fields in the editor and I am able to fill in data and when I save or update the post I am getting the notification that the post is updated and the data stays in the fields.

But if I refresh the page, the data in fields gets removed. When I check the database I can't see the custom-fields in the wp_postmeta or wp_posts been saved.

This is my code, what am I doing wrong?

<?php
// Add custom metabox for single artiesten posts
function custom_artiesten_metabox() {
    add_meta_box(
        'custom_artiesten_metabox', // Metabox ID
        'Custom Fields', // Title
        'render_custom_artiesten_metabox', // Callback function to render the metabox content
        'artiesten', // Post type
        'normal', // Context
        'high' // Priority
    );
}
add_action('add_meta_boxes', 'custom_artiesten_metabox');

// Render custom metabox content
function render_custom_artiesten_metabox($post) {

    // Retrieve existing values from post meta
    $custom_field_oa_datum = get_post_meta($post->ID, 'oa_datum', true);
    $custom_field_oa_aanvang = get_post_meta($post->ID, 'oa_aanvang', true);
    $custom_field_oa_locatie = get_post_meta($post->ID, 'oa_locatie', true);
    $custom_field_oa_toegang_prijs = get_post_meta($post->ID, 'oa_toegang_prijs', true);
    ?>
    <div class="artiesten-mb-cf">
        <div>
            <label for="oa_datum">Datum:</label>
            <input type="text" id="oa_datum" name="oa_datum" value="<?php echo esc_attr($custom_field_oa_datum); ?>">
        </div>
        <div>
            <label for="oa_aanvang">Aanvang:</label>
            <input type="text" id="oa_aanvang" name="oa_aanvang" value="<?php echo esc_attr($custom_field_oa_aanvang); ?>">
        </div>
        <div>
            <label for="oa_locatie">Locatie:</label>
            <input type="text" id="oa_locatie" name="oa_locatie" value="<?php echo esc_attr($custom_field_oa_locatie); ?>">
        </div>
        <div>
            <label for="oa_toegang_prijs">Toegang:</label>
            <input type="text" id="oa_toegang_prijs" name="oa_toegang_prijs" value="<?php echo esc_attr($custom_field_oa_toegang_prijs); ?>">
        </div>
    </div>
    <?php
}

// Save custom metabox data
function save_custom_artiesten_metabox($post_id) {
    // Check if nonce is set
    if (!isset($_POST['custom_artiesten_metabox_nonce'])) {
        return;
    }
    // Verify nonce
    if (!wp_verify_nonce($_POST['custom_artiesten_metabox_nonce'], 'custom_artiesten_metabox')) {
        return;
    }
    // Check if this is an autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check if the user has permissions to save data
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    // Save custom field data
    if (isset($_POST['oa_datum'])) {
        update_post_meta($post_id, 'oa_datum', sanitize_text_field($_POST['oa_datum']));
    }
    if (isset($_POST['oa_aanvang'])) {
        update_post_meta($post_id, 'oa_aanvang', sanitize_text_field($_POST['oa_aanvang']));
    }
    if (isset($_POST['oa_locatie'])) {
        update_post_meta($post_id, 'oa_locatie', sanitize_text_field($_POST['oa_locatie']));
    }
    if (isset($_POST['oa_toegang_prijs'])) {
        update_post_meta($post_id, 'oa_toegang_prijs', sanitize_text_field($_POST['oa_toegang_prijs']));
    }
}

add_action('save_post', 'save_custom_artiesten_metabox');

// Enqueue scripts and styles for the metabox
function enqueue_custom_artiesten_metabox_scripts($hook) {
    global $post;
    if ($hook == 'post-new.php' || $hook == 'post.php') {
        if ($post->post_type == 'artiesten') {
            wp_enqueue_style('custom-artiesten-metabox-style', get_stylesheet_directory_uri() . '/artiesten/artiesten.css');
        }
    }
}
add_action('admin_enqueue_scripts', 'enqueue_custom_artiesten_metabox_scripts');
Share Improve this question asked Apr 15, 2024 at 2:49 BrangoBrango 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Try adding this save function to see if it saves data

function save_custom_artiesten_metabox_on_reload() {
    global $post;

    if (isset($_POST['oa_datum']) && isset($_POST['oa_aanvang']) && isset($_POST['oa_locatie']) && isset($_POST['oa_toegang_prijs'])) {
        update_post_meta($post->ID, 'oa_datum', sanitize_text_field($_POST['oa_datum']));
        update_post_meta($post->ID, 'oa_aanvang', sanitize_text_field($_POST['oa_aanvang']));
        update_post_meta($post->ID, 'oa_locatie', sanitize_text_field($_POST['oa_locatie']));
        update_post_meta($post->ID, 'oa_toegang_prijs', sanitize_text_field($_POST['oa_toegang_prijs']));
    }
}
add_action('admin_init', 'save_custom_artiesten_metabox_on_reload');

本文标签: metaboxCustom fields empty after refreshing page