How to add a Blocksy Content Block To The WordPress Admin Dashboard

I wanted to find a way on how to add a custom widget to the WordPress admin dashboard using Gutenberg blocks and the Blocksy theme.

Within Blocksy Pro, it has a featured called Content Blocks, a very handy system to create hooks, popups and page / post templates to appear throughout the site in the themes many hook locations.

So I went about figuring out a way to do it and now you can add it too.

Now fair warning, it is not perfect and needs some work for styling etc, but it is a step in the right direction.

I am using the WP Stackable blocks plugin but you could use any standard Gutenberg block (you may need to test them though).

Following is the code to add to your Blocksy child theme functions.php file:

Make sure to change the last line of the code to your content block shortcode.

/*
Dashboard Widget
*/

// Prevent users from putting anything to first column
// as widget order set by user overrides everything
add_filter(
    'get_user_option_meta-box-order_dashboard',
    function($result, $option, $user) {
        // Force custom widget to 1st column
        if ( ! empty( $result['normal'] ) ) {
            $result['normal'] = array('dashboard_widget_example');
        }
        return $result;
    },
    10,
    3
);
// Style dashboard widget columns
add_action(
    'admin_head',
    function() {
        echo "<style type='text/css'>
            #dashboard-widgets .postbox-container {width: 33.333333%;}
            #dashboard-widgets #postbox-container-1 {width: 100%;}
        </style>";
    }
);
// Dashboard widget reordering
add_action( 'wp_dashboard_setup', function(){

  global $wp_meta_boxes;

    // Move all dashboard wigets from 1st to 2nd column
    if ( ! empty( $wp_meta_boxes['dashboard']['normal'] ) ) {
        if ( isset($wp_meta_boxes['dashboard']['side']) ) {
            $wp_meta_boxes['dashboard']['side'] = array_merge_recursive(
                $wp_meta_boxes['dashboard']['side'],
                $wp_meta_boxes['dashboard']['normal']
            );
        } else {
            $wp_meta_boxes['dashboard']['side'] = $wp_meta_boxes['dashboard']['normal'];
        }
        unset( $wp_meta_boxes['dashboard']['normal'] );
    }

  // Add custom dashbboard widget.
  add_meta_box( 'dashboard_widget_example',
    __( 'Welcome To Your Website', 'example-text-domain', ),
    'render_example_widget',
    'dashboard',
    'normal',
    'default'
  );

} );

function render_example_widget() {
?>
<?php echo do_shortcode('[blocksy-content-block id="227"]'); ?> // Change to your Blocksy Content Block Shortcode
<?php
}