0

Been using this answer -->> (link) to create some custom fields in my categories
and it works great ! my only problem is that i have a frontend form that allows
creating new categories with an unrelated form and i would like to insert
values for my custom fields as well as each category pre-defined fields

i am currently adding the built in fields aka category name, slug & description
using wp_insert_term using this small check & insert script

if($_SERVER['REQUEST_METHOD'] == "POST")  {
    $clientName = $_POST["clientName"];
    $term_id = $_POST["clientName"];
    $clientKeywords = $_POST["clientKeywords"];
    $clientSlug = $_POST["clientSlug"];
    $parent = '';

    $errorType = 'Category "'. $clientName .'" Added!';

        wp_insert_term($clientName, 'category', array(
        'description'=>$clientKeywords,
        'slug'=>sanitize_title($clientSlug),
        'parent'=>$parent
    )); 
}

.
Again.. inserting the custom fields values from the backend using the code i have found in the answer i have linked from at the beginning of my question works very well but i have tried a lot of ways to make it work from the front-end / integrate it with wp_insert_term and have not succeeded so far.

Any help would be most appreciated

1 Answer 1

1

the function wp_insert_term returns the newly created term id (or WP_Error on error), so once your create your term you need to store it's ID and then you can save the "extra fields" using get_option, update_option something like:

if($_SERVER['REQUEST_METHOD'] == "POST")  {
    $clientName = $_POST["clientName"];
    $term_id = $_POST["clientName"];
    $clientKeywords = $_POST["clientKeywords"];
    $clientSlug = $_POST["clientSlug"];
    $parent = '';

    $errorType = 'Category "'. $clientName .'" Added!';

    $term_id = wp_insert_term($clientName, 'category', array(
        'description'=>$clientKeywords,
        'slug'=>sanitize_title($clientSlug),
        'parent'=>$parent
    )); 
    if (!is_wp_error($term_id)){
        //save here
        $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
        $tag_extra_fields[$term_id]['field'] = strip_tags($_POST['field']);
        $tag_extra_fields[$term_id]['field2'] = strip_tags($_POST['field2']);
        update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
    }
}
1
  • 1
    Thanks a lot @Bainternet - as useall you deliver an easy to understand working solution ;)
    – Sagive
    Apr 15, 2012 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.