select_advanced ajax value not saved
- This topic has 4 replies, 1 voice, and was last updated 4 years, 2 months ago by
Julien.
-
AuthorPosts
-
February 25, 2021 at 3:09 PM #24635
Julien
ParticipantHi,
I have a problem with the select_advanced with custom options load in ajax.
My ajax call is working well but when I choose a value and I click on "update", the page is saved properly except the value defined in the select.
I made a test on a fresh install of wordpress.
PHP 7.4.13
Wordpress : 5.6.2
Theme : Twenty Twenty-One 1.1
Meta Box : 5.3.8
Meta Box AIO : 1.13.4I use Classic Editor extension but with or without the plugin the problem still the same.
You will find my code here
Put this file on your theme and u can make a simple include in the functions.phpI tried with a custom sanitize_callback seen on this topic but problem still the same
You will find a record here
Thanks for your support.
February 25, 2021 at 3:23 PM #24636Julien
ParticipantAdditional informations
For this test, if Meta Box AIO disabled, problem still persist.
February 25, 2021 at 5:11 PM #24637Julien
ParticipantCorrection
After some research, the value is in database but the select is empty on the backend.So I found an hack but it's not really sexy.
You can find adaptation here
My "temporary" solution is setting 2 more fields
1 - html field for jquery script
2 - hidden field to option value for select2When select_advanced field change, hidden field is updated with values of select_advanced
When the page load, we populate select_advanced with the hidden fieldYou can maybe adapt "select_advanced" to detect when we are in ajax situation and populate with an hidden_field like I did ?
February 25, 2021 at 6:01 PM #24639Julien
ParticipantAnother solution (without ajax) is populate the options array in PHP but this code execute each loading page of WordPress (front and back).
So we need to make conditions to populate only when we edit page but in case we have a large set or the populate function is long to fill, it's not a good solution.
Sample code here
February 25, 2021 at 7:49 PM #24641Julien
ParticipantI found a more sexiest solution.
It's a continuation of my reply
Explanations:
I coded a custom field type based on select_advanced (see documentation).
This custom field type named "select_advanced_ajax"
The gist is in two part
1 - Creation of a Meta Box with a custom Class (MetaBoxClass.php)
2 - Our custom field type declaration (RWMB_Select_advanced_ajax_Field.php)When the field is saved, we add in the database the hidden field.
My code : https://gist.github.com/harkor/2fad09a642a096eb0a48984669211312
I didn't want create a javascript file for that because it's make a third file.
Feel free to adapt.
If the support team have a better solution give it to me 😀
-
AuthorPosts
- You must be logged in to reply to this topic.