This is my first go at making my own shortcode.
What I am trying to do
I have 11 different outputs for star ratings I want to be able to use in posts. I want to wrap it all into one shortcode, and be able to call the specific rating I want. I imagine something like this:
[star_rating grade="3"]
That would produce 3 out of five stars.
What I know
I have figured out how to accomplish this using a separate function and shortcode for each grade, but it seems very redundant.
What I have
This is what I have managed to get so far:
function star_rating_5_func( $atts ) {
$a = shortcode_atts( array(
'5' => '<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>'
), $atts );
return "{$a['5']}";
}
add_shortcode( 'star_rating_5', 'star_rating_5_func' );
function star_rating_4_5_func( $atts ) {
$a = shortcode_atts( array(
'4_5' => '<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>'
), $atts );
return "{$a['4_5']}";
}
add_shortcode( 'star_rating_4_5', 'star_rating_4_5_func' );
This produces a five star rating for [star_rating_5] and a 4.5 star rating for [star_rating_4_5].
I'm not very fluent in php, so its a lot of copy and paste at this point. I know you can define variables, and it seems I should be able to simply call a specific one to get the output I want.
Is this possible?
[star_rating grade="3"] -EXAMPLE