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

Wordpress Custom Field In Tags WordPress

  • SOLVED

Please review code i am using in wordpress

http://scripton.net/_files/sponsorship.txt

If you add this to your to any local instance (functions.php) you will be able to have this custom field applied to categories.

The same applies to SHOULD apply Tags...although the hooks names are a little different. From what i found i need to use (see below)

add_action( 'add_tag_form_fields', 'sponsorship_add_new_meta_field');
and
add_action( 'edit_tag_form_fields', 'sponsorship_edit_meta_field');

Here is what i see:
The field is added in the Tags section... but when i put in a value and try to go and edit it... the value is gone. I want to be able to add this sponsorship field for every tag and finally know how to get that field value on my page. The problem is related to adding the custom field in Tags

Answers (2)

2015-07-16

Francisco Javier Carazo Gil answers:

I have done it before, let me try and I tell you now.


Francisco Javier Carazo Gil comments:

Kofi,

I have tried and the save/edit works.

I tell you now how to use it in a page.


Kofi Addaquay comments:

Thank you. Waiting for answer


Francisco Javier Carazo Gil comments:

How to get it from a page, different ways:

If you know the term_id:

$t_id = 13;
$tag_meta = get_option( "taxonomy_$t_id" );
echo $tag_meta['custom_tag_meta'];


If you know the term_slug:

$term = get_term_by('slug', 'your_slug', 'category')
$t_id = $term->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
echo $tag_meta['custom_tag_meta'];


Francisco Javier Carazo Gil comments:

Also, now you only have category working, if you want to use it in other taxonmies, you only have to add tags:

For tags:

edited_category -> edited_post_tag
create_category -> create_post_tag

So you would have to add:

add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');


Kofi Addaquay comments:

Ok... but that was the last part of the question. I am not about to edit the custom field before it shows up black for Tag... that was the main question. How the get the value in the page was the last part of question. The most important part is making it work with tags


Francisco Javier Carazo Gil comments:

But also for custom taxonomies:


add_action( 'edited_product_cat', save_sponsorship_custom_meta');

add_action( 'create_product_cat', 'save_sponsorship_custom_meta');

Or any other: edited_TAXONOMY and create_TAXONOMY


Francisco Javier Carazo Gil comments:

Read how it should work with tags and any other taxonomies.


Kofi Addaquay comments:

Ok...let me try this...sorry i just saw your post


Kofi Addaquay comments:

SORRY I AM CONFUSED. THE CODE IS NOT WORKING. PLEASE REVIEW MY FINAL CODE AND SEE WHY ITS NOT WORKING


function sponsorship_add_new_meta_field() { ?>
<div class="form-field">
<label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</div>
<?php
}
add_action( 'add_tag_form_fields', 'sponsorship_add_new_meta_field');

// Edit term page
function sponsorship_edit_meta_field($tag) { ?>
<?php
$t_id = $tag->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label></th>
<td>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="<?php echo esc_attr( $tag_meta['custom_tag_meta'] ) ? esc_attr( $tag_meta['custom_tag_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'edit_tag_form_fields', 'sponsorship_edit_meta_field');

//save sponsorships
function save_sponsorship_custom_meta( $tag_id ) {
if ( isset( $_POST['tag_meta'] ) ) {
$t_id = $tag_id;
$tag_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['tag_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['tag_meta'][$key] ) ) {
$tag_meta[$key] = $_POST['tag_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $tag_meta );
}
}
add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');


Francisco Javier Carazo Gil comments:

I am working on it.

Please wait a moment.


Francisco Javier Carazo Gil comments:

Tell me, what's the problem? I have tried and it works correctly.


Kofi Addaquay comments:

The problem is this

When i go into Tags....

I create a new tag and add a value for sponsorship.

I can successfully add the tag

When i go into edit... the sponsorship value is gone!


Kofi Addaquay comments:

The code only works for Categories... But i need them to work for Tags


Francisco Javier Carazo Gil comments:

Sorry but I am seeing it in my site and it works :(


Francisco Javier Carazo Gil comments:

No, I am trying with tags.

Are you sure you have this code working in your theme?


Kofi Addaquay comments:

It works for Tags?

Copy and Paste the code you are using in functions.php as i would also use it and let me try


Francisco Javier Carazo Gil comments:


function sponsorship_add_new_meta_field() { ?>
<div class="form-field">
<label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</div>
<?php
}
add_action( 'add_tag_form_fields', 'sponsorship_add_new_meta_field');

// Edit term page
function sponsorship_edit_meta_field($tag) { ?>
<?php
$t_id = $tag->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label></th>
<td>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="<?php echo esc_attr( $tag_meta['custom_tag_meta'] ) ? esc_attr( $tag_meta['custom_tag_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'edit_tag_form_fields', 'sponsorship_edit_meta_field');

//save sponsorships
function save_sponsorship_custom_meta( $tag_id ) {
if ( isset( $_POST['tag_meta'] ) ) {
$t_id = $tag_id;
$tag_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['tag_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['tag_meta'][$key] ) ) {
$tag_meta[$key] = $_POST['tag_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $tag_meta );
}
}
add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');


Francisco Javier Carazo Gil comments:

I have to go out now, if you want send me data via a private message and tomorrow I do it and see in your server what it is failing.


Kofi Addaquay comments:

It worked !! yes


Francisco Javier Carazo Gil comments:

Ah it worked :)

Great. Tell me if have doubts to show it in the page and I tell you.


Kofi Addaquay comments:

Now the last part from my question is how to access the sponsorship code in my page


Francisco Javier Carazo Gil comments:

I told you:

If you know the term_id:


$t_id = 13;
$tag_meta = get_option( "taxonomy_$t_id" );
echo $tag_meta['custom_tag_meta'];



If you know the term_slug:


$term = get_term_by('slug', 'your_slug', 'tag')
$t_id = $term->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
echo $tag_meta['custom_tag_meta'];

Any more question?


Kofi Addaquay comments:

let me try !


Kofi Addaquay comments:

So i have created a new tag with the following values

Name: KofiTest
Description: some test description
Spons Code: NEWKOFISPONSCODE

The slug is : kofitest

Then in single.php

if( has_tag() ) {

$term = get_term_by('slug', 'kofitest', 'tag');
$t_id = $term->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
echo 'this sponsorship is' . $tag_meta['custom_tag_meta'];
}
else {
echo "Sponsorship Test: THERE ARE NO TAG(S)";
}

Sound this not work? i dont get anything back


Francisco Javier Carazo Gil comments:

Try to remove has_tag and tell me.


Kofi Addaquay comments:

Thank you. I sent you a private message


Kofi Addaquay comments:

I need the has_tag... when i take it out... i get a different sponsorship code. There is sponsorship on the post level as well...and that is what i get. I want the sponsorship on the tag level.


Francisco Javier Carazo Gil comments:

I have to leave PC.

Send me private message and we finish it tomorrow (I will be available again in 11 hours)


Kofi Addaquay comments:

Ok. thank you

2015-07-16

Luis Abarca answers:

i think you need to add hooks for post tags.


add_action( 'edited_category', 'save_sponsorship_custom_meta');
add_action( 'create_category', 'save_sponsorship_custom_meta');

add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');


You should have a look to ACF [[LINK href="http://www.advancedcustomfields.com/"]]http://www.advancedcustomfields.com/[[/LINK]]


Kofi Addaquay comments:

CAN YOU VIEW THIS CODE... AND MAKE THE APPROPRIATE CHANGE. PLEASE POST THE ENTIRE CODE BACK WITH THE CHANGES

function sponsorship_add_new_meta_field() { ?>
<div class="form-field">
<label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</div>
<?php
}
add_action( 'add_tag_form_fields', 'sponsorship_add_new_meta_field');

// Edit term page
function sponsorship_edit_meta_field($tag) { ?>
<?php
$t_id = $tag->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label></th>
<td>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="<?php echo esc_attr( $tag_meta['custom_tag_meta'] ) ? esc_attr( $tag_meta['custom_tag_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'edit_tag_form_fields', 'sponsorship_edit_meta_field');

//save sponsorships
function save_sponsorship_custom_meta( $tag_id ) {
if ( isset( $_POST['tag_meta'] ) ) {
$t_id = $tag_id;
$tag_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['tag_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['tag_meta'][$key] ) ) {
$tag_meta[$key] = $_POST['tag_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $tag_meta );
}
}
add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');


Luis Abarca comments:


// Add term page
function sponsorship_add_new_meta_field() { ?>
<div class="form-field">
<label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</div>
<?php
}

add_action( 'category_add_form_fields', 'sponsorship_add_new_meta_field');

// Edit term page
function sponsorship_edit_meta_field($tag) { ?>
<?php
$t_id = $tag->term_id;
$tag_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tag_meta[custom_tag_meta]"><?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?></label></th>
<td>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="<?php echo esc_attr( $tag_meta['custom_tag_meta'] ) ? esc_attr( $tag_meta['custom_tag_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?></p>
</td>
</tr>
<?php
}

add_action( 'category_edit_form_fields', 'sponsorship_edit_meta_field');

//save sponsorships
function save_sponsorship_custom_meta( $tag_id ) {
if ( isset( $_POST['tag_meta'] ) ) {
$t_id = $tag_id;
$tag_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['tag_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['tag_meta'][$key] ) ) {
$tag_meta[$key] = $_POST['tag_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $tag_meta );
}
}
add_action( 'edited_category', 'save_sponsorship_custom_meta');
add_action( 'create_category', 'save_sponsorship_custom_meta');
add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');


Kofi Addaquay comments:

Does not work... this only works for Categories. I need it for Tags !

as i said earlier...it works for Categories. But it fails for Tags. I need help with making it work for Tags


Luis Abarca comments:

Try this.


<?php

// Add term page
function sponsorship_add_new_meta_field()
{
?>
<div class="form-field">
<label for="tag_meta[custom_tag_meta]">
<?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?>
</label>

<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="">

<p class="description">
<?php _e( 'Enter sponsorship code','sni_tags_spons' ); ?>
</p>
</div>
<?php
}

add_action( 'post_tags_add_form_fields', 'sponsorship_add_new_meta_field');
add_action( 'category_add_form_fields', 'sponsorship_add_new_meta_field');

// Edit term page
function sponsorship_edit_meta_field($tag)
{
$t_id = $tag->term_id;
$tag_meta = get_option( "taxonomy_{$t_id}" );
$custom_tag_meta = $tag_meta['custom_tag_meta'];
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="tag_meta[custom_tag_meta]">
<?php _e( 'Please Enter Sponsorship Code', 'sni_tags_spons' ); ?>
</label>
</th>
<td>
<input type="text" name="tag_meta[custom_tag_meta]" id="tag_meta[custom_tag_meta]" value="<?php echo $custom_tag_meta ? esc_attr($custom_tag_meta) : ''; ?>">
<p class="description"><?php _e( 'Enter sponsorship code','sni_tags_spons' ) ?></p>
</td>
</tr>
<?php
}

add_action( 'post_tags_edit_form_fields', 'sponsorship_edit_meta_field');
add_action( 'category_edit_form_fields', 'sponsorship_edit_meta_field');

//save sponsorships
function save_sponsorship_custom_meta( $tag_id )
{
if ( isset( $_POST['tag_meta'] ) ) {
$t_id = $tag_id;
$tag_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['tag_meta'] );

foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['tag_meta'][$key] ) ) {
$tag_meta[$key] = $_POST['tag_meta'][$key];
}
}

// Save the option array.
update_option( "taxonomy_{$t_id}", $tag_meta );
}
}

add_action( 'edited_category', 'save_sponsorship_custom_meta');
add_action( 'create_category', 'save_sponsorship_custom_meta');
add_action( 'edited_post_tag', 'save_sponsorship_custom_meta');
add_action( 'create_post_tag', 'save_sponsorship_custom_meta');