close
Skip to content

Fix: Search block doesn't respect background and text color options#70074

Draft
Rishit30G wants to merge 3 commits intoWordPress:trunkfrom
Rishit30G:feat/add-search-input-colors
Draft

Fix: Search block doesn't respect background and text color options#70074
Rishit30G wants to merge 3 commits intoWordPress:trunkfrom
Rishit30G:feat/add-search-input-colors

Conversation

@Rishit30G
Copy link
Contributor

What?

Closes #65343

Why?

This PR adds support for applying text and background colors to the input field in the search block, and also introduces separate text and background color options for the search button.

How?

  • This PR firstly adds the support for button in the block.json
  • Added the get_color_classes_for_button_block_core_search for adding the button color functionality and updated an existing function name to get_color_classes_for_input_block_core_search
  • Added getPresetColorClass function for adding the relevant text and background colour classes on the editor side

Testing Instructions

  • Add a new post
  • Add the Search Block
  • Go the right panel and visit the styles tab
  • Click on the ellipsis for the 'Color' option and select 'Button' from it
  • Now try to change the colours of the 'Text' , 'Background' and 'Button' to notice changes in colours of the input and search button

Screenshots or screencast

Screen.Recording.2025-05-07.at.2.38.32.PM.mov

Comment on lines +156 to +158
const bg = attributes.style?.elements?.button?.color?.background;
const text = attributes.style?.elements?.button?.color?.text;
const gradientBg = attributes.style?.elements?.button?.color?.gradient;
Copy link
Contributor Author

@Rishit30G Rishit30G May 7, 2025

Choose a reason for hiding this comment

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

I'm not sure if this would be the best way to get the colors, any alternatives would be appreciated!

Comment on lines +619 to +679
function get_color_classes_for_button_block_core_search( $attributes ) {
$classnames = array();

$color_text_name = '';
$color_background_name = '';
$color_gradient_name = '';

if (
isset( $attributes['style']['elements']['button']['color']['text'] ) &&
is_string( $attributes['style']['elements']['button']['color']['text'] )
) {
$full_value_text = $attributes['style']['elements']['button']['color']['text'];
if ( strpos( $full_value_text, '|' ) !== false ) {
$parts = explode( '|', $full_value_text );
$color_text_name = end( $parts );
}
}

if (
isset( $attributes['style']['elements']['button']['color']['background'] ) &&
is_string( $attributes['style']['elements']['button']['color']['background'] )
) {
$full_value_background = $attributes['style']['elements']['button']['color']['background'];
if ( strpos( $full_value_background, '|' ) !== false ) {
$parts = explode( '|', $full_value_background );
$color_background_name = end( $parts );
}
}

if (
isset( $attributes['style']['elements']['button']['color']['gradient'] ) &&
is_string( $attributes['style']['elements']['button']['color']['gradient'] )
) {
$full_value_gradient = $attributes['style']['elements']['button']['color']['gradient'];
if ( strpos( $full_value_gradient, '|' ) !== false ) {
$parts = explode( '|', $full_value_gradient );
$color_gradient_name = end( $parts );
}
}

// Add final classes.
if ( ! empty( $color_text_name ) ) {
$classnames[] = 'has-' . sanitize_title( $color_text_name ) . '-color';
} elseif ( ! empty( $full_value_text ) ) {
$classnames[] = 'has-text-color';
}

if ( ! empty( $color_background_name ) ) {
$classnames[] = 'has-' . sanitize_title( $color_background_name ) . '-background-color';
} elseif ( ! empty( $full_value_background ) ) {
$classnames[] = 'has-background';
}

if ( ! empty( $color_gradient_name ) ) {
$classnames[] = 'has-' . sanitize_title( $color_gradient_name ) . '-gradient-background';
} elseif ( ! empty( $full_value_gradient ) ) {
$classnames[] = 'has-gradient';
}

return implode( ' ', $classnames );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function gets the job done, but I believe there’s a better or more efficient way to implement it

@carolinan
Copy link
Contributor

I don't think this is the correct way to solve this.

Personally I would prefer this to be managed by adding the support for form elements to the elements in global styles.
Then we would get these options as separate color controls "for free" with minor changes.

#34198

@Rishit30G
Copy link
Contributor Author

Thanks for sharing the suggestion @carolinan!

I was going with the approach shared here #65343 (comment)

Then we would get these options as separate color controls "for free" with minor changes.

Interesting, I'll need to give this discussion a read 👍🏻

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.

Search block doesn't respect background and text color options

2 participants