Last active
August 24, 2021 14:01
-
-
Save taotiwordpress/76bbda6cfaed8edaec2436dd2f27763d to your computer and use it in GitHub Desktop.
API control for rebuilding WordPress admin-editor metaboxes #wordpress #metabox #tags #category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
### This file provides an API for rebuilding metaboxes | |
### See https://rudrastyh.com/wordpress/tag-metabox-like-categories.html | |
### for inspiration on content. | |
### See wp-admin/meta-boxes.php for source-code. | |
// add_action( 'admin_menu', 'taoti_post_taxonomy_metabox_remove' ); | |
// add_action( 'admin_menu', 'taoti_add_autosorting_taxonomy_metabox' ); | |
/* | |
* Meta Box Removal | |
*/ | |
function taoti_post_taxonomy_metabox_remove() { | |
$id = 'writersdiv'; // Found on page source code. Will generally be 'tagsdiv-taxonomy' or 'taxonomydiv' | |
$post_type = 'resources'; // Specifies what post-type to remove it from. | |
$position = 'side'; | |
remove_meta_box( $id, $post_type, $position ); | |
} | |
/* | |
* Add 'Categories' Style Metabox | |
* | |
* See taoti_metabox_content() | |
*/ | |
function taoti_add_autosorting_taxonomy_metabox() { | |
// @todo: Add a $type, which allows for various metabox layout options. | |
$id = 'taoti_writersdiv'; // it should be unique or flat-replace $id from taoti_post_taxonomy_metabox_remove() | |
$heading = 'Writers'; // meta box heading | |
$callback = 'taoti_metabox_content'; // the name of the callback function | |
$post_type = 'resources'; | |
$position = 'side'; | |
$pri = 'default'; // priority, 'default' is good for us | |
add_meta_box( $id, $heading, $callback, $post_type, $position, $pri ); | |
} | |
/* | |
* Fill added Metabox | |
* | |
* Can also be used as a replacement within the declaration of the Taxonomy | |
* via 'register_args' => [ 'meta_box_cb' => 'taoti_metabox_content' ] | |
* | |
* See wp-admin/includes/meta-boxes.php for example code from Core. | |
*/ | |
function taoti_metabox_content( $post, $box ) { | |
// Add code here. | |
} | |
/* | |
* Override WP Term Order | |
* | |
* WP Term Order does not allow for sorting by 'name' and must be overridden. | |
* | |
* See https://github.com/stuttter/wp-term-order/issues/11#issuecomment-507739369 | |
* for solution source. | |
*/ | |
function my_theme_get_terms( $args = null ) { | |
add_filter( 'get_terms_orderby', 'my_theme_get_terms_order_fix', 20, 2 ); | |
$terms = get_terms( $args ); | |
remove_filter( 'get_terms_orderby', 'my_theme_get_terms_order_fix', 20 ); | |
return $terms; | |
} | |
function my_theme_get_terms_order_fix( $orderby = 'name', $args = array() ) { | |
if ( 'name' == $args['orderby'] && 't.name' !== $orderby ) { | |
$orderby = 't.name'; | |
} | |
return $orderby; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment