{"id":17852244,"date":"2024-06-26T17:12:15","date_gmt":"2024-06-26T17:12:15","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/php-error-undefined-array-key-block\/"},"modified":"2024-06-26T17:23:36","modified_gmt":"2024-06-26T17:23:36","slug":"php-error-undefined-array-key-block","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/php-error-undefined-array-key-block\/","title":{"rendered":"Php error &#8211; Undefined array key &#8220;block&#8221;"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I have php log error like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2024\/06\/26 18:26:17 &#091;error] 231349#231349: *424966 FastCGI sent in stderr: \"PHP message: PHP Warning:  Undefined array key \"block\" in \/var\/www\/test\/wp-content\/plugins\/javascript-notifier\/javascript-notifier.php on line 74\" while reading upstream, client: IP, server: test.test, request: \"GET \/blablabla\/ HTTP\/2.0\", upstream: \"fastcgi:\/\/unix:\/var\/run\/php\/php8.3-fpm.sock:\", host: \"blablabla\"<br \/><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The error message indicates that the array key <code>block<\/code> is being accessed in the plugin&#8217;s code without first checking if it exists. Maybe to prevent the PHP warning, you can add a check to ensure that the <code>block<\/code> key is defined before using it. Here is the modified part of the code in <code>javascript-notifier.php<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'wp_footer',<br \/>        function() {<br \/>                $settings = get_option( 'javascript_notifier_settings' );<br \/>                $block = isset($settings&#091;'block']) ? $settings&#091;'block'] : '';<br \/>                $style = 'style=\"background-color:' . $settings&#091;'bg_color'] . ';color:' . $settings&#091;'fg_color'] . ';font-size:' . $settings&#091;'font_size'] . '%;opacity:' . $settings&#091;'opacity'] . ';' . $settings&#091;'custom_css'] . '\"';<br \/>?&gt;<br \/>&lt;!-- JavaScript Notifier --&gt;<br \/>&lt;?php if( $block ) : ?&gt;&lt;div class=\"javascript_notifier\" id=\"javascript_notifier_block\" &lt;?php echo $style; ?&gt;&gt;&lt;div id=\"javascript_notifier_block_2\"&gt;&lt;div id=\"javascript_notifier_block_3\"&gt;&lt;?php else : ?&gt;&lt;div class=\"javascript_notifier\" id=\"javascript_notifier_bar\" &lt;?php echo $style; ?&gt;&gt;&lt;?php endif; ?&gt;&lt;strong&gt;&lt;?php echo $settings&#091;'message']; ?&gt;&lt;\/strong&gt;&lt;\/div&gt;&lt;?php if( $block ) : ?&gt;&lt;\/div&gt;&lt;\/div&gt;&lt;?php endif; ?&gt;<br \/>&lt;script id=\"hide-javascript-notifier-js\" type=\"application\/javascript\"&gt;<br \/>document.getElementById('javascript_notifier_&lt;?php echo( $block ? 'block' : 'bar' ); ?&gt;').style.setProperty('display', 'none', 'important');<br \/>&lt;\/script&gt;<br \/>&lt;!-- End JavaScript Notifier --&gt;<br \/>&lt;?php<br \/>        }, 1 );<br \/><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By adding <code>isset($settings['block']) ? $settings['block'] : ''<\/code>, we can ensure that the <code>$block<\/code> variable is set to an empty string if the <code>block<\/code> key is not defined in the <code>$settings<\/code> array. This will prevent the PHP warning from being triggered. I tested it and it works for me, but maybe review it as I am not a developer and maybe there is other solution for that.<\/p>\n","protected":false},"template":"","class_list":["post-17852244","topic","type-topic","status-publish","hentry","topic-tag-php-error"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17852244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":1,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17852244\/revisions"}],"predecessor-version":[{"id":17852263,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17852244\/revisions\/17852263"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=17852244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}