0

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

2 Answers 2

1

I think you are looking for this-

function star_rating( $atts ) {
$max = 5;
$atts = shortcode_atts(
    array(
        'grade' => $max, // default
    ), $atts );

$star_icons = '';

$full_star = floor( $atts['grade'] );
for( $i=1; $i<=$full_star; $i++ ) {
    $star_icons .= '<i class="fa fa-star"></i>';
}

$half_star = 0;
if( (int) $atts['grade'] != $atts['grade'] ){
    $half_star = 1;
    $star_icons .= '<i class="fa fa-star-half-o"></i>';
}

//considering you want to show empty stars as well
$empty_star = $max - $half_star - $full_star;
for( $i=1; $i<=$empty_star; $i++ ) {
    $star_icons .= '<i class="fa fa-star-o"></i>';
}

return $star_icons;

}
add_shortcode( 'star_rating', 'star_rating' );

Note:

  1. all fraction numbers are considered as 1 half-star. I mean, 2.5 and 2.6 will produce same output.
  2. these type of stuffs should be handled with CSS.
  3. Tested and working.
1

Your shortcode should look something like that

function se224467_shotcode_star_rating( $atts ) {

    $a = shortcode_atts( array(
        'grade' => 5,
    ), $atts );

    $full_stars = floor( $a['grade'] );

    // full stars
    $output = str_repeat( '<i class="fa fa-star"></i>', $full_stars );

    // if not a whole number append a halt
    if ( floatval($full_stars) !== floatval( $a['grade'] ) ) {
        $output .= '<i class="fa fa-star-half-o"></i>';
    }

    return $output;
}

add_shortcod( 'star_rating', 'se224467_shotcode_star_rating' );

I haven't tested it. You should be able to pass floats like [star_rating grade=3.5]. In fact anything but a whole number will get you a half star. This won't account for empty stars. See mukto90 answer for those.

Hope you get the idea.

Also consult the Codex about Shortcode API - Handling Attributes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.