Can I display the widget admin in the page admin?

If I understand well your want to show the widgets adding and removing interface inside a meta box.

An easy, -a little dirty- way is using an iframe:

function metaboxed_widgets_admin() {
  if ( ! current_user_can( 'edit_theme_options' ) ) return;
  add_meta_box('metaboxed_widgets', __('Widgets'), 'metaboxed_widgets_admin_cb', 'page');
}
add_action( 'add_meta_boxes', 'metaboxed_widgets_admin' );

function metaboxed_widgets_admin_cb() {
  if ( ! current_user_can( 'edit_theme_options' ) ) return;
  $format="<div style="margin:0px;padding:0px;">";
  $format .= '<iframe src="https://wordpress.stackexchange.com/questions/140885/%s" frameborder="0" %s></iframe></div>';
  // add a query arg to recognize when inside iframe, used to hide menu and admin bar
  $url = add_query_arg(
    array( 'iframe'=> wp_create_nonce('widgets') ), admin_url( 'widgets.php' )
  );
  printf( $format, $url, 'style="height:1200px;width:100%;" height="100%" width="100%"' );
}

function metaboxed_widgets_hide_stuff() {
  if ( ! is_admin() || get_current_screen()->id !== 'widgets' ) return;  
  $iframe = filter_input( INPUT_GET, 'iframe', FILTER_SANITIZE_STRING );
  if ( wp_verify_nonce( $iframe, 'widgets' ) ) {
    echo '<style>'
    . '#wpadminbar, #adminmenuback, #adminmenuwrap, #wpfooter, '
    . '#screen-meta-links, .wrap > h2 { display:none!important; }'
    . '#wpcontent { margin-left:25px!important; }'
    . '.wrap{ margin-top:0!important; }</style>';
  }
}
add_action( 'admin_head-widgets.php', 'metaboxed_widgets_hide_stuff' );

Note that only users who can see widgets will view the widget metabox, e.g. editors will not see anything, unless you give them the 'edit_theme_options' capability.

Leave a Comment