Skip to content

Change 'checked' to 'selected' for select list options#843

Merged
superdav42 merged 1 commit intoUltimate-Multisite:mainfrom
commercial-hippie:commercial-hippie-patch-select-lists
Apr 14, 2026
Merged

Change 'checked' to 'selected' for select list options#843
superdav42 merged 1 commit intoUltimate-Multisite:mainfrom
commercial-hippie:commercial-hippie-patch-select-lists

Conversation

@commercial-hippie
Copy link
Copy Markdown
Contributor

@commercial-hippie commercial-hippie commented Apr 14, 2026

Pre-selected values don't show correctly in the checkout forms, currently printing out "checked" instead of "selected".

Summary by CodeRabbit

  • Bug Fixes
    • Fixed select dropdown fields in checkout to properly display selected options during form submission.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2026

📝 Walkthrough

Walkthrough

A template file in the checkout fields module was updated to use the correct WordPress helper function for marking selected options in a select field, changing from checked() to selected() for proper HTML semantics.

Changes

Cohort / File(s) Summary
Select Field Template
views/checkout/fields/field-select.php
Updated HTML attribute logic for <option> elements to use selected() helper function instead of checked() for both placeholder and regular options.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 With whiskers twitched and nose held high,
A select field fixed—oh my, oh my!
From "checked" to "selected," the right way now,
HTML semantics bow-wow-bow!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change in the pull request: replacing 'checked' with 'selected' for select list options to fix HTML attribute semantics.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@views/checkout/fields/field-select.php`:
- Line 45: The placeholder <option> currently lacks a value attribute and uses
selected(! $field->value) which mis-selects when $field->value is a falsy but
valid value (e.g., "0"); update the option to include an explicit value (e.g.,
value="") and replace the boolean-negation selected call with the two-argument
comparison selected($field->value, '') so selection only occurs when the actual
field value equals the empty placeholder; target the <option> line that echoes
esc_html($field->placeholder) and the selected(...) invocation to make this
change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 14daeb39-43a0-47d4-8b06-d5718b56b554

📥 Commits

Reviewing files that changed from the base of the PR and between 9e53593 and b8229cb.

📒 Files selected for processing (1)
  • views/checkout/fields/field-select.php

<?php if ($field->placeholder) : ?>

<option <?php checked(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
<option <?php selected(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether this pattern appears elsewhere and should be corrected consistently.
rg -nP --type=php -C2 'selected\(\s*!\s*\$[A-Za-z_][A-Za-z0-9_>\-\[\]\'"]*\s*\)'

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 149


🏁 Script executed:

cat -n views/checkout/fields/field-select.php | head -70

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 2037


🏁 Script executed:

# Search for selected() calls with boolean operators, using simpler pattern
rg -n --type=php 'selected\(' views/checkout/fields/ -A1 -B1

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 544


Add explicit value attribute to placeholder option and fix falsy comparison

Line 45 uses selected(! $field->value) which incorrectly selects the placeholder when $field->value is a falsy but valid value like the string '0'. Additionally, the placeholder option lacks a value attribute. Use proper two-argument comparison instead:

Suggested fix
-	<option <?php selected(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
+	<option value="" <?php selected('', (string) $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<option <?php selected(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
<option value="" <?php selected('', (string) $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@views/checkout/fields/field-select.php` at line 45, The placeholder <option>
currently lacks a value attribute and uses selected(! $field->value) which
mis-selects when $field->value is a falsy but valid value (e.g., "0"); update
the option to include an explicit value (e.g., value="") and replace the
boolean-negation selected call with the two-argument comparison
selected($field->value, '') so selection only occurs when the actual field value
equals the empty placeholder; target the <option> line that echoes
esc_html($field->placeholder) and the selected(...) invocation to make this
change.

@superdav42 superdav42 merged commit 2b88eda into Ultimate-Multisite:main Apr 14, 2026
9 of 11 checks passed
@superdav42
Copy link
Copy Markdown
Collaborator

@commercial-hippie Thank you so much for this catch — this is a fantastic contribution! This bug has been lurking in the codebase since the very first commit (November 2024) and survived through seven subsequent commits over 16 months without anyone noticing. Using checked() instead of selected() on <option> elements meant the select fields were never properly reflecting their selected state in the HTML output.

Really appreciate you taking the time to find this, fix it, and submit the PR. Merged!

We'll follow up with a small additional improvement based on CodeRabbit's review (adding an explicit value="" on the placeholder option and tightening the falsy comparison), but your fix is the one that matters.

Thanks again for contributing to Ultimate Multisite!

superdav42 added a commit that referenced this pull request Apr 14, 2026
The placeholder <option> used selected(! $field->value) which incorrectly
selected the placeholder when $field->value was a falsy but valid value
like '0'. Replace with two-argument selected('', (string) $field->value)
and add an explicit value="" attribute.

Follow-up to PR #843 — based on CodeRabbit review suggestion.
superdav42 added a commit that referenced this pull request Apr 14, 2026
…ute (#859)

The placeholder <option> used selected(! $field->value) which incorrectly
selected the placeholder when $field->value was a falsy but valid value
like '0'. Replace with two-argument selected('', (string) $field->value)
and add an explicit value="" attribute.

Follow-up to PR #843 — based on CodeRabbit review suggestion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants