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' );
}