最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Get Date of custom field and use conditional logic

programmeradmin1浏览0评论

I'm relatively new to Wordpress development and I'm trying to learn more about custom coding.

For starters, I would like to know how to retrieve the date of a custom field and use conditional logic on another date custom field.

For example: If the user chooses a date between Monday and Friday on customfield1, they will not be able to choose Saturday on customfield2.

I would really appreciate it if someone could guide me on doing so!

I'm relatively new to Wordpress development and I'm trying to learn more about custom coding.

For starters, I would like to know how to retrieve the date of a custom field and use conditional logic on another date custom field.

For example: If the user chooses a date between Monday and Friday on customfield1, they will not be able to choose Saturday on customfield2.

I would really appreciate it if someone could guide me on doing so!

Share Improve this question asked Aug 28, 2019 at 15:58 TerrisTerris 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

OK, so you've got a couple of ways to go about this, but first, you need to decide how urgently you want to inform the user that their value is invalid: immediately, or when the post is saved? A general rule of thumb would suggest that earlier is better, but given that you seem to suggest that you're new to custom coding, you may be forced to go later (it might be easier if you want to stick with Wordpress-specific coding). Here's the rundown in any case:

Immediate Validation

This can be done with Javascript/jQuery. Write some code that listens to each of the input's change events, then check if there's already a value in the other input. If so, run your validation logic, if not, wait.

Important note: this solution basically avoids Wordpress development all together. Not necessarily a bad thing, but worth noting.

Late Validation (on post save)

If you're happy to wait until the post is saved to perform validation, you can hook an action to run your validation code. If you're using Advanced Custom Fields, you can use the 'acf/validate_save_post' hook (ref: https://www.advancedcustomfields/resources/acf-validate_save_post/). If not, just use the core 'save_post' hook.

Alternate Immediate Validation (untested)

If you're using ACF, it looks like you can use a field-specific hook, like 'acf/validate_value/name=your_date_field_name' (ref: https://www.advancedcustomfields/resources/acf-validate_value/). Not around the event life-cycle of ACF well enough to know what's possible with this, so you'll need to look into it, but when the first field's validation event fires, you could store it's value (if you're developing a plugin, a static property would be best, if not, just a global variable) to check on when the second field's validation event fires.

发布评论

评论列表(0)

  1. 暂无评论