Tagged: 

Viewing 4 reply threads
  • Author
    Posts
    • #6505
      ytilis
      Member

      I’m trying to make an error notice that’ll pop up if my plugin finds that some required settings fields are empty, but I don’t want the notice to appear if they’re populated correctly.

      Followed the tutorial on notices (https://piklist.com/user-guide/docs/notices/) and it all worked great except that there’s no way to hide the notice if it’s not needed. I have logic in the notice to only show a message if we need to, and that works fine, but the notice container itself still shows up, it’s just empty.

      This can be reproduced by making a new notice and only putting the notice headers into the file, without a body. You’ll see a single pixel height empty notice container.

      Is there any way Piklist can check to make sure the notice isn’t an empty string before rendering the block for it to the page?

    • #6525
      Steve
      Keymaster

      @ytilis– How can you post you code on how you made the admin notice conditional?

    • #6526
      ytilis
      Member

      Sure. I’ve pasted the anonymized code below:

      <?php
      /*
          Notice ID: my_requirements
          Notice Type: error
          Role: administrator
      */
      
          $settings = get_option('my_settings');
      
          $admin_messages = [];
      
          if( !isset($settings['key']) || $settings['key'] == '' ){
              $admin_messages[] = "A <strong>Key</strong> is required";
          }
      
          if( !isset($settings['secret']) || $settings['secret'] == '' ){
              $admin_messages[] = "An <strong>Secret</strong> is required";
          }
      
          if(count($admin_messages) > 0) {
              $messages = '';
              foreach ($admin_messages as $message) {
                  $messages .= sprintf("<li>%s</li>", $message);
              }
      
              ?><p><strong>Warning!</strong> The following required settings for My Plugin are unset:</p>
              <ul>
                  <?php echo $messages; ?>
              </ul><?php
          }?>

      This works great except when I no longer have any errors I need to show because the admin has filled out the required settings, in which case the notice just shows up empty. If there’s a better way to do what I’m trying to accomplish, or if I missed a step somewhere, feel free to let me know.

      Thanks

    • #6527
      Steve
      Keymaster

      This sort of logic doesn’t really belong in this file. I suggest you check out the piklist_part_process_callback-notices filter. Place this in your plugin file or your theme’s functions.php file. You should probably move your code into this function.

      add_filter('piklist_part_process_callback-notices', 'no_notice', 10);
      public static function no_notice($_part)
      {
        print_r($_part);
      
        return $_part;
      }
    • #6528
      ytilis
      Member

      Ah, gotcha. That does present me with the problem that I don’t have a way of passing the missing fields to the notice itself, but I can mitigate that by making the messaging more generic, and by setting those fields to required on the settings page, so I still have a way of showing what’s missing that way.

      Thanks

Viewing 4 reply threads
  • You must be logged in to reply to this topic.