0

Originally my goal was to compress my code, now after I altered it, it is not working anymore. I can't figure out why. I simplified it a bit, now this code should save one date entered in the backend in a custom meta box of a custom post type but apparently the update_post_meta() function is not working anymore. E.g. if I select a date and save the post, the change isn't saving.

add_action ('admin_init', 'cpt_event_meta_fields');
add_action ('save_post', 'cpt_event_save_data');
    function cpt_event_meta_fields() {
        add_meta_box("event-meta-1", "Datum (von)", "cpt_event_callback", "Termin", "normal", "high");
    }
    function cpt_event_callback( $post ) {
        $dateStart = get_post_meta( get_the_ID(), 'date-start', true );
        echo $dateStart;
        echo '<input type="date" name="date-start" value="' . $dateStart . '" />';
    }
    function cpt_event_save_data() {
        global $post;
        update_post_meta(get_the_ID(), "date-start", $dateStart);
    }

After reviewing the answer below is myupdated code. My main problem was the use of global $postoutside of the Loop. I used global $_POST instead. Also I now changed the input from date to text and sanitized the textfields using sanitize_text_field()

add_action ('admin_init', 'cpt_event_meta_fields');
add_action ('save_post', 'cpt_event_save_data');
function cpt_event_meta_fields() {
    add_meta_box("event-meta-1", "Datum (von)", "cpt_event_callback", "Termin", "normal", "high");
    add_meta_box("event-meta-2", "Datum (bis)", "cpt_event_2_callback", "Termin", "normal", "high");
}
function cpt_event_callback( $post ) {
    $dateStart = get_post_meta( get_the_ID(), 'date-start', true );
    echo '<input type="text" name="date-start" value="' . $dateStart . '" />';
}
function cpt_event_2_callback( $post ) {
    $dateEnd = get_post_meta( get_the_ID(), 'date-end', true );
    echo '<input type="text" name="date-end" value="' . $dateEnd . '" />';
}
function cpt_event_save_data() {
    global $_POST;
    global $dateStart;
    $dateStart = sanitize_text_field($_POST['date-start']);
    $dateEnd = sanitize_text_field($_POST['date-end']);
    update_post_meta(get_the_ID(), "date-start", $dateStart);
    update_post_meta(get_the_ID(), "date-end", $dateEnd);
}
0

1 Answer 1

1

get_the_ID() is a "Loop" function. It won't outside the Loop, and there is not Loop in your code. Essentially, you are passing a bad ID to the function causing it to fail. If you had debugging enabled you might see feedback to that effect.

You are also using a variable-- $dateStart-- that is not set in the context of the function.

You are not properly using the save_post hook, however. WordPress will pass the post ID through to your callback if you ask for it.

Finally, what you want is global $_POST; which is the PHP form data, and not global $post; with is a WordPress variable that is a primary component of Loops.

function cpt_event_save_data($pid) {
    global $_POST;
    $dateStart = $_POST['date-start']; 
    // sanitize $datestart, maybe with PHP's checkdate()
    // I don't know what the format of your string is though
    update_post_meta($pid, "date-start", $dateStart);
}

Untested, but that should be pretty close.

7
  • thank you, the code works flawless. I have debugging enabled but wasn't able to see any feedback in this case. How am I not using save_post correctly? I Wasn't able to sanitize the date yet, it is selected in the standard formattin "dd.mm.jjjj" - How would you approach this? Coudn't get it done with checkdate and I am confused by all the possible ways to sanitize PHP
    – MrKainig
    Commented Dec 7, 2015 at 15:14
  • "How am I not using save_post correctly?" -- I explained that. You are not requesting the post ID via the hook but were trying to use get_the_ID().
    – s_ha_dum
    Commented Dec 7, 2015 at 15:15
  • Is your date reliably in that format? Your form doesn't actually seem to enforce any format at all.
    – s_ha_dum
    Commented Dec 7, 2015 at 15:16
  • Do I have to enforce a format? In the backend there is a date picker working just the way I want. I Didn't output the meta data to the frontend yet though
    – MrKainig
    Commented Dec 7, 2015 at 15:20
  • JavaScript can be bypassed but it is probably adequate in this case, with proper sanitization.
    – s_ha_dum
    Commented Dec 7, 2015 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.