Add custom wordpress taxonomy field

Asked

Viewed 297 times

1

I am giving maintenance on a Wordpress site on which there is a page called "Services" and so there is also the "Service Category", on a certain page of the site, these services are listed within each category.

I am trying to add a new field for "Service Category", the "sort", so that the customer can choose which order will be listed the categories and their services in the format 0 for first, 1 for second and so on.

Well, I saw that a Taxonomy was created for such page but I can not make display the desired additional field. Follow the taxonomy code:

function taxonomiaCategoriaServico() {

$rotulosCategoriaServico = array(
                                    'name'              => 'Categorias de Servico',
                                    'singular_name'     => 'Categoria de Servico',
                                    'search_items'      => 'Buscar categorias de Servico',
                                    'all_items'         => 'Todas categorias de Servico',
                                    'parent_item'       => 'Categoria de Servico pai',
                                    'parent_item_colon' => 'Categoria de Servico pai:',
                                    'edit_item'         => 'Editar categoria de Servico',
                                    'update_item'       => 'Atualizar categoria de Servico',
                                    'add_new_item'      => 'Nova categoria de Servico',
                                    'new_item_name'     => 'Nova categoria',
                                    'menu_name'         => 'Categorias de Servico',
                                );

$argsCategoriaServico       = array(
                                    'hierarchical'      => true,
                                    'labels'            => $rotulosCategoriaServico,
                                    'show_ui'           => true,
                                    'show_admin_column' => true,
                                    'query_var'         => true,
                                    'rewrite'           => array( 'slug' => 'categoria-servico' ),
                                );

register_taxonomy( 'categoriaServico', array( 'servico' ), $argsCategoriaServico );

}

I’ve tried to put everything inside the label, I’ve seen the documentation on Codex on the Wordpress page and also failed.

Follow as I call in the template to show the information.

<?php
                    $conteudoTaxonomias = get_terms( 'categoriaServico', '' );
                    foreach ($conteudoTaxonomias as $conteudoTaxonomia):
                            $foto = $conteudoTaxonomia->description; ?>
                            <div class="col-md-6 serv" style="background: url(<?php echo "$foto"; ?>) no-repeat;">
                                <div class="lente" id="diminuir1">

                                </div>

                                <div class="conteudo">
                                    <h3><i class="fa fa-check-circle" aria-hidden="true"></i>&nbsp;<?php echo $conteudoTaxonomia->name;?></h3>


                                    <ul>
                                        <?php
                                            $servicos = new WP_Query(array(
                                                'post_type'         => 'servico',
                                                'posts_per_page'    => -1,
                                                'tax_query'         => array(
                                                                                array(
                                                                                    'taxonomy' => 'categoriaServico',
                                                                                    'field'    => 'slug',
                                                                                    'terms'    => $conteudoTaxonomia->slug
                                                                                )
                                                                            )
                                                )
                                            );
                                            while ( $servicos->have_posts() ) : $servicos->the_post();
                                        ?>
                                        <li><h3>|  &nbsp<?php the_title(); ?></h3></li>

                                        <?php endwhile; wp_reset_query(); ?>

                                    </ul>

                                </div>
                            </div>
                    <?php
                    endforeach;
                ?>

Any suggestions?

1 answer

1


You will set the fields outside the function of registering taxonomy, using the actions {taxonomy}_edit_form_fields and/or {taxonomy}_add_form_fields

add_action( "categoriaServico_edit_form_fields", array( $this, 'exibe_campo' ), 10, 2 )
add_action( "categoriaServico_add_form_fields", array( $this, 'exibe_campo' ), 10, 2 )

public function exibe_campo( $tag, $taxonomy ) {
    ?>
    <tr class="form-field">
        <th scope="row">
            <label for="campo">Titulo do campo</label>
        </th>
        <td>
            <input type="text" name="campo" value="" />
            <p class="description">Descrição do campo</p>
        </td>
    </tr>
    <?php
}

Do not forget to also create a function to save your fields, WP does not do this by default:

add_action( 'edited_terms', array( $this, 'salvar' ), 10, 2 );

public function salvar( $term_id, $taxonomy ) {
    $valor = $_POST['campo'];
    // valide permissões, nonces e outros parâmetros necessários aqui
    // e sanitize os dados antes de salvar

        update_term_meta( $term_id, 'term_meta_key', $valor );
    }
}
  • Dude, I haven’t made it yet. I’m a beginner in wordpress so I kind of hit the head. I edited Edit-tags.php and add the following snippet <div class="form-field term-order-wrap">&#xA; <label for="tag-order"><?php _e( 'Ordenação' ); ?></label>&#xA; <input name="tag-order" id="tag-order" type="text" value="" size="40" aria-required="true" />&#xA;</div> and already showed the field. And at the beginning of the archive is the $ret = wp_insert_term( $_POST['tag-name'], $taxonomy, $_POST ); but information is not saving

  • opa, opa, take 2 steps back. You ** cannot ** edit the edit-tags.php under no circumstances. Otherwise when Wordpress is updated you will lose everything. These changes have to go in your theme or plugin.

  • In fact, not only Edit-tags: never edit ANYTHING outside the folder wp-content

  • I understand, I will try to edit the plugin, so I understood the taxonomy was declared in the plugin in a file that has the same name of the project/ site

  • One question, the $this object what would be? The plugin where taxonomy is declared has no public and private objects and functions

  • $this is when it is in a class. If it is only a procedural file you can exchange the array for a string with the function name.

  • I was able to make the field appear like that in the plugin, but I can’t save the value of the field. The function that saves also goes in the plugin?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.