Select a blog in the meta box

The following snippet shows how to select one or more blogs in the Broadcast meta box using code.


/**
	@brief		Select a blog.
	@since		2019-05-09 21:23:32
**/
function my_threewp_broadcast_prepare_meta_box( $action )
{
	$mbd = $action->meta_box_data;

	// Retrieve the form for the meta_box_data.
	if ( ! isset( $mbd->form ) )
		return;
	$form = $mbd->form;

	// Retrieve the blogs fieldset (=checkboxes).
	$blogs = $form->checkboxes( 'blogs' );

	if ( ! $blogs )
		return;

	// The checkboxes input works a bit strangely, allowing us to append multiple values.

	// Select blog 12
	$blogs->value( 12 );

	// Select blog 13
	$blogs->value( 13 );

	// If you want to go through each checkbox input separetely...
	/**
	$blog_inputs = $blogs->inputs;
	foreach( $blog_inputs as $blog_input )
	{
		// The name of the input is blog_XX
		$blog_id = $blog_input->get_name();
		$blog_id = str_replace( 'blogs_', '', $blog_id );
	}
	**/
}
add_action( 'threewp_broadcast_prepare_meta_box', 'my_threewp_broadcast_prepare_meta_box' );