Trying to run code in a plugin only if a checkbox is ticked

Support MB Settings Page Trying to run code in a plugin only if a checkbox is tickedResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #22211
    SridharSridhar
    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?

    #22212
    SridharSridhar
    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 NguyenLong 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 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.