Forum Replies Created
-
AuthorPosts
-
angeliqueMemberStill trying to figure out this thing.
I’m trying to turn the date into a timestamp, hooking on “save_post”. Basically, when the event is saved, the start-date and end-date are translated into timestamp and saved into the post-meta fields “timestamp-start-date” and “timestamp-end-date”.
In my metaboxes for events, I added two hidden fields.Now, the issue I have is:
– if I put the two hidden fields (named “timestamp-start-date” and “timestamp-end-date”), the timestamps are NOT saved (I wonder if they could be overridden with a null value after my function has just set them, but supposedly save_post is called AFTER the post has been saved).
– if I remove those hidden fields, the timestamps appear nicely in the database (but of course, disappear from the picklist form).I guess I could do without the hidden fields, but does that mean I need to destroy those timestamps when an event is deleted? Or does WP remove all “post_meta” associated with the deleted post?
Here is my code:
The hidden fields:
piklist('field', array( 'type' => 'hidden' ,'field' => 'start-date-timestamp' )); piklist('field', array( 'type' => 'hidden' ,'field' => 'end-date-timestamp' ));The hook to save the timestamp:
function save_events_meta( $post_id, $post, $update ) { $start_date = $_REQUEST['_post_meta']['event_dates']['start-date'][0]; $end_date = $_REQUEST['_post_meta']['event_dates']['end-date'][0]; if ( isset( $start_date ) ) { update_post_meta( $post_id, 'start-date-timestamp', strtotime($start_date)); } if ( isset( $end_date ) ) { update_post_meta( $post_id, 'end-date-timestamp', strtotime($end_date)); } } add_action( 'save_post_events', 'save_events_meta', 10, 3 );To sum-up: “save_events_meta” works if I don’t add the hidden field to the event metaboxes. Stop working if I put back the hidden fields.
Any help welcome 🙂
angeliqueMemberHello Kattagami,
It would be great if you could share the code! I’m not too sure how to proceed and it would help tremendously to have some code that already work as a reference!
angeliqueMemberThank you everyone 🙂
Well, it makes sense to store a date as a String if all you do is to display it. If you need those dates to do something, like comparing/sorting them, it’s a bit less convenient. I wouldn’t mind storing a datetime object and formatting it at display, but it’s not convenient with Piklist: the “dateformat” you choose for picklist is the format the date is going to be displayed in the html form after selecting the date in the calendar. Not super user friendly 🙂Anyway, this is the code I use the request sorting by start date:
/* Sort the events to appear sorted by starting date */ function get_events( $query ) { if ( is_post_type_archive('events') && $query->is_main_query()) { $query->query_vars['meta_key'] = 'starting_on'; $query->query_vars['orderby'] = 'meta_value'; $query->query_vars['order'] = 'ASC'; } return $query; } add_filter( 'pre_get_posts', 'get_events' );If I follow you, Steve, I should use a custom function here to sort my custom posts (= events) ?
Though if I do, each time the page is requested, if posts date is going to be processed for the sorting. Isn’t is consuming?
angeliqueMemberIndeed. It works now.
PiklistHelper has also been updated so now everything work perfect.
Thank you for your help.
angeliqueMemberNo, it doesn’t. I comment out PiklistHelper by default to test.
angeliqueMemberThank you for your answer.
No luck though. This change does allow the e-mail validation to work (as long as I don’t put back PickListHelper) but the new rule itself doesn’t work. I tried with both snippets: the one based on my RegEx above and the one from the updated documentation. None of them works for me.
Here is a new test plugin.
angeliqueMemberHello Steve,
Thank you for the tests you made.
I’ve been investigating a bit further after you gave me the idea of testing with the demos.
I’ve been able to narrow down the issue.
Actually, the email validation breaks (along with some other validation rules but not all of them) as soon as a custom validation is added.
– it breaks when I add myself a new rule
– it breaks when I add Jason’s PiklistHelper.php class, which add validation rules too. (https://github.com/JasonTheAdams/PiklistHelper/blob/master/PiklistHelper.php)So now I wonder if there is something wrong in my plugin “base” file or if it’s a bug?
You’ll find my basic test plugin attached.
To reproduce the bug:
– Make a fresh install of WordPress
– Add and activate the attached plugin (it will ask to add Piklist)
– Install the piklist demo
– Piklist Demo (custom type) > Add New > Validation
– Try to enter an invalid string in “email” and submit. You won’t get an error message for the email (you will for the textfield and some checkbox). Note that you should have more error messages actually (more things shouldn’t validate).Hope you can help me with this!
angeliqueMemberOk, I’ve been investigating a bit. Here is what I got so far:
First, I had made a mistake in my custom validation declaration (my validation rule was called “html”. I called it “valid_float”. I fixed this, but it didn’t make it work.
What did was to increase the priority parameter when calling the “add_filter” function. The documentation usually show “11”, but it only worked when I put it to 12. I don’t know why.add_filter('piklist_validation_rules', 'check_valid_float', 12); function check_valid_float() { $validation_rules = array( 'valid_float' => array( 'rule' => "/^[1-9]+(.\d+)?(.\d+)?/" ,'message' => __('is not a valid number.') ) ); return $validation_rules; }Now, the numeric validation works on custom post forms BUT:
– it doesn’t work at all on setting forms.
– it doesn’t work anymore if I declare another validation rule (with another name and function name, of course).Any help is welcome, really. Or a workaround for validating piklist forms another way.
angeliqueMemberDid you ever manage to make it work? Any clue about what could cause them to always pass?
angeliqueMemberOops, I didn’t see that :/
Thank you very much for your quick answer. It works perfect now.
angeliqueMemberThank you very much Steve.
I have another question. I’m trying to make those settings accessible for editors and above (admin and super admin)… with no luck. I’m sure it’s just a setting I don’t do right, but I can’t find where I’m wrong.
Here is my setting menu declaration:
add_filter('piklist_admin_pages', 'piklist_authorkit_setting_pages'); function piklist_authorkit_setting_pages($pages) { $pages[] = array( 'page_title' => __('Author Settings') ,'menu_title' => __('Author Settings', 'piklist') ,'capability' => 'edit_pages' ,'menu_slug' => 'aboutme_settings' ,'setting' => 'aboutme_settings' ,'menu_icon' => plugins_url('piklist/parts/img/piklist-icon.png') ,'page_icon' => plugins_url('piklist/parts/img/piklist-page-icon-32.png') ,'single_line' => true ,'default_tab' => 'Social Media' ,'save_text' => 'Save About Me Settings' ); return $pages; }And here is one of the tab inside:
<?php /* Title: Google Analytics Tab: Google Analytics Setting: aboutme_settings Capability: edit_pages Tab Order: 40 */ piklist('field', array( 'type' => 'text' ,'field' => 'google_analytics_id' ,'label' => __('Traking ID') ,'value' => '' ,'attributes' => array( 'class' => 'text' ) ));As a result of this, the setting pages are properly displayed for editors and above, but editors still can’t submit those setting page forms (“cheating, eh?” message). Admins can.
What am I doing wrong?
angeliqueMemberOh, ok! It works perfectly, thank you very much!
And now I understand why the arrays : all entries info are aggregated by fields. I was thinking the other way around, more object-oriented of json-like, where each entry would contain its own info.
It’s crystal clear, now, thank you very much for your help!
angeliqueMemberI’d like to achieve a list of ‘shops’ where each contains the three following fields:
1) a select field ‘shop_id’
2) a set of three checkboxes (ebook, paper, audiobook): the ‘format’
3) a text field ‘url’You can add as many ‘shop’ as you want and all three fields are required for each new shop.
-
AuthorPosts