0

I recently created a widget in my functions.php with following code :

register_sidebar(
    array(
        'name'          => __( 'Informations importantes', 'twentynineteen' ),
        'id'            => 'informations-widget',
        'description'   => __( 'Ajouter ici le slider vous permettant de mettre en avant des informations complémentaires.', 'twentynineteen' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    )
);

No problem at this point, it appears normally on the Appearance -> Widgets section

Well now, i have an issue when i try to make it appear on my dashboard i can add a custom widget without any problem, but make an existing appear on dashboard it is a little more difficult. I tried with this code ( with 'informations widget' as the idea of the original widget :

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {

add_meta_box('informations-widget', 'Dashboard Widget Title', 

'custom_dashboard_infos', 'dashboard', 'side', 'high');
    }

function custom_dashboard_infos() {
echo '<p>Une infos importantes';
}

Thanks in advance for the help !

1 Answer 1

0

Widgets aren't metaboxes, they can't be added to the dashboard directly.

wp_add_dashboard_widget expects a callable function, not a sidebar ID. You can't plug a widget or a widget area/sidebar into a function like that.

Instead, you would need to add a metabox, then, in its callback display what you wanted to display, e.g.

add_action('wp_dashboard_setup', 'custom_dashboard_widgets'); 
function custom_dashboard_widgets() {
    global $wp_meta_boxes;
    wp_add_dashboard_widget('custom_dashboard_box', 'Example', 'custom_dashboard_box');
}
function custom_dashboard_box() {
    // Widget Content Here
    echo '<p>Hello World</p>';
}

You could then call the_widget inside the callback to display an individual widget with predefined parameters.

Additionally, your questions code registers a sidebar, not a widget. If it's the displaying of a widget area in the dashboard that you want, that is not what you asked. Please update your question accordingly.

4
  • Still not working with : function custom_dashboard_widgets() { global $wp_meta_boxes; wp_add_dashboard_widget('custom_dashboard_box', 'Example', 'custom_dashboard_box'); } function custom_dashboard_box() { // Widget Content Here $args = array( 'before_widget' => '<div class="widget %s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widget-title">', 'after_title' => '</h2>' ); $instance = array( 'title' => 'Title', 'text' => 'Text' ); the_widget( 'informations-widget', $instance, $args ); } Commented Jul 11, 2019 at 11:46
  • When you say it's not working can you be more specific? informations-widget is not a Widget, it's a sidebar, you can't plug it into the_widget
    – Tom J Nowell
    Commented Jul 11, 2019 at 16:24
  • Well, the metabox it's showing but nothing appears inside, yes i figured that out but even when i use the id of the widget nothing comes up in the metabox. When i try to use WP_Widget_Text, nothing appears too Commented Jul 12, 2019 at 6:32
  • Widgets and sidebars don't go in meta boxes. The content of the meta box needs to go in your custom_dashboard_infos() function. Commented Jul 12, 2019 at 9:41

Your Answer

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

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