0

我正在为 wordpress 开发一个插件——我对此很陌生——但我正在尝试制作显示来自不同来源(CNN、洋葱、MOZ 等)的提要的仪表板小部件。我设置了一个插件我从https://jeremyhixon.com/tool/wordpress-option-page-generator/获得的自定义选项页面,一切似乎都正常。我的问题是 - 我如何从选项页面中获取值并将它们添加到我的小部件代码中。以下是具体选项:

Feed 标签
Feed URL
文章数

    /* 
 * Retrieve this value with:
 * $dashboard_feeds_options = get_option( 'dashboard_feeds_option_name' ); // Array of All Options
 * $feed_label_0 = $dashboard_feeds_options['feed_label_0']; // Feed Label
 * $feed_url_1 = $dashboard_feeds_options['feed_url_1']; // Feed URL
 * $number_of_articles_2 = $dashboard_feeds_options['number_of_articles_2']; // Number of Articles
 */

这是我的小部件代码示例,其中洋葱作为提要源

/** START The Onion Dashboard */

add_action( 'wp_dashboard_setup', 'onion_dashboard_add_widgets' );
function onion_dashboard_add_widgets() {
    wp_add_dashboard_widget( 'dw_dashboard_widget_onion', __( 'The Onion', 'dw' ), 'dw_dashboard_widget_onion_handler' );
}

function dw_dashboard_widget_onion_handler() {
    $feeds = array(
        array(
            'url'          => 'https://www.theonion.com/rss',
            'items'        =>15,
            'show_summary' => 1,
            'show_author'  => 0,
            'show_date'    => 1,

        ),

    );

    ob_start(); // start output buffering
    wp_dashboard_primary_output( 'dw_dashboard_widget_onion', $feeds );
    $buffer = ob_get_clean(); // get the buffer without printing the content

    // add the target attribute to the a-tag:
    $result = str_replace("<a class='rsswidget'",
                          "<a class='rsswidget' target='_blank'", $buffer);
    echo $result;
};

/** END The Onion Dashboard */

总而言之 - 我如何将在选项页面中输入的值带入此代码,以便某人轻松地将多个仪表板小部件添加到他们的网站?一如既往 - 提前感谢您帮助我解决这个问题!

4

1 回答 1

0

我会使用get_option,您可以在这里找到更多信息:https ://developer.wordpress.org/reference/functions/get_option/ 。基本的想法是这样的:

// from your plugin

$example_option_value = get_option('example_option_key');

// do something with $example_option_value
于 2020-06-09T15:02:16.340 回答