创建新的分类、添加额外的字段、注册术语和额外的字段值?

时间:2019-06-10 作者:cap340

我需要创建一个新的分类法(nbateam),向这个分类法添加额外的字段(\'code\',\'color\',…)AND 插件启动时注册它们。

现在,除了为我创建的额外字段注册值之外,我可以做任何事情。

我使用的代码:

<?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 );
    }
}
最后一部分register_new_terms_to_nbateam_taxonomy() 该函数仅适用于常用术语值(“name”、“slug”、“description”),但不适用于我添加的额外字段。

1 个回复
最合适的回答,由SO网友:Milan Hirpara 整理而成

在您的情况下,“wp\\u insert\\u term”不会保存额外的自定义字段。

请尝试以下代码:

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 );
}