Set Option Value after click on button

Support General Set Option Value after click on buttonResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #43773
    goninski@gmail.com[email protected]
    Participant

    Hi there

    I'm trying to set a field value of a setting page (after clicking a button in a post, all within admin)

    I tried to use this action filter via functions.php:

    
    add_action( 'init', function() {
        rwmb_set_meta( 'name_of_my_metabox_setting_page, 'my_field', 999, ['object-type' => 'setting'] );
    }, 99 );
    

    (999 = test post id value which should be set for this field)

    But nothing happens, the (numeric) field on the setting page remains empty.

    Is this the right way to do? if so, why it is not working?

    and how can this then be implemented to fire after clicking a button (metabox field)?
    ..is this only possible with JS? if so, I see there is some documentation for the button, but then where you write '// do something' what do I need to call in order to call this hook?

    Thanks in advance for some hints
    Best regards
    François

    #43774
    PeterPeter
    Moderator

    Hello,

    I see there are 3 errors in your code:

    1. Use the name or ID of the settings page, it must be the option name.

    2. Missing the close single quote for the first argument.

    3. The dash character "object-type" in the fourth argument must be underscore.

    add_action( 'init', function() {
        rwmb_set_meta( 'option_name', 'my_field', 999, ['object_type' => 'setting'] );
    }, 99 );

    Following the documentation https://docs.metabox.io/extensions/mb-settings-page/
    https://docs.metabox.io/functions/rwmb-set-meta/

    how can this then be implemented to fire after clicking a button (metabox field)?

    Is the page reloaded after clicking on the button? If not, you should use a JS code with Ajax call. If yes, you can try to hook the callback function to the action save_post.

    #43794
    goninski@gmail.com[email protected]
    Participant

    Hi there

    Thanks for your fast reply and explanations.

    I could only find 1 bug in the (#3 understore). After that the code did work.
    Thanks for this.

    The firing of the code can have page reload but not a save, so the save-post action is not suitable.
    So I guess there is no other option than JS?

    #43798
    PeterPeter
    Moderator

    Hello,

    Yes, you have to use JS code to do something when clicking a button without reloading the page.

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