Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button block: Allow for setting custom link class #14121

Open
webmandesign opened this issue Feb 26, 2019 · 11 comments
Open

Button block: Allow for setting custom link class #14121

webmandesign opened this issue Feb 26, 2019 · 11 comments
Labels
[Block] Buttons Affects the Buttons Block [Type] Enhancement A suggestion for improvement.

Comments

@webmandesign
Copy link
Contributor

Currently the custom block class is applied on the block container, which in Button block case is a <div class="wp-block-button my-custom-class"> (using my-custom-class as an example here).

However, as most themes style some sort of button class (such as .button or .btn) that could also be simply applied on links, we can not apply such class on the Button block link itself.

The desired Button block output HTML would then be (using my-custom-class as an example here):

<div class="wp-block-button">
	<a class="wp-block-button__link my-custom-class" href="...">...</a>
</div>

The best would be to allow theme developers to add such class via a filter hook maybe? But surely, probably a user input field would be beneficial too.

This was also discussed in #8971

@youknowriad youknowriad added the [Type] Enhancement A suggestion for improvement. label Feb 27, 2019
@klihelp
Copy link

klihelp commented Jul 11, 2019

The referenced ticket for button is closed:
#8971

There is no way to set the class to the anchor link inside the button block?

@webmandesign
Copy link
Contributor Author

@klihelp Depending on what exactly you need to do, you can use something like:

<?php

add_filter( 'render_block', function( $block_content, $block ) {

	// Button block additional class.
	$block_content = str_replace(
		'wp-block-button__link',
		'wp-block-button__link button',
		$block_content
	);

	return $block_content;

}, 5, 2 );

You can take it further from here if needed.

@klihelp
Copy link

klihelp commented Jul 15, 2019

Thank you @webmandesign, this is helpful for developers, but not for the content editors within the edit page.
Main function would be to use button classes provided by css frameworks, like Bootstrap.

@webmandesign
Copy link
Contributor Author

@klihelp You could actually tweak the code I've provided to gain such functionality you request. However, you would probably have to do some trickery, so, if we could set up a custom CSS class on button link directly in block options, it would be much better solution.

But here is the simplest solution I came up with:

add_filter( 'render_block', function( $block_content, $block ) {
	if ( 'core/button' === $block['blockName'] && isset( $block['attrs']['className'] ) ) {
		// Setting up a subset of custom button link classes.
		$allowed_button_link_classes = array(
			'my-custom-class',
			'another-custom-class',
			'example-custom-class',
			// ...
		);

		// Remove allowed button link classes from the button container first.
		$block_content = str_replace(
			$allowed_button_link_classes,
			'',
			$block_content
		);

		// Get custom button classes set for the block.
		$custom_classes = explode( ' ', $block['attrs']['className'] );

		// Apply allowed custom button link classes.
		$block_content = str_replace(
			'wp-block-button__link',
			'wp-block-button__link ' . implode( ' ', array_intersect( $custom_classes, $allowed_button_link_classes ) ),
			$block_content
		);
	}

	return $block_content;
}, 5, 2 );

Basically, this is looking only for a specified subset of custom button classes that can be applied on button link directly. Please set the $allowed_button_link_classes array to your CSS framework button classes you would like to use. Should work just fine then.

@klihelp
Copy link

klihelp commented Jul 15, 2019

Thank you, this looks really good to extend block classes as part of the theme design.
Most other blocks set classes directly to the container. With this method also possible to apply default classes on frontend. I have to try and see in the editor if it would be possible to reflect the classes and apply styles from custom editor css.

@pagelab
Copy link
Contributor

pagelab commented Jan 28, 2020

@webmandesign This is a nice workaround, thanks for sharing. But the ability to easily filter classes on child elements for any native block should definitely be part of core. Filtering only the container class is not flexible enough. It makes something like functional CSS not feasible to use.

@pagelab
Copy link
Contributor

pagelab commented Jan 28, 2020

Related #17194

@paaljoachim
Copy link
Contributor

Is this issue still valid?

@webmandesign
Copy link
Contributor Author

Is this issue still valid?

Well, not for me anymore as I am theme author. I can work around this issue. But I still believe this can be useful for editors, for example.

@skorasaurus skorasaurus added the [Block] Buttons Affects the Buttons Block label May 3, 2021
@pagelab
Copy link
Contributor

pagelab commented Jul 12, 2021

This would be easily solved by improving the Block Variations API. Adding a customClass attribute, or something similar, to child elements will allow developers to define custom classes for users to choose from in the UI.

@paaljoachim
Copy link
Contributor

Perhaps this issue would be something for Marcus @mkaz to take a closer look at....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Buttons Affects the Buttons Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

6 participants