Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active August 29, 2015 13:56
Show Gist options
  • Save franz-josef-kaiser/9100450 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/9100450 to your computer and use it in GitHub Desktop.
WordPress plugin that temporarily disables the user defined order (user settings). The plugin is targeted at developers. Often it's annoying when you add a meta box to the admin UI and can't be sure if it's in the right position because you might have already moved it around and added a user setting. The plugin can be reused to temporarily overr…
<?php
namespace WCM;
/**
* Plugin Name: (WCM) Meta Box Order Debug
* Description: Temporarily overrides the MetaBox-order by dismissing the user setting.
* Author: Franz Josef Kaiser
* Author URI: https://unserkaiser.com
* Version: 1.0
* License: MIT
* © Copyright 2013 by Franz Josef Kaiser <[email protected]>
*/
defined( 'ABSPATH' ) or exit;
add_filter( 'get_user_metadata', 'WCM\disableMetaBoxOrder', 10, 4 );
/**
* @param mixed $abort Whether to return or bypass. Default: NULL
* @param int $userID
* @param string $key
* @param boolean $single
* @return array|null
*/
function disableMetaBoxOrder( $abort, $userID, $key, $single )
{
static $queryString;
NULL === $queryString
AND isset( $_SERVER['QUERY_STRING'] )
AND parse_str( $_SERVER['QUERY_STRING'], $queryString );
if (
empty( $queryString )
OR ! isset( $queryString['disable'] )
)
{
remove_filter( current_filter(), __FUNCTION__ );
return NULL;
}
// Too early: abort
if (
! function_exists( 'get_current_screen' )
OR NULL === get_current_screen()
OR ! property_exists( get_current_screen(), 'post_type' )
)
return NULL;
// Don't trigger on the wrong screens
if ( 'post' !== get_current_screen()->base )
{
remove_filter( current_filter(), __FUNCTION__ );
return NULL;
}
$target = 'meta-box-order_';
$target .= get_current_screen()->post_type;
// Trigger on default as on site specific settings
if (
$key === $target
OR $key === $GLOBALS['wpdb']->prefix.$target
)
{
// Prevent endless loop
remove_filter( current_filter(), __FUNCTION__ );
$meta = get_user_meta(
get_current_user_id(),
$target,
TRUE
);
return array( array_combine(
array_keys( $meta ),
array_fill( 0, count( $meta ), "" )
) );
}
return NULL;
}
/**
* Adds a node to the admin bar.
* The node is a link to the current page including a new key/value arg.
* The value will be checked in the user meta filter to turn debugging on/off.
* @param \WP_Admin_Bar $bar
*/
add_action( 'admin_bar_menu', function( \WP_Admin_Bar $bar )
{
if ( 'post' !== get_current_screen()->base )
return;
$link = 'add' === get_current_screen()->action
? add_query_arg(
'post_type',
get_current_screen()->post_type,
admin_url( 'post-new.php' )
)
: get_edit_post_link(
get_the_ID(),
'toolbar'
);
$link = add_query_arg(
'disable',
'metaboxorder',
$link
);
$bar->add_node( array(
'id' => 'metabox-order-disable',
'title' => '<span class="ab-icon dashicons-forms"></span><span class="ab-label">Disable</span>',
'href' => $link,
'group' => FALSE,
'meta' => array(
'html' => '',
'class' => '',
'onclick' => '',
'target' => '',
'title' => '',
'tabindex' => '',
),
) );
}, 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment