Plugin Directory

Changeset 3261851


Ignore:
Timestamp:
03/26/2025 12:42:28 AM (10 months ago)
Author:
hyperspective
Message:

Added feature to replace dynamic video with Featured Image if the video field is empty.

Location:
dynamic-video-for-divi-posts
Files:
11 added
4 edited

Legend:

Unmodified
Added
Removed
  • dynamic-video-for-divi-posts/trunk/dynamic-video-for-divi-posts.php

    r3261070 r3261851  
    22/*
    33Plugin Name: Dynamic Video for Divi Posts
    4 Description: Dynamically embeds a video into your blog posts by using a custom field. When editing a post, a new meta box labeled "Video URL" appears. Enter a video URL (e.g., a YouTube link) into this box, and then use the [dynamic_video] shortcode in your Divi Builder template. The video is automatically embedded using WordPress’s oEmbed functionality.
    5 Version: 1.0.1
     4Description: Dynamically embeds a video into your blog posts by using a custom field. When editing a post, a new meta box labeled "Video URL" appears. Enter a video URL (e.g., a YouTube link) into this box, and then use the [dynamic_video] shortcode in your Divi Builder template. The video is automatically embedded using WordPress’s oEmbed functionality. Optional feature to display featured image when no video URL is provided.
     5Version: 1.0.2
    66Author: hyperspective
     7Author URI: https://hyperspective.com
     8Plugin URI: https://wordpress.org/plugins/dynamic-video-for-divi-posts/
    79License: GPL2
    810License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1012
    1113if ( ! defined( 'ABSPATH' ) ) {
    12     exit;
     14    exit;
    1315}
    1416
    1517/**
    16  * Display the video via shortcode.
     18 * Display the video or featured image via shortcode.
    1719 *
    1820 * @param array $atts Shortcode attributes.
    19  * @return string Embedded video HTML or empty string.
     21 * @return string Embedded video HTML, featured image HTML, or empty string.
    2022 */
    2123function dynvid_display_video_shortcode( $atts ) {
    22     global $post;
    23     $video_url = get_post_meta( $post->ID, '_dynvid_video_url', true );
    24     if ( empty( $video_url ) ) {
    25         return '';
    26     }
    27     // Embed the video using WordPress's oEmbed functionality.
    28     return wp_oembed_get( esc_url( $video_url ) );
     24    global $post;
     25    $video_url = get_post_meta( $post->ID, '_dynvid_video_url', true );
     26   
     27    if ( ! empty( $video_url ) ) {
     28        // Embed the video using WordPress's oEmbed functionality.
     29        return wp_oembed_get( esc_url( $video_url ) );
     30    } elseif ( get_option( 'dynvid_use_featured_image', 'no' ) === 'yes' ) {
     31        // If no video URL and featured image option is enabled, display featured image
     32        if ( has_post_thumbnail( $post->ID ) ) {
     33            return get_the_post_thumbnail( $post->ID, 'full', array( 'class' => 'dynvid-featured-image' ) );
     34        }
     35    }
     36    return '';
    2937}
    3038add_shortcode( 'dynamic_video', 'dynvid_display_video_shortcode' );
     
    3442 */
    3543function dynvid_add_video_metabox() {
    36     add_meta_box(
    37         'dynvid_video_metabox',
    38         esc_html__( 'Video URL', 'dynamic-video-for-divi-posts' ),
    39         'dynvid_video_url_metabox_callback',
    40         'post',
    41         'side',
    42         'default'
    43     );
     44    add_meta_box(
     45        'dynvid_video_metabox',
     46        esc_html__( 'Video URL', 'dynamic-video-for-divi-posts' ),
     47        'dynvid_video_url_metabox_callback',
     48        'post',
     49        'side',
     50        'default'
     51    );
    4452}
    4553add_action( 'add_meta_boxes', 'dynvid_add_video_metabox' );
     
    5159 */
    5260function dynvid_video_url_metabox_callback( $post ) {
    53     // Add nonce for security.
    54     wp_nonce_field( 'dynvid_video_url_save', 'dynvid_video_url_nonce' );
    55     $video_url = get_post_meta( $post->ID, '_dynvid_video_url', true );
    56     ?>
    57     <p>
    58         <label for="dynvid_video_url"><?php esc_html_e( 'Enter the video URL (e.g., YouTube):', 'dynamic-video-for-divi-posts' ); ?></label>
    59     </p>
    60     <p>
    61         <input type="text" name="dynvid_video_url" id="dynvid_video_url" value="<?php echo esc_attr( $video_url ); ?>" style="width:100%;" />
    62     </p>
    63     <?php
     61    wp_nonce_field( 'dynvid_video_url_save', 'dynvid_video_url_nonce' );
     62    $video_url = get_post_meta( $post->ID, '_dynvid_video_url', true );
     63    ?>
     64    <p>
     65        <label for="dynvid_video_url"><?php esc_html_e( 'Enter the video URL (e.g., YouTube):', 'dynamic-video-for-divi-posts' ); ?></label>
     66    </p>
     67    <p>
     68        <input type="text" name="dynvid_video_url" id="dynvid_video_url" value="<?php echo esc_attr( $video_url ); ?>" style="width:100%;" />
     69    </p>
     70    <?php
    6471}
    6572
     
    7077 */
    7178function dynvid_save_video_url_meta( $post_id ) {
    72     // Check if nonce is set and valid.
    73     if ( ! isset( $_POST['dynvid_video_url_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['dynvid_video_url_nonce'] ) ), 'dynvid_video_url_save' ) ) {
    74         return;
    75     }
    76     // Check for autosave.
    77     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    78         return;
    79     }
    80     // Verify user permissions.
    81     if ( isset( $_POST['post_type'] ) && 'post' === $_POST['post_type'] ) {
    82         if ( ! current_user_can( 'edit_post', $post_id ) ) {
    83             return;
    84         }
    85     }
    86     if ( ! isset( $_POST['dynvid_video_url'] ) ) {
    87         return;
    88     }
    89     // Sanitize and update the video URL.
    90     $video_url = sanitize_text_field( wp_unslash( $_POST['dynvid_video_url'] ) );
    91     update_post_meta( $post_id, '_dynvid_video_url', esc_url_raw( $video_url ) );
     79    if ( ! isset( $_POST['dynvid_video_url_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['dynvid_video_url_nonce'] ) ), 'dynvid_video_url_save' ) ) {
     80        return;
     81    }
     82    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
     83        return;
     84    }
     85    if ( isset( $_POST['post_type'] ) && 'post' === $_POST['post_type'] ) {
     86        if ( ! current_user_can( 'edit_post', $post_id ) ) {
     87            return;
     88        }
     89    }
     90    if ( ! isset( $_POST['dynvid_video_url'] ) ) {
     91        return;
     92    }
     93    $video_url = sanitize_text_field( wp_unslash( $_POST['dynvid_video_url'] ) );
     94    update_post_meta( $post_id, '_dynvid_video_url', esc_url_raw( $video_url ) );
    9295}
    9396add_action( 'save_post', 'dynvid_save_video_url_meta' );
    9497
    9598/**
    96  * Add an admin submenu page for plugin details.
     99 * Add an admin submenu page for plugin details and settings.
    97100 */
    98101function dynvid_add_admin_menu() {
    99     add_submenu_page(
    100         'options-general.php',
    101         esc_html__( 'Dynamic Video for Divi Posts Details', 'dynamic-video-for-divi-posts' ),
    102         esc_html__( 'Dynamic Video for Divi Posts', 'dynamic-video-for-divi-posts' ),
    103         'manage_options',
    104         'dynvid_plugin_details_page',
    105         'dynvid_plugin_details_page'
    106     );
     102    add_submenu_page(
     103        'options-general.php',
     104        esc_html__( 'Dynamic Video for Divi Posts Details', 'dynamic-video-for-divi-posts' ),
     105        esc_html__( 'Dynamic Video for Divi Posts', 'dynamic-video-for-divi-posts' ),
     106        'manage_options',
     107        'dynvid_plugin_details_page',
     108        'dynvid_plugin_details_page'
     109    );
    107110}
    108111add_action( 'admin_menu', 'dynvid_add_admin_menu' );
     112
     113/**
     114 * Register settings for the plugin.
     115 */
     116function dynvid_register_settings() {
     117    register_setting( 'dynvid_settings_group', 'dynvid_use_featured_image', array(
     118        'type' => 'string',
     119        'sanitize_callback' => 'sanitize_text_field',
     120        'default' => 'no'
     121    ));
     122}
     123add_action( 'admin_init', 'dynvid_register_settings' );
    109124
    110125/**
     
    112127 */
    113128function dynvid_plugin_details_page() {
    114     ?>
    115     <div class="wrap">
    116         <h1><?php esc_html_e( 'Dynamic Video for Divi Posts Details', 'dynamic-video-for-divi-posts' ); ?></h1>
    117         <p><?php esc_html_e( 'This plugin dynamically embeds a video into your blog posts using a custom field. When editing a post, enter a video URL (e.g., a YouTube link) in the Video URL meta box, and then use the [dynamic_video] shortcode in your Divi Builder template to display the video.', 'dynamic-video-for-divi-posts' ); ?></p>
    118         <h2><?php esc_html_e( 'Usage Instructions', 'dynamic-video-for-divi-posts' ); ?></h2>
    119         <ol>
    120             <li><?php esc_html_e( 'Edit a post and enter the video URL in the meta box on the right.', 'dynamic-video-for-divi-posts' ); ?></li>
    121             <li><?php esc_html_e( 'In your Divi Builder template, add a Code or Text module and insert the shortcode: [dynamic_video]', 'dynamic-video-for-divi-posts' ); ?></li>
    122             <li><?php esc_html_e( 'The video will be embedded automatically using WordPress’s oEmbed functionality.', 'dynamic-video-for-divi-posts' ); ?></li>
    123         </ol>
    124     </div>
    125     <?php
     129    ?>
     130    <div class="wrap">
     131        <h1><?php esc_html_e( 'Dynamic Video for Divi Posts Details', 'dynamic-video-for-divi-posts' ); ?></h1>
     132        <p><?php esc_html_e( 'This plugin dynamically embeds a video into your blog posts using a custom field. When editing a post, enter a video URL (e.g., a YouTube link) in the Video URL meta box, and then use the [dynamic_video] shortcode in your Divi Builder template to display the video.', 'dynamic-video-for-divi-posts' ); ?></p>
     133       
     134        <form method="post" action="options.php">
     135            <?php
     136            settings_fields( 'dynvid_settings_group' );
     137            do_settings_sections( 'dynvid_settings_group' );
     138            $use_featured_image = get_option( 'dynvid_use_featured_image', 'no' );
     139            ?>
     140            <h2><?php esc_html_e( 'Settings', 'dynamic-video-for-divi-posts' ); ?></h2>
     141            <table class="form-table">
     142                <tr>
     143                    <th scope="row"><?php esc_html_e( 'Use Featured Image Fallback', 'dynamic-video-for-divi-posts' ); ?></th>
     144                    <td>
     145                        <input type="checkbox" name="dynvid_use_featured_image" value="yes" <?php checked( $use_featured_image, 'yes' ); ?> />
     146                        <?php esc_html_e( 'Display featured image when no video URL is provided', 'dynamic-video-for-divi-posts' ); ?>
     147                    </td>
     148                </tr>
     149            </table>
     150            <?php submit_button(); ?>
     151        </form>
     152
     153        <h2><?php esc_html_e( 'Usage Instructions', 'dynamic-video-for-divi-posts' ); ?></h2>
     154        <ol>
     155            <li><?php esc_html_e( 'Edit a post and enter the video URL in the meta box on the right.', 'dynamic-video-for-divi-posts' ); ?></li>
     156            <li><?php esc_html_e( 'In your Divi Builder template, add a Code or Text module and insert the shortcode: [dynamic_video]', 'dynamic-video-for-divi-posts' ); ?></li>
     157            <li><?php esc_html_e( 'The video will be embedded automatically using WordPress’s oEmbed functionality.', 'dynamic-video-for-divi-posts' ); ?></li>
     158            <li><?php esc_html_e( 'If no video URL is provided and the featured image option is enabled, the post\'s featured image will be displayed instead.', 'dynamic-video-for-divi-posts' ); ?></li>
     159        </ol>
     160    </div>
     161    <?php
    126162}
  • dynamic-video-for-divi-posts/trunk/readme.txt

    r3261070 r3261851  
    11=== Dynamic Video for Divi Posts ===
    22Contributors: hyperspective
    3 Tags: video, dynamic, Divi, embed, shortcode
     3Tags: video, dynamic, Divi, embed, shortcode, featured image
    44Requires at least: 5.0
    55Tested up to: 6.7
    6 Stable tag: 1.0.1
     6Stable tag: 1.0.2
    77License: GPL2
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    99
    10 Short Description: Embeds videos into posts via a custom field and [dynamic_video] shortcode for Divi.
     10Short Description: Embeds videos into posts via a custom field and [dynamic_video] shortcode for Divi, with optional featured image fallback.
    1111
    1212== Description ==
    13 Dynamic Video for Divi Posts dynamically embeds a video into your blog posts by using a custom field. When editing a post, a new meta box labeled "Video URL" appears. Enter a video URL (e.g., a YouTube link) into this box, and then use the [dynamic_video] shortcode in your Divi Builder template. The video is automatically embedded using WordPress’s oEmbed functionality.
     13Dynamic Video for Divi Posts dynamically embeds a video into your blog posts by using a custom field. When editing a post, a new meta box labeled "Video URL" appears. Enter a video URL (e.g., a YouTube link) into this box, and then use the [dynamic_video] shortcode in your Divi Builder template. The video is automatically embedded using WordPress’s oEmbed functionality. New in version 1.0.2: Optional setting to display the post's featured image when no video URL is provided.
    1414
    1515== Installation ==
     
    18183. Edit any post to see the "Video URL" meta box and enter your video URL.
    19194. In your Divi Builder template, add a Code or Text module and insert the shortcode: `[dynamic_video]`.
     205. (Optional) Go to Settings > Dynamic Video for Divi Posts to enable the featured image fallback option.
     21
     22== Screenshots ==
     231. Settings, Instructions
     242. Snippet example for Divi Code Module
     253. Blog entry for video URL
    2026
    2127== Frequently Asked Questions ==
     
    2430
    2531= Why isn’t my video appearing? =
    26 Ensure that the video URL is correct and that your post type supports the shortcode. Also, verify that your Divi Builder template includes the `[dynamic_video]` shortcode.
     32Ensure that the video URL is correct and that your post type supports the shortcode. Also, verify that your Divi Builder template includes the `[dynamic_video]` shortcode. If no video appears and the featured image option is enabled, check if the post has a featured image set.
     33
     34= How does the featured image fallback work? =
     35When enabled in the plugin settings, if no video URL is provided for a post, the plugin will display the post's featured image instead of leaving the space blank. You can turn this feature on or off in the plugin settings.
    2736
    2837== Changelog ==
     38= 1.0.2 =
     39* Added optional feature to display featured image when no video URL is provided
     40* Added settings page checkbox to enable/disable featured image fallback
     41* Updated documentation to include new feature instructions
     42
    2943= 1.0.1 =
    3044* Renamed all functions, meta keys, nonce names, and admin slugs to use the unique prefix "dynvid_" to avoid naming conflicts.
     
    3549
    3650== Upgrade Notice ==
    37 = 1.0.1 =
    38 This update updates function naming conventions and security practices to comply with WordPress coding standards. Please update to this version to continue using the plugin with enhanced security and compatibility.
     51= 1.0.2 =
     52This update adds an optional featured image fallback feature when no video URL is provided, with a settings toggle. Update to access this new functionality while maintaining all previous features.
Note: See TracChangeset for help on using the changeset viewer.