Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

add custom fields of each term of one taxonomy WordPress

  • SOLVED

to add custom fields in the page of edtion of each category i use this code

//add extra fields to category edit form hook
add_action ( 'edit_category_form_fields', 'extra_category_fields');
//add extra fields to category edit form callback function
function extra_category_fields( $tag ) { //check for existing featured ID
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_soustitre">Sous titre</label></th>
<td>
<input type="text" name="Cat_meta[soustitre]" id="Cat_meta[soustitre]" size="3" style="width:60%;" value="<?php echo $cat_meta['soustitre'] ? $cat_meta['soustitre'] : ''; ?>">
<span class="description">Ici on ecrit le sous-titre</span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="texte">Texte long</label></th>
<td>
<textarea name="Cat_meta[texte]" id="Cat_meta[texte]" style="width:60%;"><?php echo $cat_meta['texte'] ? $cat_meta['texte'] : ''; ?></textarea>
<span class="description">Ici le texte long</span>
</td>
</tr>
<?php
}


but what i want is to use this fields only in the page of edition of each term of a specific taxonomy (named "society")

Answers (3)

2012-06-22

Arnav Joy answers:

try this

<?php

if(is_admin() && $_GET['taxonomy'] == 'society' ) {

//add extra fields to category edit form hook

add_action ( 'edit_category_form_fields', 'extra_category_fields');

//add extra fields to category edit form callback function

function extra_category_fields( $tag ) { //check for existing featured ID

$t_id = $tag->term_id;

$cat_meta = get_option( "category_$t_id");

?>

<tr class="form-field">

<th scope="row" valign="top"><label for="cat_soustitre">Sous titre</label></th>

<td>

<input type="text" name="Cat_meta[soustitre]" id="Cat_meta[soustitre]" size="3" style="width:60%;" value="<?php echo $cat_meta['soustitre'] ? $cat_meta['soustitre'] : ''; ?>">

<span class="description">Ici on ecrit le sous-titre</span>

</td>

</tr>

<tr class="form-field">

<th scope="row" valign="top"><label for="texte">Texte long</label></th>

<td>

<textarea name="Cat_meta[texte]" id="Cat_meta[texte]" style="width:60%;"><?php echo $cat_meta['texte'] ? $cat_meta['texte'] : ''; ?></textarea>

<span class="description">Ici le texte long</span>

</td>

</tr>

<?php

}

}


Sébastien | French WordpressDesigner comments:

it doesn't work


Arnav Joy comments:

to use it

i do not how you are fetching term_id but following code will help you..


<?php
$t_id = $tag->term_id; // $t_id will the term id of the term
$cat_meta = get_option( "category_$t_id");

echo $cat_meta['soustitre'] ? $cat_meta['soustitre'] : 'NA';
echo $cat_meta['texte'] ? $cat_meta['texte'] : 'NA';

?>


Arnav Joy comments:

for adding content try following

add_action( 'edited_society', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_society', 'save_taxonomy_custom_meta', 10, 2 );


function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['Cat_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['Cat_meta'][$key] ) ) {
$term_meta[$key] = $_POST['Cat_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}


Sébastien | French WordpressDesigner comments:

sorry but could you write ALL the code please


Arnav Joy comments:


if(is_admin() && $_GET['taxonomy'] == 'society' ) {

//add extra fields to category edit form hook



add_action ( 'edit_tag_form_fields', 'extra_category_fields');

//add extra fields to category edit form callback function



function extra_category_fields( $tag ) { //check for existing featured ID

$t_id = $tag->term_id;

$cat_meta = get_option( "category_$t_id");

?>

<tr class="form-field">

<th scope="row" valign="top"><label for="cat_soustitre">Sous titre</label></th>

<td>

<input type="text" name="Cat_meta[soustitre]" id="Cat_meta[soustitre]" size="3" style="width:60%;" value="<?php echo $cat_meta['soustitre'] ? $cat_meta['soustitre'] : ''; ?>">

<span class="description">Ici on ecrit le sous-titre</span>

</td>

</tr>

<tr class="form-field">

<th scope="row" valign="top"><label for="texte">Texte long</label></th>

<td>

<textarea name="Cat_meta[texte]" id="Cat_meta[texte]" style="width:60%;"><?php echo $cat_meta['texte'] ? $cat_meta['texte'] : ''; ?></textarea>

<span class="description">Ici le texte long</span>

</td>

</tr>

<?php

}

add_action( 'edited_society', 'save_taxonomy_custom_meta', 10, 2 );

add_action( 'create_society', 'save_taxonomy_custom_meta', 10, 2 );





function save_taxonomy_custom_meta( $term_id ) {

if ( isset( $_POST['Cat_meta'] ) ) {

$t_id = $term_id;

$term_meta = get_option( "taxonomy_$t_id" );

$cat_keys = array_keys( $_POST['Cat_meta'] );

foreach ( $cat_keys as $key ) {

if ( isset ( $_POST['Cat_meta'][$key] ) ) {

$term_meta[$key] = $_POST['Cat_meta'][$key];

}

}

// Save the option array.

update_option( "taxonomy_$t_id", $term_meta );

}

}

}




Sébastien | French WordpressDesigner comments:

that doesn't work, the content of fields is not saved

2012-06-22

Navjot Singh answers:

Try this:

if(is_admin() && $_GET['taxonomy'] == 'society' ) {
//add extra fields to category edit form hook

add_action ( 'edit_tag_form_fields', 'extra_category_fields');
//add extra fields to category edit form callback function

function extra_category_fields( $tag ) { //check for existing featured ID
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_soustitre">Sous titre</label></th>
<td>
<input type="text" name="Cat_meta[soustitre]" id="Cat_meta[soustitre]" size="3" style="width:60%;" value="<?php echo $cat_meta['soustitre'] ? $cat_meta['soustitre'] : ''; ?>">
<span class="description">Ici on ecrit le sous-titre</span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="texte">Texte long</label></th>
<td>
<textarea name="Cat_meta[texte]" id="Cat_meta[texte]" style="width:60%;"><?php echo $cat_meta['texte'] ? $cat_meta['texte'] : ''; ?></textarea>
<span class="description">Ici le texte long</span>
</td>
</tr>
<?php
}
}


Sébastien | French WordpressDesigner comments:

well ! It works ! :-)
and how can i use the content of each fields in front-office please ?


Sébastien | French WordpressDesigner comments:

just a problem : the content of fields are not saved


Navjot Singh comments:

That's because the code to save fields was not included. Here's the complete code to build and save the fields:

add_action ( 'society_edit_form_fields', 'extra_category_fields');
//add extra fields to category edit form callback function

function extra_category_fields( $tag ) { //check for existing featured ID
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_soustitre">Sous titre</label></th>
<td>
<input type="text" name="cat_meta[soustitre]" id="Cat_meta[soustitre]" size="3" style="width:60%;" value="<?php echo $cat_meta['soustitre'] ? $cat_meta['soustitre'] : ''; ?>">
<span class="description">Ici on ecrit le sous-titre</span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="texte">Texte long</label></th>
<td>
<textarea name="cat_meta[texte]" id="Cat_meta[texte]" style="width:60%;"><?php echo $cat_meta['texte'] ? $cat_meta['texte'] : ''; ?></textarea>
<span class="description">Ici le texte long</span>
</td>
</tr>
<?php
}
}
add_action( 'edited_society', 'save_extra_taxonomy_fields');
function save_extra_taxonomy_fields( $term_id ) {
if ( isset( $_POST['cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "taxonomy_$t_id");
$cat_keys = array_keys($_POST['cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['cat_meta'][$key])){
$cat_meta[$key] = $_POST['cat_meta'][$key];
}
}
//save the option array
update_option( "taxonomy_$t_id", $cat_meta );
}
}


Also to use this code on frontend, use this:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ));
//so term ID
$t_ID = $term->term_id;
$term_data = get_option("t_ID");


So now to show the value of your field name 'soustitre' you can use
if (isset($term_data['soustitre'])){
echo $term_data['soustitre'];
}


Sébastien | French WordpressDesigner comments:

that doesn't work, the content of fields is not saved


Sébastien | French WordpressDesigner comments:

i have change your code

add_action( 'edited_society', 'save_extra_taxonomy_fields');

function save_extra_taxonomy_fields( $term_id ) {

if ( isset( $_POST['cat_meta'] ) ) {

$t_id = $term_id;

$cat_meta = get_option( "taxonomy_$t_id");

$cat_keys = array_keys($_POST['cat_meta']);

foreach ($cat_keys as $key){

if (isset($_POST['cat_meta'][$key])){

$cat_meta[$key] = $_POST['cat_meta'][$key];

}

}

//save the option array

update_option( "taxonomy_$t_id", $cat_meta );

}

}


by

add_action( 'edited_society', 'save_extra_taxonomy_fields');
function save_extra_taxonomy_fields( $term_id ) {
if ( isset( $_POST['cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['cat_meta'][$key])){
$cat_meta[$key] = $_POST['cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}

and now the content of fields is saved

Now i search how display the content in the front (your code doesn't work)


Sébastien | French WordpressDesigner comments:

ok, now it works.
I modified your code.
You wrote
$term_data = get_option("t_ID");
You must have been tired ;-)


Navjot Singh comments:

Ah, sorry. I missed it. :| Glad you got it working.

2012-06-22

Luis Abarca answers:

Use this hook


add_action( 'society_edit_form_fields', 'extra_category_fields', 10, 2);


Sébastien | French WordpressDesigner comments:

it doesn't work


Luis Abarca comments:

Sorry its


add_action( 'society_edit_form', 'extra_category_fields' );