MainWP Community

Create custom widget

Hello,

I need to create a custom widget for my child sites. The MainWp plugin seems to include widgets with the MainWP_UI class inside the page-mainwp-overview.php file.

In another post, I read that we can use the mainwp_before_overview_widgets() hook to include custom code.

Do I use this hook to call my custom widget class ? Do I use the MainWP_UI class inside this hook ? It’s not clear for me.

Thanks for your help and examples.

Hi @Cyril, do you need widget only on the Individual site Overview or on the Global overview page too?

Hi Bogdan,

to begin, only on the individual site overview.

Thanks.

One more thing I need to ask, are you building a custom extension or you just want to build a custom snippet to insert custom widget on the Overview page?

I need to add aditionnal datas for the child sites (on the overview page). So, I want to create new widgets in the overview page of the child sites.

For the moment I can add new columns in the sites table. It’ not very complicated. But, for create new widgets, with specific datas, I don’t see how to proceed.

So, if I use a custom snippet, maybe I could create these new widgets.

Can you give us, a simply process for this ?

  1. Use the hook_before_overview_page() function
  2. Inside use MainWp_Ui class to declare new widget…

Thanks a lot !

Hi Cyril, thanks for getting back to me.

Can you please try with something like this:

add_filter( 'mainwp_getmetaboxes', 'mycustom_widget_get_metaboxes', 10, 1 );
function mycustom_widget_get_metaboxes( $metaboxes ) {
	if ( ! isset( $_GET['dashboard'] ) ) {
		return $metaboxes;
	}
	if ( ! is_array( $metaboxes ) ) {
			$metaboxes = array();
	}
	$metaboxes[] = array(
		'id'            => 'my-metabox-widget',
		'plugin'        => __FILE__,
		'custom'        => true,
		'metabox_title' => __( 'My Metabox' ),
		'callback'      => 'render_mycustom_widget_get_metaboxes',
	);
	return $metaboxes;
}
function render_mycustom_widget_get_metaboxes() {
	?>
		<div class="ui grid">
			<div class="twelve wide column">
				<h3 class="ui header handle-drag">
			<?php esc_html_e( 'My Metabox' ); ?>
					<div class="sub header"><?php esc_html_e( 'My Metabox' ); ?></div>
				</h3>
			</div>
			<div class="four wide column right aligned"></div>
		</div>
		<?php
}

Hi Bogdan !

thank for this example.
To test, I use directly your code. But I don’t see any new widget in the site dashboard (after having clear the cache). The first function seems to be correct. But, the callback doesn’t seem to be calling. I don’t see a widget with the ‘My Metabox’ title.

Any idea ? Thanks.

Thanks for getting back to me. Just to be sure, does it show on the main Overview page?

No. No element/widget seems to appear on the main overview page or in site overview page.
If I insert a ‘print_r’ inside the function …( $metaboxes ) I see an element. A ‘print_r’ inside the callback function don’t render anything.

Can you try this version:

add_filter( 'mainwp_getmetaboxes', 'mycustom_widget_get_metaboxes', 10, 1 );
function mycustom_widget_get_metaboxes( $metaboxes ) {
	if ( ! is_array( $metaboxes ) ) {
			$metaboxes = array();
	}
	$metaboxes[] = array(
		'id'            => 'my-metabox-widget',
		'plugin'        => __FILE__,
		'custom'        => true,
		'metabox_title' => __( 'My Metabox' ),
		'callback'      => 'render_mycustom_widget_get_metaboxes',
	);
	return $metaboxes;
}
function render_mycustom_widget_get_metaboxes() {
	?>
		<div class="ui grid">
			<div class="twelve wide column">
				<h3 class="ui header handle-drag">
			<?php esc_html_e( 'My Metabox' ); ?>
					<div class="sub header"><?php esc_html_e( 'My Metabox' ); ?></div>
				</h3>
			</div>
			<div class="four wide column right aligned"></div>
		</div>
		<?php
}
1 Like

Please try in the MainWP Custom Dashboard Extension, in the PHP Section

I tried in my setup, and it worked:

Hi @Bogdan,

I understand the problem. I don’t use the MainWP Custom Dashboard Extension and develop a custom plugin. In my custom plugin, your code has no effect.

How can I use your code inside a custom plugin ?

Thanks.

Hi Cyril, can you please check code in this example?

Hi Bogdan,

thanks for this code !! Now, I can see a custom widget on all overview pages.

In the mainwp_before_overview_widgets() hook, i call the widget class to render it.

But, the widget is rendered at the top of the widgets. WIth the other mainwp_after_overview_widgets() hook, the widget is rendered at the end.

Is there a possibility to render my custom widget among the other ? (with another hook)

Thanks.

Hi Cyril,

Thanks for getting back to me.

can you please share your code you have so far so I can check it and see what can be readjusted?

Hi Bogdan,

for the moment I use a simple hook for this :

function myplugin_before_overview_widgets() {
  require_once '..mywidget-class.php';
  $widget = new MainWP_MyWidget();
  print $widget->render_widget_metaboxes();
}
add_action( 'mainwp_after_overview_widgets', 'myplugin_before_overview_widgets' );