1

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

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. 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
    Dec 27, 2019 at 18:41
  • @DaveRomsey edited the question so it's formatted better that the previous comment.
    – Daviid
    Dec 27, 2019 at 18:44

1 Answer 1

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();

...
1
  • 1
    There's my widget, thank you so much. I had to change get_template_directory to get_stylesheet_directory since I'm using a child-theme and "includes/dashboard.php" to "/includes/dashboard.php" since get_stylesheet_directory doesn't end with a "/" and now it's all working.
    – Daviid
    Dec 28, 2019 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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