是否将字段添加到仪表板以更新主页上的嵌入URL?

时间:2017-11-14 作者:code-sushi

我尝试过搜索,但所有“添加仪表板小部件”的答案似乎都过于复杂,试图做得太多了,否则它们太简单了(例如,小部件只显示一条消息;不传递变量)。

我在主模板(主页)上添加了一行代码来嵌入视频,我希望在仪表板上有一个文本输入字段,以便其他管理员输入YouTube URL,并将该URL替换为当前嵌入的URL。只是更新变量的简单方法。请有人给我一步一步的指导,具体如何做?

我介绍给模板的代码行有:

<?php
        $your_YouTube_url = \'https://www.youtube.com/watch?v=w6DW4i-mfbA\';
        echo wp_oembed_get( $your_YouTube_url );
?>
我只想在仪表板上输入一个文本字段来更新变量$your\\u YouTube\\u url。我的专长是HTML/CSS/js;我懂一点PHP,熟悉WP,但不是专家。我的同事是一名完全不懂技术的管理员,所以我想我们都会更愿意走这条路,而不是让他每天更新一行代码。(所讨论的视频不是在YT直播的,或者我只想通过直播流URL进行直播。)谢谢

1 个回复
最合适的回答,由SO网友:Lovin Nagi 整理而成

在函数中添加以下代码。php文件,它将在常规设置选项页面中创建一个文本字段

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
然后在要获取嵌入视频的模板文件中调用以下代码
<?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 ?>

结束

相关推荐

Wp-admin登录后检查GET_POST值

我想在登录到wp admin后检查get\\u选项值。我已将日期保存在get\\u选项中。我想检查get\\u选项中的值是否与wp admin登录后的当前日期相同。如果相同,那么我想显示管理员通知。每次我登录到wp admin时,它都会检查get\\u选项值。这是我的管理员通知代码。I have no idea how can i check the get_option value after wp-admin login. function sample_admin_notice__success()