Last active
August 29, 2015 14:08
-
-
Save tivnet/c99265e4884f7df5cb0f to your computer and use it in GitHub Desktop.
meta_box_cb bug demo
This file contains 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 | |
/** | |
* Plugin Name: TIVWP Demo 28033 | |
* Plugin URI: https://gist.github.com/tivnet/c99265e4884f7df5cb0f | |
* Description: Demonstrates WP bug <a href="https://core.trac.wordpress.org/ticket/28033">#28033</a> | |
* Version: 14.11.02 | |
* Author: TIV.NET | |
* Author URI: http://www.tiv.net | |
* License: GPL2 | |
* Copyright 2014 Gregory Karpinsky (tiv.net), All Rights Reserved | |
*/ | |
add_action( 'init', | |
function () { | |
register_post_type( 'test_cpt', | |
[ | |
'labels' => [ | |
'name' => 'Test CPT' | |
], | |
'public' => true, | |
] | |
); | |
register_taxonomy( 'test_tax', 'test_cpt', | |
[ | |
'labels' => [ | |
'name' => 'Test TAX' | |
], | |
/** | |
* This causes the bug | |
*/ | |
'meta_box_cb' => 'post_categories_meta_box', | |
/** | |
* This is a possible workaround - uncomment to see | |
*/ | |
// 'meta_box_cb' => 'single_taxonomy_select_meta_box', | |
] | |
); | |
} | |
); | |
/** | |
* Meta box to select only one taxonomy value | |
* | |
* @param WP_Post $post | |
* @param array $box | |
*/ | |
function single_taxonomy_select_meta_box( WP_Post $post, Array $box ) { | |
$taxonomy = $box['args']['taxonomy']; | |
$term_name_of_the_current_post = wp_get_object_terms( $post->ID, $taxonomy, [ 'fields' => 'names' ] )[0]; | |
$all_term_names = get_terms( $taxonomy, [ 'fields' => 'names', 'hide_empty' => false ] ); | |
?> | |
<label> | |
<?php esc_html_e( 'Please choose:' ); ?> | |
<select name="tax_input[<?php echo esc_attr( $taxonomy ); ?>]"> | |
<?php foreach ( $all_term_names as $term_name ) : ?> | |
<option <?php selected( $term_name_of_the_current_post, $term_name ); ?>> | |
<?php echo esc_html( $term_name ); ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</label> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment