Adding widget to dashboard with wp_dashboard_setup not working

Question

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?

0
, , Daviid 2 years 2019-12-26T17:38:18-05:00 0 Answers 76 views 0

Leave an answer

Browse
Browse