Custom Fields
Re-Envisioned
Support MB Settings Page Trying to run code in a plugin only if a checkbox is ticked

  • Creator
    Topic
  • #22211
    Resolved Sridhar
    Participant

    Hello.

    I am working on a custom plugin in which I would like to run certain code only if a checkbox field created using MB is ticked.

    Code for the Settings page and fields: https://pastebin.com/raw/4QSjwZSr

    Desired conditional code: https://pastebin.com/raw/qjT0bDCh

    The checkbox field has been set to be ticked by default.

    I am looking to run the code only if it is ticked.

    What is happening currently is that the code is not running all the time i.e, as if the checkbox is not ticked.

    Any idea?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #22212
    Sridhar
    Participant

    I think the issue is that when Save Changes is not clicked even once soon after the plugin has been activated, the value is null and I need to check for that in the code.

    #22213
    Long Nguyen
    Moderator

    Hi Sridhar,

    You are right, before clicking the button Save Settings, the fields’ value is not saved to the database so the helper function rwmb_meta() always returns a null value.

    If you want to set the field kn_checkbox_kadence checked as default, please try to use this code:

    add_action( 'admin_init', function() {
        if( get_option( 'kadence_navigator' ) !== null ) {
            $options = array( 
                'kn_checkbox_kadence' => 1
            );
            update_option( 'kadence_navigator', $options );
        }
    } );
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.