Custom Fields
Re-Envisioned
Support General select_advanced ajax value not saved

  • Creator
    Topic
  • #24635
    Julien
    Participant

    Hi,

    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.4

    I 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.php

    I 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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #24636
    Julien
    Participant

    Additional informations

    For this test, if Meta Box AIO disabled, problem still persist.

    #24637
    Julien
    Participant

    Correction
    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 select2

    When select_advanced field change, hidden field is updated with values of select_advanced
    When the page load, we populate select_advanced with the hidden field

    You can maybe adapt "select_advanced" to detect when we are in ajax situation and populate with an hidden_field like I did ?

    See documentation of Select2

    #24639
    Julien
    Participant

    Another 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

    #24641
    Julien
    Participant

    I 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 😀

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.