php warnings and solution
-
Hi there,
Plugin creates some warnings as below.
The code is trying to access array keys ‘type’ and ‘rows’ without checking if they exist first.Warning: Undefined array key "type" in /wp-content/plugins/gtm-consent-mode-banner/src/Service/SettingsService.php on line 417
Warning: Undefined array key "rows" in /wp-content/plugins/gtm-consent-mode-banner/src/Service/SettingsService.php on line 399Suggested code
public function textareaField( $args ): void {
// Get the value of the setting we've registered with register_setting()
$value = $args['value'] ?? get_option( $args['label_for'] );
$rows = isset($args['rows']) ? (int)$args['rows'] : 5; // Default to 5 rows if not specified
?>
<textarea
id="<?php echo esc_attr( $args['label_for'] ); ?>"
class="large-text code"
rows="<?php echo esc_html( $rows ); ?>"
placeholder="<?php echo esc_html( @$args['placeholder'] ); ?>"
name="<?php echo esc_attr( $args['label_for'] ); ?>"><?php echo wp_kses($value, SanitizationUtil::WP_KSES_ALLOWED_HTML, SanitizationUtil::WP_KSES_ALLOWED_PROTOCOLS); ?></textarea>
<?php if (isset($args['description'])) : ?>
<p class="description">
<?php echo esc_html( $args['description'] ); ?>
</p>
<?php endif; ?>
<?php
}
public function inputField( $args ): void {
// Get the value of the setting we've registered with register_setting()
$value = $args['value'] ?? get_option( $args['label_for'] );
$type = isset($args['type']) ? $args['type'] : 'text'; // Default to text if not specified
?>
<input
id="<?php echo esc_attr( $args['label_for'] ); ?>"
class="large-text code"
type="<?php echo esc_html( $type ); ?>"
<?php if (true === @$args['disabled']) : ?>
disabled="disabled"
<?php endif; ?>
value="<?php echo esc_html($value); ?>"
placeholder="<?php echo esc_html( @$args['placeholder'] ); ?>"
name="<?php echo esc_attr( $args['label_for'] ); ?>" />
<?php if (isset($args['description'])) : ?>
<p class="description">
<?php echo esc_html( $args['description'] ); ?>
</p>
<?php endif; ?>
<?php
}
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.