0

I have this script (found online) to import the values of an input form field as a taxonomy term. I got it working, but now I'd like to import/save the values of multiple form fields. Now all it does is import the value of the field 'input_name' as a taxonomy term. But, lets say, I'd like to import the values of 'input_name2' and 'input_name3' as well. I should be able to do that with an array and a foreach right? I am just quite new to PHP so could someone help me out here? Below is the code (also notice the replace function to replace spaces with -). Thanks a lot!

//Custom field to custom taxonomy script
$parent_term2 = term_exists( 'the_tax' ); // array is returned if taxonomy is given
$parent_term_id2 = $parent_term2['term_id']; // get numeric term id
$plaats2 = $_POST['input_name'];
$plaatsn2 = ucwords(strtolower( $plaats2 ));
$plaatsn3 = array(" ");
$plaatsn4   = array("-");

$plaatsn5 = str_replace($plaatsn3, $plaatsn4, $plaatsn2);

$cid2 = wp_insert_term(
$plaatsn5, // the term 
  'the_tax', // the taxonomy
  array(
    'description'=> 'Tax term '.$plaatsn5.'!',
    'slug' => $plaatsn5, 
    'parent'=> $parent_term2['term_id']
  )
);

wp_set_object_terms( $listing_id, $plaatsn5, 'the_tax', false);

2 Answers 2

0

I would build the input fields for terms as array of inputs. Like this (note the [] inthe name attribute:

<input name="input_name[]" type="text" value="">
<input name="input_name[]" type="text" value="">
<input name="input_name[]" type="text" value="">

Then, in PHP:

//Now $_POST['input_name']; is an array
//get the array and sanitize it
$input_terms = array_map( 'sanitize_text_field', $_POST['input_name'] );
//Set the array of terms for later use on wp_set_object_terms
$terms = array();
foreach( $input_terms as $term ) {

    $existent_term = term_exists( $term, 'the_tax' );

    if( $existent_term && isset($existent_term['term_id']) ) {

        $term_id = $existent_term['term_id'];

    } else {

        //intert the term if it doesn't exsit
        $term = wp_insert_term(
            $term, // the term 
            'the_tax' // the taxonomy
        );
        if( !is_wp_error($term ) && isset($term['term_id']) ) {
             $term_id = $term['term_id'];
        } 

   }

   //Fill the array of terms for later use on wp_set_object_terms
   $terms[] = (int) $term_id;

}

//I assume that $listing_id is correct, you didn't shou it in your code
wp_set_object_terms( $listing_id, $terms, 'the_tax' );

UPDATE

I think is not very good approach, but as you asked about it:

<input name="input_name1" type="text" value="">
<input name="input_name2" type="text" value="">
<input name="input_name3" type="text" value="">

Then, in PHP:

//Populate an array with the input fields
$input_terms = array();
$input_terms[] = sanitize_text_field( $_POST['input_name1'] );
$input_terms[] = sanitize_text_field( $_POST['input_name2'] );
$input_terms[] = sanitize_text_field( $_POST['input_name3'] );

//Set the array of terms for later use on wp_set_object_terms
$terms = array();
foreach( $input_terms as $term ) {

    $existent_term = term_exists( $term, 'the_tax' );

    if( $existent_term && isset($existent_term['term_id']) ) {

        $term_id = $existent_term['term_id'];

    } else {

        //intert the term if it doesn't exsit
        $term = wp_insert_term(
            $term, // the term 
            'the_tax' // the taxonomy
        );
        if( !is_wp_error($term ) && isset($term['term_id']) ) {
             $term_id = $term['term_id'];
        } 

   }

   //Fill the array of terms for later use on wp_set_object_terms
   $terms[] = (int) $term_id;

}

//I assume that $listing_id is correct, you didn't shou it in your code
wp_set_object_terms( $listing_id, $terms, 'the_tax' );

NOTE: You don't need the str_replace function to replace white spaces for "-". wp_insert_term generates the slug from the term name.

8
  • Hi, thank you. Not familiar with this sorry. Can i give the input names different values? I need the input names to be unique for some other reason. If i'd do that with your solution than the $input_terms is not correct anymore right? Could you maybe further explain this solution? Thank you!
    – RobbertT
    Commented Dec 31, 2014 at 11:27
  • Do you mean set differents values to name attribute of the input fields?
    – cybmeta
    Commented Dec 31, 2014 at 12:04
  • okay thanks. So i have these input fields: input name"input_name[]" type="text" value="input1"> and input name"input_name2[]" type="text" value="input2"> but how does the $input_terms = look like then? because $input_terms = array_map( 'sanitize_text_field', $_POST['input_name'], $_POST['input_name2'] ); won't work right? Thanks
    – RobbertT
    Commented Dec 31, 2014 at 12:09
  • See edited answer.
    – cybmeta
    Commented Dec 31, 2014 at 12:13
  • Thanks again. Here is the code I am using now. jsfiddle.net/tywk34mp It is not working. No value is inserted as tax term. However when i change $terms to $term in the wp_set_object_terms only the last value (the value of the last input field) is inserted as tax term. Am I doing something wrong?
    – RobbertT
    Commented Dec 31, 2014 at 12:34
0

You can do it with foreach loop.. for example please checkbelow example

private function set_post_terms( $post, $value, $taxonomy ) {

/* First check to see if there are multiple terms and,
 * if so, then loop through the values and update each
 * term. 
 */
if ( $this->has_multiple_terms( $value ) ) {
    $this->set_multiple_terms( $post, $value, $taxonomy );
} else {

    $term = term_exists( strtolower( $value ), $taxonomy );

    // If the taxonomy doesn't exist, then we create it
    if ( 0 === $term || null === $term ) {

        $term = wp_insert_term(
            $value,
            $taxonomy,
            array(
                'slug' => strtolower( str_ireplace( ' ', '-', $value ) )
            )
        );

    }

}

    // Then we can set the taxonomy
    wp_set_post_terms( $post['ID'], $term, $taxonomy, true );

}
2
  • Hi, Thank you so much. I still need to add some to your code right? How do I put the $_POST values in an array and how to put them in a foreach loop? I tried something like this: $list = array($_POST['input_name'], $_POST['input_name2']); foreach ($list as $value) { $cid2 = wp_insert_term( $value, // the term 'the_tax', // the taxonomy array( 'description'=> 'Tax term'.$value.'!', 'slug' => $value, 'parent'=> $parent_term2['term_id'] ) ); But that doesnt seem to work. Also; I'd like to use the str_replace for the input value as well, not only the slug. Thank you!\
    – RobbertT
    Commented Dec 31, 2014 at 11:07
  • you can use your data in array like <input name="input_name[name1]" type="text"> then <input name="input_name[name2]" type="text" /> then you can get your input_name as array in $_POST Commented Jan 1, 2015 at 10:57

Your Answer

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

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