Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Last active April 29, 2022 21:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pingram3541/49b110dd8d6188324abe47d7663e39ec to your computer and use it in GitHub Desktop.
Save pingram3541/49b110dd8d6188324abe47d7663e39ec to your computer and use it in GitHub Desktop.
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_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 );
@TRADEREXDE
Copy link

Hi mate,

any idea how to make that script work when you want to use dynamic values for the star rating? It only works if I enable the schema..

Thanks so much for your help!!

@petersedivy
Copy link

Hi,

same question, any idea how to make that script work when you want to use dynamic values for the star rating? It only works if I enable the schema..

Thanks so much.

@pingram3541
Copy link
Author

I tried both dynamic values returned as a String and an INT and it works for me.

I used this shortcode as my dynamic source:

//Shortcode to output simple numeric value function custom_star_rating_func(){ return 3.5; } add_shortcode('custom_star_rating', 'custom_star_rating_func');

I then chose 'shortcode' as the dynamic type and placed [custom_star_rating] within it's textarea and the value rendered as expected (must use shortcode brackets in the value).

@pingram3541
Copy link
Author

Ah ok ok, I see it doesn't render on the front end because it needs to execute the shortcode first and get the value before returning the widget $settings. This is achieved by running get_settings_for_display() instead of just plain old get_settings(). Great catch, gist has been updated.

@petersedivy
Copy link

Hi!

you are the best! Awesome work.
Well thank you.
P.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment