1

I've tried searching but all the "add a dashboard widget" answers seem overcomplicated trying to do far too much -- or else they are too simple (e.g. the widget simply displays a message; does not pass a variable).

I have added a line of code on the main template (homepage) to embed a video and I want to have a single text input field on the DASHBOARD for a fellow admin to enter a YouTube URL and have that URL replace the currently embedded URL. Just something simple where the variable is updated. Please can someone give me step-by-step instructions on exactly how to do this?

The lines of code I introduced to the template are:

<?php
        $your_YouTube_url = 'https://www.youtube.com/watch?v=w6DW4i-mfbA';
        echo wp_oembed_get( $your_YouTube_url );
?>

All I want to do is put ONE text input field on the dashboard to update variable $your_YouTube_url. My forte is HTML/CSS/js; I know a little PHP and am familiar with WP but not an expert. My fellow admin is totally non-tech so I think we'd both be more comfortable going that route rather than having him update the line of code each day. (The video in question is not livestreamed at YT or I'd just go that route with the livestream URL.) Thanks!

1

Add the below code in the functions.php file and it will create a text field in the general setting options page

add_action('admin_init', 'embed_url_initialize');

function embed_url_initialize() {

    // First, we register a section. This is necessary since all future options must belong to one. 

    add_settings_section(

        'general_settings_section',         // ID used to identify this section and with which to register options

        'Embed Url Option',                  // Title to be displayed on the administration page

        'embed_url_general_options_callback', // Callback used to render the description of the section

        'general'                           // Page on which to add this section of options

    ); 

    // Next, we will introduce the fields for toggling the visibility of content elements.

    add_settings_field( 

        'video_url',                      // ID used to identify the field throughout the theme

        'Enter Video Url',                  // The label to the left of the option interface element

        'video_url_callback',   // The name of the function responsible for rendering the option interface

        'general',                          // The page on which this option will be displayed

        'general_settings_section'         // The name of the section to which this field belongs



    );

    // Finally, we register the fields with WordPress

    register_setting(

        'general',

        'video_url'

    );
} 

/* ------------------------------------------------------------------------ *

 * Section Callbacks

 * ------------------------------------------------------------------------ */
/**

 * This function provides a simple description for the General Options page. 

 *

 * It is called from the 'embed_url_initialize' function by being passed as a parameter

 * in the add_settings_section function.

 */

function embed_url_general_options_callback() {

    echo '<p>Please enter the embed url.</p>';

} // end embed_url_general_options_callback



/* ------------------------------------------------------------------------ *

 * Field Callbacks

 * ------------------------------------------------------------------------ */

/**

 * This function renders the interface element for input embed url.

 */

function video_url_callback($args) {

    $html = '<input type="textbox" class="regular-text" id="video_url" name="video_url" value="' . get_option("video_url") .'" />';    

    echo $html;
} // end video_url_callback_callback

then call the below code in the template file where you want to get the embed video

<?php if(get_option('video_url')) { 
    $embed = wp_oembed_get( get_option('video_url') );
    if( $embed ) {
        echo $embed;
    } else {
        // The embed HTML couldn't be fetched
    }
 } // end if ?>
  • Our installation doesn't have a general top-level "functions.php" page so I used theme-functions.php instead. It seems to be working fine so far, so I have accepted your answer and want to say thank you very much for the help! I will definitely keep a copy of this code handy to use in the future as well in case I need or want to add more. – code-sushi Nov 15 '17 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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