最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Adding widget to dashboard with wp_dashboard_setup not working

programmeradmin6浏览0评论

I just separated my functions.php file into smaller files and the required each of those inside functions.php

One of those files is 'dashboard.php' where I add a widget to change a notice in my shop.

<?php
// Store Notice Dashboard Widget
add_action('wp_dashboard_setup', 'widgets_definitions');

function widgets_definitions() {
    error_log("widget_defs");
    wp_add_dashboard_widget(
        'update_store_notice_widget', // widget slug
        'Update Store Notice',              // Title
        'render_store_notice_widget', // Display function
        'handle_store_notice_widget'  // Handle configure function
    );
    error_log("widget_defs 2");

}

function render_store_notice_widget() {
    error_log("render");

    $notice_text = get_option('woocommerce_demo_store_notice');
    error_log($notice_text);

    if (!isset($notice_text)) {
        $notice_text = "";
    }

    echo "<p><strong>Current Store Notice:</strong></p>";
    echo "<p>" . $notice_text . "</p>";
}

function handle_store_notice_widget() {
    error_log("handler");

    $notice_text = get_option('woocommerce_demo_store_notice');

    if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['woocommerce_demo_store_notice'])) {
        error_log("POST");
        //minor validation
        $notice_text = wp_kses($_POST['woocommerce_demo_store_notice']);
        error_log($notice_text);
        //save update
        update_option('woocommerce_demo_store_notice', $notice_text);
    }

    if (!isset($notice_text)) {
        $notice_text = "";
    }
    error_log($notice_text);
    echo "<p><strong>New Store Notice</strong></p>";
    echo "<textarea name='woocommerce_demo_store_notice'>" . $notice_text . "</textarea>";

}

?>

For some reason it just doesn't show up in my dashboard, no errors.

I added error_log() before and after the require and at the end of the 'functions.php' file and I can see all 3 of them in the log file, but none of the ones on 'dashboard.php'.

Any idea what's going on?


Edit

<?php

add_action('wp_enqueue_scripts', 'supro_child_enqueue_scripts', 20);
function supro_child_enqueue_scripts() {
  wp_enqueue_style('supro-child-style', get_stylesheet_uri());
  if (is_rtl()) {
    wp_enqueue_style('supro-rtl', get_template_directory_uri() . '/rtl.css', array(), '20180727');
  }
}

require "includes/dashboard.php";

require 'file';

require 'file';

require 'file';

[...]

That's my functions.php file, the only thing omitted are all the other requires.

I'll change that to wp_kses_post(), thanks. @DaveRomsey

I just separated my functions.php file into smaller files and the required each of those inside functions.php

One of those files is 'dashboard.php' where I add a widget to change a notice in my shop.

<?php
// Store Notice Dashboard Widget
add_action('wp_dashboard_setup', 'widgets_definitions');

function widgets_definitions() {
    error_log("widget_defs");
    wp_add_dashboard_widget(
        'update_store_notice_widget', // widget slug
        'Update Store Notice',              // Title
        'render_store_notice_widget', // Display function
        'handle_store_notice_widget'  // Handle configure function
    );
    error_log("widget_defs 2");

}

function render_store_notice_widget() {
    error_log("render");

    $notice_text = get_option('woocommerce_demo_store_notice');
    error_log($notice_text);

    if (!isset($notice_text)) {
        $notice_text = "";
    }

    echo "<p><strong>Current Store Notice:</strong></p>";
    echo "<p>" . $notice_text . "</p>";
}

function handle_store_notice_widget() {
    error_log("handler");

    $notice_text = get_option('woocommerce_demo_store_notice');

    if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['woocommerce_demo_store_notice'])) {
        error_log("POST");
        //minor validation
        $notice_text = wp_kses($_POST['woocommerce_demo_store_notice']);
        error_log($notice_text);
        //save update
        update_option('woocommerce_demo_store_notice', $notice_text);
    }

    if (!isset($notice_text)) {
        $notice_text = "";
    }
    error_log($notice_text);
    echo "<p><strong>New Store Notice</strong></p>";
    echo "<textarea name='woocommerce_demo_store_notice'>" . $notice_text . "</textarea>";

}

?>

For some reason it just doesn't show up in my dashboard, no errors.

I added error_log() before and after the require and at the end of the 'functions.php' file and I can see all 3 of them in the log file, but none of the ones on 'dashboard.php'.

Any idea what's going on?


Edit

<?php

add_action('wp_enqueue_scripts', 'supro_child_enqueue_scripts', 20);
function supro_child_enqueue_scripts() {
  wp_enqueue_style('supro-child-style', get_stylesheet_uri());
  if (is_rtl()) {
    wp_enqueue_style('supro-rtl', get_template_directory_uri() . '/rtl.css', array(), '20180727');
  }
}

require "includes/dashboard.php";

require 'file';

require 'file';

require 'file';

[...]

That's my functions.php file, the only thing omitted are all the other requires.

I'll change that to wp_kses_post(), thanks. @DaveRomsey

Share Improve this question edited Dec 27, 2019 at 18:43 Daviid asked Dec 26, 2019 at 23:38 DaviidDaviid 1631 silver badge9 bronze badges 3
  • I tested the code, and the widget does show up for me. Perhaps the problem is with the way you are including the file? SIde note, you might want to change the call to wp_kses() to wp_kses_post() or add the missing parameters to wp_kses() to avoid an error when saving. – Dave Romsey Commented Dec 27, 2019 at 18:05
  • php <?php add_action('wp_enqueue_scripts', 'supro_child_enqueue_scripts', 20); function supro_child_enqueue_scripts() { wp_enqueue_style('supro-child-style', get_stylesheet_uri()); if (is_rtl()) { wp_enqueue_style('supro-rtl', get_template_directory_uri() . '/rtl.css', array(), '20180727'); } } require "includes/dashboard.php"; require 'file'; require 'file'; require 'file'; [...] That's my functions.php file, the only thing omitted are all the other requires. I'll change that to wp_kses_post(), thanks. – Daviid Commented Dec 27, 2019 at 18:41
  • @DaveRomsey edited the question so it's formatted better that the previous comment. – Daviid Commented Dec 27, 2019 at 18:44
Add a comment  | 

1 Answer 1

Reset to default 1

Use this code to require the file from your theme's functions.php file:

require get_template_directory() . 'includes/dashboard.php';

For the sake of completeness (and sanity) my dashboard.php file lives here:

my-theme/includes/dashboard.php

I took a closer look at the original issue, and I think what's happening is that the require statement is silently failing because the wp-admin/includes/dashboard.php file is actually what's being included.

To test this, I added the following code to my theme's functions.php file:

// require 'includes/dashboard.php';
$file_contents = file_get_contents( 'includes/dashboard.php' );
error_log( var_export( $file_contents, true ) );

And the result from PHP's error log shows that we are indeed including the wrong file.

[27-Dec-2019 20:26:27 UTC] '<?php
/**
 * WordPress Dashboard Widget Administration Screen API
 *
 * @package WordPress
 * @subpackage Administration
 */

/**
 * Registers dashboard widgets.
 *
 * Handles POST data, sets up filters.
 *
 * @since 2.5.0
 *
 * @global array $wp_registered_widgets
 * @global array $wp_registered_widget_controls
 * @global array $wp_dashboard_control_callbacks
 */
function wp_dashboard_setup() {
    global $wp_registered_widgets, $wp_registered_widget_controls, $wp_dashboard_control_callbacks;
    $wp_dashboard_control_callbacks = array();

...
发布评论

评论列表(0)

  1. 暂无评论