I need to create a new Taxonomy (nbateam), add extra fields to this taxonomy ('code', 'color', ...) AND register them when the plugin initiate.
For now I'm able to do everything except register values for the extra fields I create.
code I use :
<?php
// Taxonomy part
add_action( 'init', 'create_nbateam_taxonomy', 0 );
// Extra fields
add_action( 'nbateam_add_form_fields', 'add_extra_fields_to_nbateam_taxonomy' );
add_action( 'create_nbateam', 'save_extra_fields_to_nbateam_taxonomy' );
add_action( 'edit_nbateam', 'save_extra_fields_to_nbateam_taxonomy' );
add_action( 'nbateam_edit_form_fields', 'edit_extra_fields_to_nbateam_taxonomy' );
// Register terms
add_action( 'init', 'register_new_terms_to_nbateam_taxonomy' );
function create_nbateam_taxonomy() {
$labels = array(
'name' => _x( 'Nba Teams', 'Taxonomy General Name', 'nba-teams' ),
'singular_name' => _x( 'Nba Team', 'Taxonomy Singular Name', 'nba-teams' ),
'menu_name' => __( 'Nba Teams', 'nba-teams' ),
'all_items' => __( 'All Nba Teams', 'nba-teams' ),
'parent_item' => __( 'Parent Item', 'nba-teams' ),
'parent_item_colon' => __( 'Parent Item:', 'nba-teams' ),
'new_item_name' => __( 'New Item Name', 'nba-teams' ),
'add_new_item' => __( 'Add New Item', 'nba-teams' ),
'edit_item' => __( 'Edit Item', 'nba-teams' ),
'update_item' => __( 'Update Item', 'nba-teams' ),
'view_item' => __( 'View Item', 'nba-teams' ),
'separate_items_with_commas' => __( 'Separate Nba Teams with commas', 'nba-teams' ),
'add_or_remove_items' => __( 'Add or remove Nba Teams', 'nba-teams' ),
'choose_from_most_used' => __( 'Choose from the most used Nba Teams', 'nba-teams' ),
'popular_items' => __( 'Popular Nba Teams', 'nba-teams' ),
'search_items' => __( 'Search Nba Teams', 'nba-teams' ),
'not_found' => __( 'Not Found', 'nba-teams' ),
'no_terms' => __( 'No Nba Teams', 'nba-teams' ),
'items_list' => __( 'Nba Teams list', 'nba-teams' ),
'items_list_navigation' => __( 'Nba Teams list navigation', 'nba-teams' ),
);
$rewrite = array(
'slug' => 'nba-teams',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
'show_in_rest' => true,
);
register_taxonomy( 'nbateam', array( 'post' ), $args );
// Flush rewrite rules after create new taxonomy
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_extra_fields_to_nbateam_taxonomy( $taxonomy_name ) {
?>
<div class="form-field">
<label for="team-code">Code</label>
<input type="text" name="team-code" id="team-code"/>
<p>Fill the Team Code ( ex: Hawks -> ATL ) </p>
</div>
<div class="form-field">
<label for="team-nba-id">Nba Id</label>
<input type="text" name="team-nba-id" id="team-nba-id"/>
<p>Fill the Team Nba Id for Api ( ex: Hawks -> 1610612737 ) </p>
</div>
<div class="form-field">
<label for="team-espn-id">Espn Id</label>
<input type="text" name="team-espn-id" id="team-espn-id"/>
<p>Fill the Team Espn Id for Api ( ex: Hawks -> 1 ) </p>
</div>
<div class="form-field">
<label for="team-color">Color</label>
<input type="text" name="team-color" id="team-color"/>
<p>Fill the Team Color Hex Code ( ex: Hawks -> #e21a37 ) </p>
</div>
<?php
}
function save_extra_fields_to_nbateam_taxonomy( $term_id ) {
//collect all term related data for this new taxonomy
$term_item = get_term( $term_id, 'nbateam' );
$term_slug = $term_item->slug;
//collect our custom fields
$term_team_code = sanitize_text_field( $_POST['team-code'] );
$term_team_nba_id = sanitize_text_field( $_POST['team-nba-id'] );
$term_team_espn_id = sanitize_text_field( $_POST['team-espn-id'] );
$term_team_color = sanitize_text_field( $_POST['team-color'] );
//save our custom fields as wp-options
update_option( 'term_team_code_' . $term_slug, $term_team_code );
update_option( 'term_team_nba_id_' . $term_slug, $term_team_nba_id );
update_option( 'term_team_espn_id_' . $term_slug, $term_team_espn_id );
update_option( 'term_team_color_' . $term_slug, $term_team_color );
}
function edit_extra_fields_to_nbateam_taxonomy( $term ) {
//collect the term slug
$term_slug = $term->slug;
//collect our saved term field information
$term_team_code = get_option( 'term_team_code_' . $term_slug );
$term_team_nba_id = get_option( 'term_team_nba_id_' . $term_slug );
$term_team_espn_id = get_option( 'term_team_espn_id_' . $term_slug );
$term_team_color = get_option( 'term_team_color_' . $term_slug );
//output our additional fields
?>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-code">Code</label>
</th>
<td>
<input type="text" name="team-code" id="team-code" value="<?php echo $term_team_code; ?>"/>
<p class="description">Fill the Team Code ( ex: Hawks -> ATL ) </p>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-nba-id">Nba Id</label>
</th>
<td>
<input type="text" name="team-nba-id" id="team-nba-id" value="<?php echo $term_team_nba_id; ?>"/>
<p class="description">Fill the Team Nba Id for Api ( ex: Hawks -> 1610612737 ) </p>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-espn-id">Espn Id</label>
</th>
<td>
<input type="text" name="team-espn-id" id="team-espn-id" value="<?php echo $term_team_espn_id; ?>"/>
<p class="description">Fill the Team Espn Id for Api ( ex: Hawks -> 1 ) </p>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-color">Color</label>
</th>
<td>
<input type="text" name="team-color" id="tteam-color" value="<?php echo $term_team_color; ?>"/>
<p class="description">Fill the Team Color Hex Code ( ex: Hawks -> #e21a37 ) </p>
</td>
</tr>
<?php
}
function register_new_terms_to_nbateam_taxonomy() {
$this->taxonomy = 'nbateam';
$this->terms = array (
'0' => array (
'name' => 'Atlanta Hawks',
'slug' => 'atlanta-hawks',
'description' => 'Atlanta Hawks Page'
'code' => 'ATL',
'nba-id' => '1610612737',
'espn-id' => '1',
'color' => '#e21a37',
),
'1' => array (
'name' => 'Boston Celtics',
'slug' => 'boston-celtics',
'description' => 'Boston Celtics Page'
'code' => 'BOS',
'nba-id' => '1610612738',
'espn-id' => '2',
'color' => '#00611b',
),
);
foreach ( $this->terms as $term_key=>$term) {
// insert new terms in taxonomy
wp_insert_term(
$term['name'],
$this->taxonomy,
array(
'slug' => $term['slug'],
'description' => $term['description'],
// Doesn't work !!
'code' => $term['code'],
'nba-id' => $term['nba-id'],
'espn-id' => $term['espn-id'],
'color' => $term['color'],
)
);
unset( $term );
}
}
The last part of the register_new_terms_to_nbateam_taxonomy()
function works only for usual terms values ( 'name', 'slug', 'description' ) but not for extra fields I add.
I need to create a new Taxonomy (nbateam), add extra fields to this taxonomy ('code', 'color', ...) AND register them when the plugin initiate.
For now I'm able to do everything except register values for the extra fields I create.
code I use :
<?php
// Taxonomy part
add_action( 'init', 'create_nbateam_taxonomy', 0 );
// Extra fields
add_action( 'nbateam_add_form_fields', 'add_extra_fields_to_nbateam_taxonomy' );
add_action( 'create_nbateam', 'save_extra_fields_to_nbateam_taxonomy' );
add_action( 'edit_nbateam', 'save_extra_fields_to_nbateam_taxonomy' );
add_action( 'nbateam_edit_form_fields', 'edit_extra_fields_to_nbateam_taxonomy' );
// Register terms
add_action( 'init', 'register_new_terms_to_nbateam_taxonomy' );
function create_nbateam_taxonomy() {
$labels = array(
'name' => _x( 'Nba Teams', 'Taxonomy General Name', 'nba-teams' ),
'singular_name' => _x( 'Nba Team', 'Taxonomy Singular Name', 'nba-teams' ),
'menu_name' => __( 'Nba Teams', 'nba-teams' ),
'all_items' => __( 'All Nba Teams', 'nba-teams' ),
'parent_item' => __( 'Parent Item', 'nba-teams' ),
'parent_item_colon' => __( 'Parent Item:', 'nba-teams' ),
'new_item_name' => __( 'New Item Name', 'nba-teams' ),
'add_new_item' => __( 'Add New Item', 'nba-teams' ),
'edit_item' => __( 'Edit Item', 'nba-teams' ),
'update_item' => __( 'Update Item', 'nba-teams' ),
'view_item' => __( 'View Item', 'nba-teams' ),
'separate_items_with_commas' => __( 'Separate Nba Teams with commas', 'nba-teams' ),
'add_or_remove_items' => __( 'Add or remove Nba Teams', 'nba-teams' ),
'choose_from_most_used' => __( 'Choose from the most used Nba Teams', 'nba-teams' ),
'popular_items' => __( 'Popular Nba Teams', 'nba-teams' ),
'search_items' => __( 'Search Nba Teams', 'nba-teams' ),
'not_found' => __( 'Not Found', 'nba-teams' ),
'no_terms' => __( 'No Nba Teams', 'nba-teams' ),
'items_list' => __( 'Nba Teams list', 'nba-teams' ),
'items_list_navigation' => __( 'Nba Teams list navigation', 'nba-teams' ),
);
$rewrite = array(
'slug' => 'nba-teams',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
'show_in_rest' => true,
);
register_taxonomy( 'nbateam', array( 'post' ), $args );
// Flush rewrite rules after create new taxonomy
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_extra_fields_to_nbateam_taxonomy( $taxonomy_name ) {
?>
<div class="form-field">
<label for="team-code">Code</label>
<input type="text" name="team-code" id="team-code"/>
<p>Fill the Team Code ( ex: Hawks -> ATL ) </p>
</div>
<div class="form-field">
<label for="team-nba-id">Nba Id</label>
<input type="text" name="team-nba-id" id="team-nba-id"/>
<p>Fill the Team Nba Id for Api ( ex: Hawks -> 1610612737 ) </p>
</div>
<div class="form-field">
<label for="team-espn-id">Espn Id</label>
<input type="text" name="team-espn-id" id="team-espn-id"/>
<p>Fill the Team Espn Id for Api ( ex: Hawks -> 1 ) </p>
</div>
<div class="form-field">
<label for="team-color">Color</label>
<input type="text" name="team-color" id="team-color"/>
<p>Fill the Team Color Hex Code ( ex: Hawks -> #e21a37 ) </p>
</div>
<?php
}
function save_extra_fields_to_nbateam_taxonomy( $term_id ) {
//collect all term related data for this new taxonomy
$term_item = get_term( $term_id, 'nbateam' );
$term_slug = $term_item->slug;
//collect our custom fields
$term_team_code = sanitize_text_field( $_POST['team-code'] );
$term_team_nba_id = sanitize_text_field( $_POST['team-nba-id'] );
$term_team_espn_id = sanitize_text_field( $_POST['team-espn-id'] );
$term_team_color = sanitize_text_field( $_POST['team-color'] );
//save our custom fields as wp-options
update_option( 'term_team_code_' . $term_slug, $term_team_code );
update_option( 'term_team_nba_id_' . $term_slug, $term_team_nba_id );
update_option( 'term_team_espn_id_' . $term_slug, $term_team_espn_id );
update_option( 'term_team_color_' . $term_slug, $term_team_color );
}
function edit_extra_fields_to_nbateam_taxonomy( $term ) {
//collect the term slug
$term_slug = $term->slug;
//collect our saved term field information
$term_team_code = get_option( 'term_team_code_' . $term_slug );
$term_team_nba_id = get_option( 'term_team_nba_id_' . $term_slug );
$term_team_espn_id = get_option( 'term_team_espn_id_' . $term_slug );
$term_team_color = get_option( 'term_team_color_' . $term_slug );
//output our additional fields
?>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-code">Code</label>
</th>
<td>
<input type="text" name="team-code" id="team-code" value="<?php echo $term_team_code; ?>"/>
<p class="description">Fill the Team Code ( ex: Hawks -> ATL ) </p>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-nba-id">Nba Id</label>
</th>
<td>
<input type="text" name="team-nba-id" id="team-nba-id" value="<?php echo $term_team_nba_id; ?>"/>
<p class="description">Fill the Team Nba Id for Api ( ex: Hawks -> 1610612737 ) </p>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-espn-id">Espn Id</label>
</th>
<td>
<input type="text" name="team-espn-id" id="team-espn-id" value="<?php echo $term_team_espn_id; ?>"/>
<p class="description">Fill the Team Espn Id for Api ( ex: Hawks -> 1 ) </p>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="team-color">Color</label>
</th>
<td>
<input type="text" name="team-color" id="tteam-color" value="<?php echo $term_team_color; ?>"/>
<p class="description">Fill the Team Color Hex Code ( ex: Hawks -> #e21a37 ) </p>
</td>
</tr>
<?php
}
function register_new_terms_to_nbateam_taxonomy() {
$this->taxonomy = 'nbateam';
$this->terms = array (
'0' => array (
'name' => 'Atlanta Hawks',
'slug' => 'atlanta-hawks',
'description' => 'Atlanta Hawks Page'
'code' => 'ATL',
'nba-id' => '1610612737',
'espn-id' => '1',
'color' => '#e21a37',
),
'1' => array (
'name' => 'Boston Celtics',
'slug' => 'boston-celtics',
'description' => 'Boston Celtics Page'
'code' => 'BOS',
'nba-id' => '1610612738',
'espn-id' => '2',
'color' => '#00611b',
),
);
foreach ( $this->terms as $term_key=>$term) {
// insert new terms in taxonomy
wp_insert_term(
$term['name'],
$this->taxonomy,
array(
'slug' => $term['slug'],
'description' => $term['description'],
// Doesn't work !!
'code' => $term['code'],
'nba-id' => $term['nba-id'],
'espn-id' => $term['espn-id'],
'color' => $term['color'],
)
);
unset( $term );
}
}
The last part of the register_new_terms_to_nbateam_taxonomy()
function works only for usual terms values ( 'name', 'slug', 'description' ) but not for extra fields I add.
1 Answer
Reset to default 0In your cases "wp_insert_term" does not save extra custom fields.
Please try with below code :
foreach ( $this->terms as $term_key=>$term) {
// insert new terms in taxonomy
wp_insert_term(
$term['name'],
$this->taxonomy,
array(
'slug' => $term['slug'],
'description' => $term['description']
)
);
update_option( 'term_team_code_' . $term['slug'], $term['code'] );
update_option( 'term_team_nba_id_' . $term['slug'], $term['nba-id'] );
update_option( 'term_team_espn_id_' . $term['slug'], $term['espn-id'] );
update_option( 'term_team_color_' . $term['slug'], $term['color'] );
unset( $term );
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745415921a4626747.html
评论列表(0条)