如何在WordPress仪表板小部件中向函数添加php值?

问题描述 投票:0回答:1

我正在为wordpress制作插件-对此我还很陌生-但我正在尝试制作显示来自不同来源(CNN,The Onion,MOZ等)的供稿的信息中心小部件。我已经设置了插件我从https://jeremyhixon.com/tool/wordpress-option-page-generator/获得了自定义选项页面,一切似乎都正常运行。我的问题是-如何从选项页面中获取值并将其添加到我的小部件代码中。以下是具体选项:

进纸标签资讯提供网址文章数

    /* 
 * 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 */

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

php wordpress wordpress-plugin-creation
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
© www.soinside.com 2019 - 2024. All rights reserved.