How can I add pre-defined options to the “add new” custom field dropdown?
Here’s two examples of automatically adding and showing new custom fields:
That’s close to what I want to do; my goal is to add pre-defined options to the “add new” custom filed dropdown, but not have them show as fields until the blogger adds them.
I’d like to know how to do this without using a plugin.
You cannot do that with pure PHP, because the fields are fetched from existing fields, and there is no hook. But you can use JavaScript, check if the post type supports custom fields and the field does not exist already – and insert it:
Just add the above code anywhere in your themes function.php.
After you’ve added the above code, it would add ‘www.cyberxoft.com’ to the drop down as one of the option to select.
If you get to see it, then just replace ‘www.cyberxoft.com’ with your required field name and refresh the admin page and when you see that happened just go ahead and add as many as you like BUT remember that only 30 could be viewed as thats the default limit set for it.
Enjoy…
Below is a modified version of the awesome script posted by @toscho. I just needed the ability to create the
<select>
if it didn’t already exist.I’m sure my JS adaptations could be improved, but it gets the job done. I’ll post updated code back here if I make changes/improvements.
Thanks again @toscho!!!! I owe you one. 🙂
It works in wordpress v5.8.1