Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Elementor Widget Control to Remove Schema from Star Rating Widget
/**
* 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();
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.