Skip to content

Commit

Permalink
Fix invalid HTML structure on the widgets screen (#24866)
Browse files Browse the repository at this point in the history
* Fix invalid HTML structure on the widgets screen

* Hide the save button on widget blocks

* Add inline CSS to both widgets.php AND the customizer

* Only hide the save button on widgets.php

* Use a named function instead of anonymous one

* Lint
  • Loading branch information
adamziel authored Aug 28, 2020
1 parent 86c6053 commit cefab03
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
36 changes: 34 additions & 2 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,41 @@ function register_site_icon_url( $response ) {
* Registers the WP_Widget_Block widget
*/
function gutenberg_register_widgets() {
if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
register_widget( 'WP_Widget_Block' );
if ( ! gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
return;
}

register_widget( 'WP_Widget_Block' );
// By default every widget on widgets.php is wrapped with a <form>.
// This means that you can sometimes end up with invalid HTML, e.g. when
// one of the widgets is a Search block.
//
// To fix the problem, let's add a filter that moves the form below the actual
// widget content.
global $pagenow;
if ( 'widgets.php' === $pagenow ) {
add_filter(
'dynamic_sidebar_params',
'gutenberg_override_sidebar_params_for_block_widget'
);
}
}

/**
* Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag.
*
* @param array $arg Dynamic sidebar params.
* @return array Updated dynamic sidebar params.
*/
function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
if ( 'Block' === $arg[0]['widget_name'] ) {
$arg[0]['before_form'] = '';
$arg[0]['before_widget_content'] = '<div class="widget-content">';
$arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
$arg[0]['after_form'] = '</form>';
}

return $arg;
}

add_action( 'widgets_init', 'gutenberg_register_widgets' );
4 changes: 4 additions & 0 deletions lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function gutenberg_widgets_init( $hook ) {
if ( 'widgets.php' === $hook ) {
wp_enqueue_style( 'wp-block-library' );
wp_enqueue_style( 'wp-block-library-theme' );
wp_add_inline_style(
'wp-block-library-theme',
'.block-widget-form .widget-control-save { display: none; }'
);
return;
}
if ( ! in_array( $hook, array( 'gutenberg_page_gutenberg-widgets', 'gutenberg_customizer' ), true ) ) {
Expand Down

0 comments on commit cefab03

Please sign in to comment.