Support » Plugins » Basic Shortcode Help…

  • I run a review site and we use stars throughout our site that aren’t part of our theme. We were using a plugin to create the stars but then ran into an issue with it bogging down. So, we’d like to dump the plugin and do this manually. Our stars range from 1 to 5 with half-stars possible. For our reviews we pull from a /images folder that contains 10 images of stars (one-half star up to five stars).

    We reference these stars in a text widget, in reviews and, most importantly, in a compendium of all reviews for a year. That means each movie has 3 locations where it gets manual text to present the stars. Reducing the likelihood of a typing error is a nice thing.

    So I figured we could finally utilize shortcodes to do this. I’d like to create a one that works like this:

    [stars 5] or [stars 3.5] (for half stars) and have that be a macro standing in for the image tag:

    <img src="">

    That will keep things much cleaner. However, having not created a shortcode before I need a hand from anyone willing to provide the code snippet. I’ll learn from that and be able to expand it from there and not have to bug the forum any longer but I’d appreciate the help as, otherwise, I’ll spend weeks of limited free time making a thousand mistakes trying to get this first example right.

    I realize I need to create a function like:

    function star_image ($rating) {
    return ‘<img src=”‘. $rating . ‘.png’ . ‘” />’;

    Then I need to add the shortcode call to functions.php as so:

    add_shortcode(‘stars’, ‘star_image’);

    Does that look correct or did I forget something or just botch it entirely?

    Thank you in advance….

Viewing 1 replies (of 1 total)
  • Oh, where does the function code itself go? Does the add_shortcode portion simply go above the function code in functions.php or are they in different files?

Viewing 1 replies (of 1 total)
  • The topic ‘Basic Shortcode Help…’ is closed to new replies.