WordPress: how to automatically convert custom fields to post tags

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');

?>
  1. Add the names of the custom fields you would like to automatically add as tags to the array $custom_field_names
    /*create list of custom fields to add as tags on save*/
    $custom_field_names = array('sample_custom_field','another_sample_custom_field_name');

 

  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!

4 Comments

  1. 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 !

    Reply
    • 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! ;)

      Reply
  2. 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?

    Reply
    • 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!

      Reply

Leave a Comment