Categories
Uncategorized

WordPress: how to automatically convert custom fields to post tags

Convert your post’s custom fields into tags automatically in WordPress with this nifty script!!!

Hi all,

Sifting through stackoverflow.com I ran into this question: how do you add custom fields automatically as post tags in WordPress?

A while ago, someone asked something similar, and I put together a little script to help, but now I refined that script to be more encompassing. So, here’s a function to add custom fields automatically as post tags.

How it works

The vq20_convert_custom_fields_to_tags() function uses jQuery to retrieve the value of specific custom fields (which you can specify in an array, more on that below), and then adds those to the “tags” list on your post editor on save.

Instructions

  1. Put this script in your functions.php file in your WordPress install:
<?php

function vq20_convert_custom_fields_to_tags(){

    /*create list of custom fields to add as tags on save*/
    $custom_field_names = array();

    if(count($custom_field_names)>0) {?>
        <script type="text/javascript">
            jQuery(document).ready(function($){
            $('form#post').submit(function(event){
            <?php
                foreach($custom_field_names as $name){?>
                    cf_key = $('input[value="<?php echo $name; ?>"]').attr('id').replace('meta[', '').replace('][key]', '');
                    $('#new-tag-post_tag').val($('textarea[id*='+cf_key+']').val());
            <?php } ?>});
            });
        </script>
<?php
    }
}

add_action('admin_footer', 'vq20_convert_custom_fields_to_tags');

?>

 

* * * UPDATE * * *

A couple of  users in the comments below pointed out that the previous code was only adding the last custom field to the tag list, so I decided to go ahead and revamp the whole thing. Use this code instead:

<?php 

function vq20_convert_custom_fields_to_tags(){ ?>

  <script type="text/javascript">
    jQuery(document).ready(function($){      	

    	// Create list of custom fields to add as tags on save
    	// (e.g. var custom_field_names = ['my_custom_field', 'my_other_custom_field', 'yet_another_custom_field'];)
    	var custom_field_names = [];

    	$('form#post').submit(function(){
    		if(custom_field_names.length > 0) {
	    		var custom_field_values = [];
	    		$('#postcustom tr[id^="meta-"]').each(function(){
	    			var meta_id = $(this).attr('id').substring($(this).attr('id').indexOf('-')).replace('-','');
	    			if ($.inArray($(':text[id="meta[' + meta_id + '][key]"]').val(), custom_field_names) !== -1) {
	    				custom_field_values.push($('textarea[id="meta[' + meta_id + '][value]"]').val().toLowerCase());
	    			}
	    		});
	    		var tags = custom_field_values.join(',');
	    		$('#new-tag-post_tag').val(tags);
	    	}
    	});

    });
  </script>
<?php }
add_action('admin_footer', 'vq20_convert_custom_fields_to_tags');

?>

 

  1. Add the names of the custom fields you would like to automatically add as tags to the custom_field_names array
    
    // Create list of custom fields to add as tags on save 
    // (e.g. var custom_field_names = ['my_custom_field', 'my_other_custom_field', 'yet_another_custom_field'];) 
    var custom_field_names = ['my_custom_field', 'my_other_custom_field'];


 

  1. Save/upload your functions.php file, and then go to your post, add the matching custom field(s), and their values should be added as tags as soon as you save the post.

Note: this only works for custom fields holding individual values!!! (i.e. only one value per custom field will be added as a tag)

Let me know what you think!

Happy tagging!

12 replies on “WordPress: how to automatically convert custom fields to post tags”

Hi,

I’m actualy looking for a plugin that do the opposite.
I want to add a specific custom field, everytime I put a specific tag.

Do you know any plugin that does that ?

thx !

Great work !

Hey Colomb,

So, are you thinking of adding a custom field named after each tag? If so, what values would you add to it?

Let me know and we can further develop this! 😉

Hi,

Thanks for the Plugin.

I tried it as you mentioned, but not working.
Here is my function from function.php file.
function vq20_convert_custom_fields_to_tags(){

/*create list of custom fields to add as tags on save*/
$custom_field_names = array(‘actors’,’rating’);

if(count($custom_field_names)>0) {?>

jQuery(document).ready(function($){
$(‘form#post’).submit(function(event){

cf_key = $(‘input[value=””]’).attr(‘id’).replace(‘meta[‘, ”).replace(‘][key]’, ”);
$(‘#new-tag-post_tag’).val($(‘textarea[id*=’+cf_key+’]’).val());
});
});

<?php
}
}

add_action('admin_footer', 'vq20_convert_custom_fields_to_tags');

Can you please guide?

Hi parthvi,

Looking at the code you provided, it seems like you are missing the “foreach” statement somehow. That is the part that looks through your list of custom field names ($custom_field_names) and creates custom JavaScript to transfer the custom field values to the “tags” field on save.

The function should work as is. Would you mind copying and pasting it again? Also, make sure that you add the custom fields to your post as well.

Thanks, and let me know how it goes, I’ll be happy to help!

Hi Aaron,

Sorry about the delay on my response.

I’m not completely sure whether this would work with custom taxonomies, though once taxonomies are registered, the should heed to the same functions as default taxonomies.

Have you given it a try? I’d be curious to know as well. Let me know, and maybe I can help you.

Thanks!

Hi Vidal, i need a plugin like this but it needs to save tags to userprofiles.
Set or change tags to skills so users can add them to their profiles.
I know i can let users add these by adding custom fields, but i need them to act like tags en be visible in a tag cloud. Is this possible?
Or by adding tags to another taxonomy?
Willing to pay for a plugin solution

Hello there – I’ve implemented this code and it seem to function properly but is only adding the value of the very last custom field in the array to the list of tags. Thoughts?

Leave a Reply to rgponce Cancel reply

Your email address will not be published. Required fields are marked *