This one drove me crazy, but I finally figured it out.
In WordPress, if you need to automatically add tags to a post via a PHP script, the best way to do it is via the wp_set_object_terms() function. There is no handy “wp_insert_tag()” function, or something like that, so look no further.
wp_set_object_terms()
wp_set_object_terms() is a powerful function that not only helps you assign tags to posts (or pages), but also categories and other terms. For this example, here’s the PHP code needed to assign tags to a post.
<?php
$tags = array('html', 'css', 'javascript');
wp_set_object_terms( $post_id, $tags, 'post_tag', true );
?>
The code above will create the new tags (or terms) if they don’t exist and link them to the post specified by $post_id.
For more info on this function and its parameter, check out the codex page at http://codex.wordpress.org/Function_Reference/wp_set_object_terms/

Thanks for the tip, very useful !
You are welcome!
I think I this will be helpful on a project I’m working on.
I’m looking for a way to automatically fetch the SEO meta keyword from The All in one SEO Plugin to add as a post tag. The idea is to make the meta keyword the same as the post tag upon hitting publish.
I appreciate your help in figuring out how to implement the above snippet on the project I’m working with.
Thanks
Hi Aris,
The All-in-one SEO pack plugin for WP actually has an option to use post tags as meta keywords (in addition to any other keywords you may have already added to the post), possibly accomplishing what you are looking for. I’m not sure if it does it by default, but you can definitely choose to do so on the plugin’s option page.
Please let me know if that is what you were looking for. Otherwise, I’d be happy to help you further explore the matter.
Thanks!
Thanks for your help, Vidal!
I am aware of that feature of the All in one SEO plugin where you can automatically set the tags as meta keywords.
But the solution I want to implement is the the other way around.
I want the the keywords on SEO meta keywords to be automatically fetch or converted into post tags upon hitting publish..
Thanks Again…
Best Regards,
Aris
Hi Aris,
Sorry it took me so long to get back to you, but I finally got some time to take a stab at this.
I found a solution to your problem using jQuery. Paste the code below to your theme’s functions.php file and it will allow you to add all-in-one-seo-pack keywords as post tags on publish/update.
Thanks, and please let me know how it goes!
Vidal
< ?php
function aiosp_convert_keywords_to_tags(){?>
jQuery(document).ready(function($){
$('form#post').submit(function(event){
$('#new-tag-post_tag').val($('input[name="aiosp_keywords"]').val());
});
});
<?php }
add_action('admin_footer', 'aiosp_convert_keywords_to_tags');
?>
Thanks for putting time on this, Vidal!
I will let you know of the results once I implement your solution.
I really appreciate your help..
Regards,
Aris
Hi Vidal,
Your above solutions works like charm when I implement it on my project.
Thank you very much! More power…
Kind Regards,
Aris
You are welcome! I’m glad it worked.