When building custom solutions in WordPress with Elementor, you might run into scenarios where you need more control over the widget’s output. One common example is removing the schema markup from the Star Rating Widget. In this article, we’ll explain how you can add a new control to toggle schema output, and then adjust the widget’s rendering accordingly.
Overview
Elementor’s widgets can be extended by hooking into the element’s control registration process. We can add a switcher control that lets users enable or disable the inclusion of schema. Later, during the widget’s rendering phase, we check that control’s state to determine whether to include the schema in the output.
Step 1: Adding the Toggle Control
The first part of the code uses the elementor/element/star-rating/section_rating/before_section_end hook. This hook lets you add new controls before the end of a given section in the widget’s settings panel. In our case, we add a switcher control named use_schema:
/**
* Add toggle control to star rating widget for schema
**/
add_action( 'elementor/element/star-rating/section_rating/before_section_end', function( $element, $args ) {
$element->add_control(
'use_schema',
[
'type' => \Elementor\Controls_Manager::SWITCHER,
'label' => __( 'Use Schema', 'elementor' ),
'label_on' => __( 'Enable', 'elementor' ),
'label_off' => __( 'Disable', 'elementor' ),
'return_value' => 'yes',
'default' => 'yes',
]
);
}, 10, 2 );
Step 2: Modifying the Widget’s Output
Next, we want to adjust the output based on the use_schema setting. We hook into the widget’s rendering using elementor/widget/render_content. Here’s how we do it:
/**
* Render star rating widget without schema
**/
add_action( 'elementor/widget/render_content', function( $content, $widget ) {
if ( 'star-rating' === $widget->get_name() ) {
$settings = $widget->get_settings_for_display();
if ( empty( $settings['use_schema'] ) ) {
// Schema is disabled
// Get the rating scale and current rating
$rating_scale = (int) $settings['rating_scale'];
$rating_n = (float) $settings['rating'] > $rating_scale ? $rating_scale : $settings['rating'];
$rating_data = [ $rating_n, $rating_scale ];
// Set the appropriate icon style
$icon = '';
if ( 'star_fontawesome' === $settings['star_style'] ) {
if ( 'outline' === $settings['unmarked_star_style'] ) {
$icon = '';
}
} elseif ( 'star_unicode' === $settings['star_style'] ) {
$icon = '★';
if ( 'outline' === $settings['unmarked_star_style'] ) {
$icon = '☆';
}
}
// Build the HTML for the star rating
$rating = (float) $rating_data[0];
$floored_rating = floor( $rating );
$stars_html = '';
for ( $stars = 1.0; $stars <= $rating_data[1]; $stars++ ) {
if ( $stars <= $floored_rating ) {
$stars_html .= '<i class="elementor-star-full">' . $icon . '</i>';
} elseif ( $floored_rating + 1 === $stars && $rating !== $floored_rating ) {
$stars_html .= '<i class="elementor-star-' . ( $rating - $floored_rating ) * 10 . '">' . $icon . '</i>';
} else {
$stars_html .= '<i class="elementor-star-empty">' . $icon . '</i>';
}
}
// Combine all HTML parts together
$star_rating = '<div class="elementor-star-rating__wrapper">';
if( ! empty( $settings['title'] ) ){
$star_rating .= '<div class="elementor-star-rating__title">' . $settings['title'] . '</div>';
}
$star_rating .= '<div class="elementor-star-rating">' . $stars_html . '</div>';
$star_rating .= '</div>';
// Replace the widget content with our custom output
$content = $star_rating;
}
}
return $content; // Ensure the content is returned
}, 10, 2 );
/**
* add toggle control to star rating widget for schema
**/
add_action( 'elementor/element/star-rating/section_rating/before_section_end', function( $element, $args ) {
$element->add_control(
'use_schema',
[
'type' => \Elementor\Controls_Manager::SWITCHER,
'label' => __( 'Use Schema', 'elementor' ),
'label_on' => __( 'Enable', 'elementor' ),
'label_off' => __( 'Disable', 'elementor' ),
'return_value' => 'yes',
'default' => 'yes',
]
);
}, 10, 2 );
/**
* render star rating widget without schema
**/
add_action( 'elementor/widget/render_content', function( $content, $widget ) {
if ( 'star-rating' === $widget->get_name() ) {
$settings = $widget->get_settings_for_display();
if ( empty( $settings['use_schema'] ) ) {
//schema is disabled
$rating_scale = (int) $settings['rating_scale'];
$rating_n = (float) $settings['rating'] > $rating_scale ? $rating_scale : $settings['rating'];
$rating_data = [ $rating_n, $rating_scale ];
//set our icon style
$icon = '';
if ( 'star_fontawesome' === $settings['star_style'] ) {
if ( 'outline' === $settings['unmarked_star_style'] ) {
$icon = '';
}
} elseif ( 'star_unicode' === $settings['star_style'] ) {
$icon = '★';
if ( 'outline' === $settings['unmarked_star_style'] ) {
$icon = '☆';
}
}
//build our icon list items
$rating = (float) $rating_data[0];
$floored_rating = floor( $rating );
$stars_html = '';
for ( $stars = 1.0; $stars <= $rating_data[1]; $stars++ ) {
if ( $stars <= $floored_rating ) {
$stars_html .= '<i class="elementor-star-full">' . $icon . '</i>';
} elseif ( $floored_rating + 1 === $stars && $rating !== $floored_rating ) {
$stars_html .= '<i class="elementor-star-' . ( $rating - $floored_rating ) * 10 . '">' . $icon . '</i>';
} else {
$stars_html .= '<i class="elementor-star-empty">' . $icon . '</i>';
}
}
//put revised html together
$star_rating = '<div class="elementor-star-rating__wrapper">';
if( ! empty( $settings['title'] ) ){
$star_rating .= '<div class="elementor-star-rating__title">' . $settings['title'] . '</div>';
}
$star_rating .= '<div class="elementor-star-rating">' . $stars_html . '</div>';
$star_rating .= '</div>';
$content = $star_rating;
}
}
return $content; //do not mess with this
}, 10, 2 );