Advertisement
miriamdepaula

WordPress: adicionando meta fields no cadastro de taxonomias

Aug 26th, 2011
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. /*
  2. adicionando meta fields no cadastro de taxonomies / categories
  3. ATENÇÃO: LEMBRE-SE DE ALTERAR {taxonomy-slug} para o slug da sua taxonomia/ categoria. Por exemplo:
  4.  
  5. Taxonomia = colecao
  6.  
  7. {taxonomy-slug}_edit_form_fields = colecao_edit_form_fields ... e assim por diante!
  8.  
  9. Para exibir o valor do campo no tema, faça o seguinte:
  10. <?php
  11. $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
  12. $tax_meta = get_option('term_'.$term->term_id);
  13.  
  14. echo $tax_meta['{taxonomy-slug}_foto']; //não esqueça de alterar {taxonomy-slug} pelo slug da sua taxonomia/ categoria.
  15. ?>
  16. */
  17.  
  18. add_action ('{taxonomy-slug}_edit_form_fields', '{taxonomy-slug}_tax_fields');
  19. add_action('{taxonomy-slug}_add_form_fields','{taxonomy-slug}_tax_fields');
  20.  
  21. function {taxonomy-slug}_tax_fields( $tag ) {    //check for existing featured ID
  22.     $t_id = $tag->term_id;
  23.     $Tax_meta = get_option( "term_$t_id");
  24.  
  25.  
  26.     echo '<tr class="form-field">';
  27.     echo '      <th scope="row" valign="top"><label for="{taxonomy-slug}_foto">Foto</label></th>';
  28.     echo '          <td>';
  29.     echo '              <input type="text" name="Tax_meta[{taxonomy-slug}_foto]" id="Tax_meta[{taxonomy-slug}_foto]" size="25" style="width:60%;" value="';
  30.     echo ($Tax_meta['{taxonomy-slug}_foto']) ? $Tax_meta['{taxonomy-slug}_foto'] : '';
  31.     echo '"><br />';
  32.     echo '                      <span class="description">URL da foto ou avatar. Utilize o menu Midia para fazer o upload do arquivo.</span>';
  33.     echo '              </td>';
  34.     echo '  </tr>';
  35.  
  36. }
  37. // save extra taxonomy extra fields hook
  38. add_action ('edited_{taxonomy-slug}', 'save_extra_tax_fileds');
  39. add_action('created_{taxonomy-slug}','save_extra_tax_fileds');
  40.  
  41.  
  42. // save extra taxonomy extra fields callback function
  43. function save_extra_tax_fileds( $term_id ) {
  44.     if ( isset( $_POST['Tax_meta'] ) ) {
  45.         $t_id = $term_id;
  46.         $Tax_meta = get_option( "term_$t_id" );
  47.         $Tax_keys = array_keys($_POST['Tax_meta']);
  48.             foreach ($Tax_keys as $key){
  49.             if (isset($_POST['Tax_meta'][$key])){
  50.                 $Tax_meta[$key] = $_POST['Tax_meta'][$key];
  51.             }
  52.         }
  53.         //save the option array
  54.         update_option( "term_$t_id", $Tax_meta );
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement