Support » Developing with WordPress » If checkbox is checked, do function (Admin Menu Option Page for a plugin)

  • Hi everyone,

    I’m in the process of developing a small plugin that would provide some basic function, like activate custom post if they are needed, or a coming soon page.
    I developed an option page in the backoffice with checkboxes, and I am desperatly trying to put condition to do things if these checkboxes are checked or not.

    Here is my actual code :

    /*
    * Choose if you want Coming Soon page or not
    */
    
    function creative_checkbox_field_1_render() { 
      $options = get_option( 'creative_settings' );?>
      <input type='checkbox' name='creative_settings[creative_checkbox_field_1]' <?php checked( $options['creative_checkbox_field_1'], 1 ); ?> value='1'>
      <?php
    }
    
      // If is checked, activate function to display coming soon page
      if( $options['creative_checkbox_field_1'] == '1' ) { 
          add_action('get_header', 'coming_soon_mode');
      } 
      else { 
          //do nothing
      }
    
    /*
    * Display Coming soon Page
    */
    
    function coming_soon_mode() {
      if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
          wp_die('Ce site est en cours de création. Revenez-vite nous voir !');
      }
    }

    I must precise that in the BO, when I check or uncheck my checkboxes, the option is saved.
    I tried multiple version of the code above, without any success.
    I read a lot of thread and forums without finding any solutions or what is actually wrong in my code 🙁
    The coming soon page function is working like a charm by it’s own (I tested it).

    The above code is not getting me any error, it just seems to not do anything…
    I want to use the same process for my other fields on my option page (but of course activating other functions).

    Any help would be highly appreciated, thanks ! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Your conditional add_action() always fails because it’s executed way too early, when your plugin is loaded. At that time, $options would have unreliable values because WP is still unstable. The adding an action based on an option value process needs to be in an action callback itself so it executes at the right time. The “init” action is usually good for this, but the best action may depend on what needs to be done for a particular option.

    The other problem with your conditional add_action() code is $options is never assigned a value which can be evaluated. You call get_option() when outputting your page, but values assigned within a function are not available outside of that function. You need to call get_option() within the scope of your conditional code.

    I don’t really get how I can do all that. Would you have some example for me ?

    This part of code I give is part of other things I have on the same options for my plugin, so If I move something just for this one (the coming soon checkbox), I’ll have to consider the rest as well.

    Like I said, it’s first time I do something like that and unfortunately I don’t have anyone to refer in case of trouble.
    I spend hours on forum and codex tro try to get it but just can’t 🙁

    • This reply was modified 4 months, 1 week ago by  redmorgane. Reason: typo
    Moderator bcworkz

    (@bcworkz)

    add_action('init', 'rm_handle_cb');
    function rm_handle_cb() {
      $options = get_option( 'creative_settings' );
    
      // If is checked, activate function to display coming soon page
      if( $options['creative_checkbox_field_1'] == '1' ) { 
          add_action('get_header', 'coming_soon_mode');
      } 
      else { 
          //do nothing
      }
      return;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.