Update a database value if a video is selected?

2019-08-15 09:49发布

问题:

I will try to explain this, I have a button where I can upload and select a video to add to a post. And I have a input field which I can put in a duration (in seconds). I have made a function which pulls the length of the video from the database, and this length I want to put in the database under slide_duration. And if a video isn't selected I want it to work as it usually do, you can put in a custom value (in seconds) or you can leave this input empty (which makes it revert to the standard value).

The related PHP looks like this:

if ( isset( $_POST['slide_duration'] ) && is_numeric( $_POST['slide_duration'] ) && $_POST['slide_duration']  >= 1 ) {
    $slide_duration = intval($_POST['slide_duration']);
    update_post_meta( $post_id, 'slide_duration', $slide_duration );
}
else {
    delete_post_meta( $post_id, 'slide_duration' );
}

This is how it looked before I touched it. It deletes slide_duration if no value is entered, this is how I need to keep it, to make it revert to the default value if nothing is put there. I now added this part after the above code:

if ( isset( $_POST['background-video'] ) ) {
            update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
        }

This will update the slide_duration in the database when a video is selected. But I cannot manually set the time in the input field, it always reverts to 0 when saving/updating the post. If I put the code above the original, I can put in whatever value I want in the input, but selecting a video won't update slide_duration any longer.

For more context, this is how the input looks:

<?php
    $slide_duration = isset( $theme_stored_meta['slide_duration'] ) ? intval($theme_stored_meta['slide_duration'][0]) : '';
?>
<input type="number" name="slide_duration" value="<?=$slide_duration?>" min="1" step="1" style="width: 70px;" /> <?=__('seconds', 'theme')?>

Hope I have explained well enough and provided enough of the code to get a clear picture of this.

EDIT: Tried this too:

    if ( isset( $_POST['slide_duration'] ) && is_numeric( $_POST['slide_duration'] ) && $_POST['slide_duration']  >= 1 ) {
    $slide_duration = intval($_POST['slide_duration']);
    update_post_meta( $post_id, 'slide_duration', $slide_duration );
    }
    elseif ( isset( $_POST['slide_duration'] ) ) {
        update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
    }
    if ( is_null( $_POST['slide_duration'] ) ) {
        delete_post_meta( $post_id, 'slide_duration' );
    }

回答1:

Try this:

// If a video is selected, set slide_duration to the database value.
if ( isset( $_POST['background-video'] ) ) {
    update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
// If a video is not selected, use the custom slide_duration input/value, if
// it's specified and that it is >= 1.
} elseif (
    isset( $_POST['slide_duration'] ) &&
    is_numeric( $_POST['slide_duration'] ) &&
    $_POST['slide_duration'] >= 1
) {
    update_post_meta( $post_id, 'slide_duration', (int) $_POST['slide_duration'] );
// If no video is selected, and slide_duration is either not specified or
// invalid, then we delete the meta data.
} else {
    delete_post_meta( $post_id, 'slide_duration' );
}


回答2:

Got it working finally!

I don't really know what did it, but this is working:

    if ( $_POST['slide_duration'] == NULL ) {
            delete_post_meta( $post_id, 'slide_duration' );
    }

    if ( isset( $_POST['slide_duration'] ) && is_numeric( $_POST['slide_duration'] ) && $_POST['slide_duration'] >= 1 ) {
        update_post_meta( $post_id, 'slide_duration', (int) $_POST['slide_duration'] );
    }

    if( isset( $_POST[ 'background-video' ] ) && $video_meta['length'] >= 1 ) {
          update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
    }

Thank you very much Sally CJ



标签: php wordpress