Hi there,
I’m implementing the OptionTree in my WordPress new template. I’ve set all settings with option tree and everything works great. Two things I would love to know:
1. Is it possible to make a field required (mandatory), that cannot be left empty for example. 2. Is there a way to validate to input ?
Thanks.
- Interviewed on the Envato Notes blog
- Microlancer Beta Tester
- Most Wanted Bounty Winner
- Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
- Author was Featured
- Bought between 100 and 499 items
- Referred between 500 and 999 users
- Won a Competition
Hi Neutrino!
Good question! It might be best to post this over at http://wordpress.org/tags/option-tree, but I’ve been working with OT a lot and haven’t really run across this feature yet… usually I use a fallback of sorts… something like this can be handy:
if(ot_get_option('frontpage_post_count')) :
$fp_post_count = ot_get_option('frontpage_post_count');
else :
$fp_post_count = -1;
endif;
That basically just checks for a value, and sets a fallback value in case the user hasn’t filled it out yet.
Alternatively, you can do it with markup… for instance, I use something like this for the user’s logo… if they’ve uploaded something, we naturally want to use their image… but if they haven’t uploaded an image, we’ll want to use their “Site Title” and “Site Description” that they setup in their general settings panel:
<a href="<?php echo home_url(); ?>/" title="<?php echo bloginfo('blog_name'); ?>">
<h1 id="logo" class="sixteen columns">
<?php if(ot_get_option('logo')) {
$logopath = ot_get_option('logo'); ?>
<img id="logotype" src="<?php echo $logopath; ?>" alt="<?php echo bloginfo('blog_name'); ?>" />
<?php } else { ?>
<?php echo bloginfo('blog_name'); ?>
<br />
<span><?php echo get_bloginfo('description'); ?></span>
<?php } ?>
</h1>
</a>
I could probably tidy that second one up a bit for the sake of clarity, but hopefully that’ll give you the basic idea.
Cheers! B
- Author had a File in an Envato Bundle
- Author was Featured
- Bought between 1 and 9 items
- Europe
- Exclusive Author
- Has been a member for 2-3 years
- Item was Featured
- Referred between 100 and 199 users
Enque a custom jquery file and make jquery validations? 
Fallbacks are a great idea with or without the validation.
- Envato Staff
- Reviewer
- Community Moderator
- Venezuela
- Has been a member for 4-5 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Microlancer Beta Tester
- Sold between 10 000 and 50 000 dollars
- Exclusive Author
Why would you do that? Never force your users to enter information. That’s against one of the design principles:
“Flexibility in Use: The design accommodates a wide range of individual preferences [...]”
Use conditionals and make sure the layout doesn’t break if the field has not been set.
- Has been a member for 4-5 years
- Item was Featured
- Author was Featured
- Bought between 100 and 499 items
- Referred between 200 and 499 users
- Exclusive Author
- Microlancer Beta Tester
- Author had a Free File of the Month
epicera said
Hi Neutrino!Good question! It might be best to post this over at http://wordpress.org/tags/option-tree, but I’ve been working with OT a lot and haven’t really run across this feature yet… usually I use a fallback of sorts… something like this can be handy:
if(ot_get_option('frontpage_post_count')) : $fp_post_count = ot_get_option('frontpage_post_count'); else : $fp_post_count = -1; endif;
ot_get_option already has “default” value: of_get_option($id,$default);
so you could just write
ot_get_option('frontpage_post_count',-1)
I’m writing this in case someone missed it, I think in first version of OT it wasn’t working like this.
Cheers
purethemes said
so you could just writeot_get_option('frontpage_post_count',-1)![]()
I’m writing this in case someone missed it, I think in first version of OT it wasn’t working like this.
Cheers
+1 – That function works just like get_theme_mod and get_option.
Thanks for the replies.
@Ivor – I do understand your point of view, but sometimes you can save a lot of time for a confused customer who just don’t understand what’s wrong in the string format he or she filled in. But in general I really agree with what you have said.
I’m surprised that such a nice framework does not include validation and more flexibility. I was surprised that there is no date picker as well. But bottom line it satisfied.
