Tagged: notices
- This topic has 4 replies, 2 voices, and was last updated 5 years, 8 months ago by
ytilis.
-
AuthorPosts
-
-
May 17, 2016 at 7:12 pm #6505
ytilisMemberI’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?
-
May 20, 2016 at 1:34 pm #6525
-
May 20, 2016 at 3:22 pm #6526
ytilisMemberSure. 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
-
May 20, 2016 at 3:46 pm #6527
SteveKeymasterThis sort of logic doesn’t really belong in this file. I suggest you check out the
piklist_part_process_callback-noticesfilter. 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; } -
May 20, 2016 at 4:29 pm #6528
ytilisMemberAh, 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
-
-
AuthorPosts
- You must be logged in to reply to this topic.