Settings page "switch" field - hopefully quick question

Support MB Settings Page Settings page "switch" field - hopefully quick questionResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #26785
    KevinKevin
    Participant

    Is there a way to trigger a PHP function when the value in the database from the saved field value is 1?

    I'm familiar with how the custom fields work but this is the first time I have used a settings page.

    Basically what I want to do is trigger PHP function when they turn something on.
    An example setting I have is to disable the block editor in WP, so if that is turned on, I would need to trigger the PHP function to load as long as that is on.

    This is the code I'm working with but not really sure if this is correct. Any tips on this would be super helpful.

    $value = rwmb_meta( switch_sb90fcsksws );
    // If field is on.
    if ( $value ) {
    // would I put my whole function in here or would this code need to be part of the function (sorry).
    }
    // If field is off.
    else {
    
    }
    #26794
    Long NguyenLong Nguyen
    Moderator

    Hi Kevin,

    Thank you for reaching out.

    You can hook a function to the action rwmb_{$meta_box_id}_after_save_post with the latest meta box register on the settings page. After saving the value of this meta box, the custom function will be fired.

    Get more details in the documentation
    https://docs.metabox.io/actions/#rwmb_after_save_post
    https://docs.metabox.io/extensions/mb-settings-page/#getting-field-value

    #26802
    KevinKevin
    Participant

    Sorry to ask, but do you know of any examples like this?
    I'm not following what exactly to do.

    It's just something I wanted to do is build a plugin with some PHP codes that help with some things like security and disabling things that most of my clients don't want, so my goal is to have a plugin with a settings panel that some of these things can be turned on or off.

    I am not a full stack developer and know enough about PHP to get me in trouble, but I do create my own plugins and have done quite few neat things, but I honestly (even after reading the documentation - which is meant for developers) do not know what to do with that.

    Not your fault, and this may be more advanced than some other things I have done, but usually once I see a full example of something like what I want to do then I can understand how to do it myself.

    If you can't provide that level of support that is fine but just thought I would ask about it.

    I still love Metabox so far.

    Also, is there any way to get support via email so all my conversations are private? I'm not sure I like the forum thing for support too much (I have no other premium plugins that do this so this is strange for me).

    Thanks

    #26808
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Here is an example:

    1. The code which I use to create a settings page and a meta box with a switch field
    2. add_filter( 'mb_settings_pages', 'custom_settings_page' );
      
      function custom_settings_page( $settings_pages ) {
          $settings_pages[] = [
              'menu_title'  => __( 'Options', 'project-name' ),
              'option_name' => 'my_options',
              'id'        => 'my_options',
              'position'    => 25,
              'columns'     => 1,
              'icon_url'    => 'dashicons-admin-generic',
          ];
      
          return $settings_pages;
      }
      
      add_filter( 'rwmb_meta_boxes', 'custom_sitewide_fields_group' );
      
      function custom_sitewide_fields_group( $meta_boxes ) {
          $prefix = '';
      
          $meta_boxes[] = [
              'title'          => __( 'My Meta Box', 'project-name' ),
              'id'             => 'my-meta-box',
              'settings_pages' => 'my_options',
              'fields'         => [
                  [
                      'id'        => 'enable_slider',
                      'name'      => 'Enable Slider?',
                      'type'      => 'switch',
                      'style'     => 'rounded',
                      'on_label'  => 'Yes',
                      'off_label' => 'No',
                  ],
              ],
          ];
          return $meta_boxes;
      }

      See more on docs https://docs.metabox.io/extensions/mb-settings-page/
      https://docs.metabox.io/fields/switch/

    3. The code which I use to create a custom function and hook to the action rwmb_{$meta_box_id}_after_save_post, the function will be fired after you click Save Settings on the settings page.
    add_action( 'rwmb_my-meta-box_after_save_post', function() {
        $enable_slider = rwmb_meta( 'enable_slider', ['object_type' => 'setting'], 'my_options' );
            
        if( $enable_slider ) {
            // function goes here
            echo "Enabled";
        } else {
            // function goes here
            echo "Disabled";
        }
    } );

    Hope that makes sense.

    #26863
    KevinKevin
    Participant

    Thanks. That does make more sense. I'll have a play with this over the weekend and get back to you if I have further questions.

    Thanks for the nice example 🙂

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