1

I have a front end form, I need that form to attach a taxonomy term to the post, however, I have offered users a dropdown of options, 'a dropdown of custom taxonomy terms.' To create this drop down i have tried a select attribute and pulled the terms through as options. The problem comes however, due to the fact I have a large multitude of taxonomies that are not practical to show in a dropdown. In this case I pulled the terms through a datalist. This then creates the problem that users can, with the code i'm using below, create their own custom taxonomy terms by writing something that isn't suggested by the datalist.

I need to stop this, either by stopping the datalist from allowing values that aren't in the options, or by using

wp_set_object_terms($post_id, $_POST['terms'], 'products-tax', true);

to not accept any taxonomy terms that do not exist, and parse an error to say choose a value value from the list. However, I cannot work out how to do either option.

Any help would be invaluable thank you!

Code I am using:

This is used to set the taxonomy and terms for the post.

'tax_input' => array('products-tax' => $_POST['terms'])

This attaches the terms

wp_set_object_terms($post_id, $_POST['terms'], 'products-tax', true);

This is the input and datalist that fetches the taxonomy terms and lists them as options.

<input type="text" list="products" id="product" name="terms[]" required>
<datalist id="products">
<?php
$products_tax = get_terms('products-tax', 'orderby=id&hide_empty=0');
$counter = 0;
foreach ($products_tax as $product ) {
$counter++;
echo '<option value="'.$product->slug.'">';

}
?>
</datalist>
  • 1
    You can find options by typing with a regular select too. Why would you use a datalist for that? – fuxia Feb 3 '15 at 0:01
  • 1
    how is it a wordpress specific question? – Mark Kaplun Feb 3 '15 at 7:05
  • Is this about a JavaScript autocomplete? – kaiser Feb 3 '15 at 8:31
  • @Mark Kaplun it is wordpress specific in that each option is set by the taxonomy term variable, and so I am not sure whether I have to adapt this to stop users from being able to create new terms. – NDog Feb 3 '15 at 17:49
  • 1
    @MarkKaplun ok, apologies, I will ask again in an html forum. And again, I say it is wordpress specific because I am unsure if the wp_set_object_terms($post_id, $_POST['terms'], 'products-tax', true); causes the problem by allowing users to make their own taxonomy term, instead I would want it to parse an error to say 'this product doesn't exist, please choose an eligible product' sort of a thing. – NDog Feb 3 '15 at 18:14
0

you will probably want to use term_exists http://codex.wordpress.org/Function_Reference/term_exists to check if the term is "valid". Probably worth checking how good is that API with mismatched cases.

Another option is to use AJAX to populated your input control base on the user input. I am almost sure there is some jquery plugin for that, and then hopefully you should be able to use a hidden input to contain the term IDs. This might not be very easy to implement on the server side depending on the details of the values you have to return in each AJAX request.

And what might be the best option if you don't care about that specific page to get a little bloated, is to create an array in javascript that contains all the terms and verify the input against it before sending it to the server (and again send only the term id). You probably have a jquery plugin for this as well, and all that you will need to do on the wordpress side is to generate the javascript array and inject it into the HTML.

  • I think the term_exists function could possibly work, however, haven't figured out how to implement it. In terms of the second javascript idea, this could work, however the taxonomy terms will be increasing as new ones will be constantly added and so would be unsustainable to manually add them each time one is made into the javascript array. This could only work if I use a wordpress function to pull them in. However, I am slightly out of my depth. I am very grateful for your input so far though. – NDog Feb 4 '15 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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