video timestamp code

Home » Snippets » video timestamp code
0

Created with:

Visibility: 

public

Creator: Navin awal

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Register script and enqueue it
function wpturbo_enqueue_scripts() {
    wp_enqueue_script( 'wpturbo-script', plugin_dir_url( __FILE__ ) . 'script.js', array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpturbo_enqueue_scripts' );

// Add shortcode for displaying the form and video
function wpturbo_timestamp_form() {
    ob_start();
    ?>
    <form id="wpturbo-timestamp-form" action="" method="post">
        <input type="text" name="video_url" placeholder="Enter YouTube video URL" />
        <input type="text" name="timestamp" placeholder="Enter timestamp" />
        <button type="submit">Add Timestamp</button>
    </form>
    <div id="wpturbo-video-container">
        <div id="wpturbo-video"></div>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode( 'wpturbo_timestamp_form', 'wpturbo_timestamp_form' );

// Handle form submission and add timestamp button
function wpturbo_handle_form_submission() {
    if ( isset( $_POST['video_url'] ) && ! empty( $_POST['video_url'] ) &&
         isset( $_POST['timestamp'] ) && ! empty( $_POST['timestamp'] ) ) {
        $video_url = sanitize_text_field( $_POST['video_url'] );
        $timestamp = sanitize_text_field( $_POST['timestamp'] );

        // Get current timestamp buttons from user meta
        $buttons = get_user_meta( get_current_user_id(), 'wpturbo_timestamp_buttons', true );
        if ( ! is_array( $buttons ) ) {
            $buttons = array();
        }

        // Add new timestamp button to the array
        $buttons[] = array(
            'video_url' => $video_url,
            'timestamp' => $timestamp,
        );

        // Update user meta with the new array of timestamp buttons
        update_user_meta( get_current_user_id(), 'wpturbo_timestamp_buttons', $buttons );
    }
}
add_action( 'init', 'wpturbo_handle_form_submission' );

// Add timestamp buttons to the video
function wpturbo_add_timestamp_buttons() {
    // Get timestamp buttons from user meta
    $buttons = get_user_meta( get_current_user_id(), 'wpturbo_timestamp_buttons', true );
    if ( ! is_array( $buttons ) ) {
        $buttons = array();
    }

    // Output the timestamp buttons
    foreach ( $buttons as $button ) {
        $video_url = esc_url( $button['video_url'] );
        $timestamp = esc_attr( $button['timestamp'] );
        ?>
        <button class="wpturbo-timestamp-button" data-video-url="<?php echo $video_url; ?>" data-timestamp="<?php echo $timestamp; ?>"><?php echo $timestamp; ?></button>
        <?php
    }
}
add_action( 'wp_footer', 'wpturbo_add_timestamp_buttons' );
				

Explanation:

Please note that you will need to modify the JavaScript code in the 'script.js' file to handle the embedding of the YouTube video based on the clicked timestamp button. You can use the YouTube iframe API or any other suitable method for embedding and controlling YouTube videos.

Remember to replace the existing code in your 'wp-plugin.php' file with this updated code, and make sure to create the 'script.js' file in the same directory and add the necessary JavaScript code.

Register an account to save your snippets or go Pro to get more features.