Conversation
supportFor some reason I am unable to reproduce, get_option('duplicate_page_options') will sometimes return false, and generate a bunch of warnings about offset on false. PHP Warning: Trying to access array offset on false in /var/www/html/wp-content/plugins/duplicate-page/inc/admin-settings.php on line 60 PHP Warning: Trying to access array offset on false in /var/www/html/wp-content/plugins/duplicate-page/inc/admin-settings.php on line 61 PHP Warning: Trying to access array offset on false in /var/www/html/wp-content/plugins/duplicate-page/inc/admin-settings.php on line 62 ... This patch adds an empty value, and uses the WordPress native selected() index 46d1b28..254a4e7 100644 --- a/inc/admin-settings.php +++ b/inc/admin-settings.php @@ -27,6 +27,14 @@ if (current_user_can('manage_options') && isset($_POST['submit_duplicate_page']) endif; $opt = get_option('duplicate_page_options'); +if (!is_array($opt)) { + $opt = array( + "duplicate_post_editor" => '', + "duplicate_post_status" => '', + "duplicate_post_redirect" => '', + "duplicate_post_suffix" => '' + ); +} if (!empty($msg) && $msg == 1): _e('<div class="updated settings-error notice is-dismissible" id="setting-error-settings_updated"> <p><strong>Settings saved.</strong></p><button class="notice-dismiss button-custom-dismiss" type="button"><span class="screen-reader-text">Dismiss this notice</span></button></div>','duplicate-page'); @@ -46,9 +54,9 @@ endif; <th scope="row"><label for="duplicate_post_editor"><?php _e('Choose Editor', 'duplicate-page'); ?></label></th> <td> <select id="duplicate_post_editor" name="duplicate_post_editor"> - <option value="all" <?php echo (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'all') ? "selected = 'selected'" : ''; ?>><?php _e('All Editors', 'duplicate-page'); ?></option> - <option value="classic" <?php echo (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'classic') ? "selected = 'selected'" : ''; ?>><?php _e('Classic Editor', 'duplicate-page'); ?></option> - <option value="gutenberg" <?php echo (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'gutenberg') ? "selected = 'selected'" : ''; ?>><?php _e('Gutenberg Editor', 'duplicate-page'); ?></option> + <option value="all"<?php selected($opt['duplicate_post_editor'], 'all') ?>><?php _e('All Editors', 'duplicate-page'); ?></option> + <option value="classic"<?php selected($opt['duplicate_post_editor'], 'classic') ?>><?php _e('Classic Editor', 'duplicate-page'); ?></option> + <option value="gutenberg"<?php selected($opt['duplicate_post_editor'], 'gutenberg') ?>><?php _e('Gutenberg Editor', 'duplicate-page'); ?></option> </select> <p><?php _e('Please select which editor you are using.<strong> Default: </strong> Classic Editor', 'duplicate-page'); ?></p> </td> @@ -57,10 +65,10 @@ endif; <th scope="row"><label for="duplicate_post_status"><?php _e('Duplicate Post Status', 'duplicate-page'); ?></label></th> <td> <select id="duplicate_post_status" name="duplicate_post_status"> - <option value="draft" <?php echo($opt['duplicate_post_status'] == 'draft') ? "selected = 'selected'" : ''; ?>><?php _e('Draft', 'duplicate-page'); ?></option> - <option value="publish" <?php echo($opt['duplicate_post_status'] == 'publish') ? "selected = 'selected'" : ''; ?>><?php _e('Publish', 'duplicate-page'); ?></option> - <option value="private" <?php echo($opt['duplicate_post_status'] == 'private') ? "selected = 'selected'" : ''; ?>><?php _e('Private', 'duplicate-page'); ?></option> - <option value="pending" <?php echo($opt['duplicate_post_status'] == 'pending') ? "selected = 'selected'" : ''; ?>><?php _e('Pending', 'duplicate-page'); ?></option> + <option value="draft"<?php selected($opt['duplicate_post_status'], 'draft') ?>><?php _e('Draft', 'duplicate-page'); ?></option> + <option value="publish"<?php selected($opt['duplicate_post_status'], 'publish') ?>><?php _e('Publish', 'duplicate-page'); ?></option> + <option value="private"<?php selected($opt['duplicate_post_status'], 'private') ?>><?php _e('Private', 'duplicate-page'); ?></option> + <option value="pending"<?php selected($opt['duplicate_post_status'], 'pending') ?>><?php _e('Pending', 'duplicate-page'); ?></option> </select> <p><?php _e('Please select any post status you want to assign for duplicate post.<strong> Default: </strong> Draft','duplicate-page'); ?></p> </td> @@ -68,8 +76,8 @@ endif; <tr> <th scope="row"><label for="duplicate_post_redirect"><?php _e('Redirect to after click on <strong>Duplicate This Link</strong>', 'duplicate-page'); ?></label></th> <td><select id="duplicate_post_redirect" name="duplicate_post_redirect"> - <option value="to_list" <?php echo($opt['duplicate_post_redirect'] == 'to_list') ? "selected = 'selected'" : ''; ?>><?php _e('To All Posts List', 'duplicate-page'); ?></option> - <option value="to_page" <?php echo($opt['duplicate_post_redirect'] == 'to_page') ? "selected = 'selected'" : ''; ?>><?php _e('To Duplicate Edit Screen', 'duplicate-page'); ?></option> + <option value="to_list"<?php selected($opt['duplicate_post_redirect'], 'to_list') ?>><?php _e('To All Posts List', 'duplicate-page'); ?></option> + <option value="to_page"<?php selected($opt['duplicate_post_redirect'], 'to_page') ?>><?php _e('To Duplicate Edit Screen', 'duplicate-page'); ?></option> </select> <p><?php _e('Please select any post redirection, redirect you to selected after click on duplicate this link.<strong> Default: </strong>To All Posts List','duplicate-page'); ?></p> </td>
Thank you for sharing this and explaining the issue so clearly. The warnings you are seeing happen because get_option('duplicate_page_options') sometimes returns false , usually when the settings have not been saved yet or the option does not exist in the database. In that situation, trying to read values like $opt['duplicate_post_editor'] causes the “array offset on false” warnings. Your patch handles this well by making sure the option is always treated as an array, even when get_option() returns false. This prevents the warnings from appearing and keeps the settings page loading smoothly. Switching to the WordPress selected() helper is also a nice improvement. It makes the code cleaner and more reliable. We appreciate you taking the time to report this and provide such a clean fix. We will review it and include an update in an upcoming release. Thanks again for your contribution!
Thank you for sharing this and explaining the issue so clearly. The warnings you are seeing happen because get_option('duplicate_page_options') sometimes returns false , usually when the settings have not been saved yet or the option does not exist in the database. In that situation, trying to read values like $opt['duplicate_post_editor'] causes the “array offset on false” warnings. Your patch handles this well by making sure the option is always treated as an array, even when get_option() returns false. This prevents the warnings from appearing and keeps the settings page loading smoothly. Switching to the WordPress selected() helper is also a nice improvement. It makes the code cleaner and more reliable. We appreciate you taking the time to report this and provide such a clean fix. We will review it and include an update in an upcoming release. Thanks again for your contribution!