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
- 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'); ?>
- 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'];
- 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!