Overview
The EasyCodingWithAI – Star Rating Shortcode addon allows WordPress users to easily incorporate a customizable 5-star rating system into their posts and pages. This plugin provides a shortcode that accepts two attributes: rating and color.
Users can specify how many stars should be filled (1 to 5) and choose from multiple colours, including black, red, orange, yellow, green, and blue.
Example Prompt.
“Create a WordPress addon that adds a shortcode for a 5-star rating system. The shortcode should allow users to specify the number of filled stars (from 1 to 5) and choose a color for the filled stars. The available colors should include black (default), red, orange, yellow, green, and blue. Ensure that unfilled stars are displayed with low opacity for clarity. The plugin should include both a PHP file for the functionality and a CSS file for styling the stars. Additionally, provide examples of how to use the shortcode in a post.”
Step 1: Set Up the Folder and File Structure
- Create the plugin folder:
- Name it easycoding-star-rating and place it in your WordPress
wp-content/plugins
directory.
- Create the main PHP file:
- Inside the plugin folder, create a file named easycoding-star-rating.php.
- Create the CSS file:
- In the same folder, create a CSS file named easycoding-star-rating.css. This file will contain the styling for the stars.
Step 2: Write the Main PHP File (easycoding-star-rating.php)
- Open easycoding-star-rating.php.
- Add the following code to define the plugin and register the shortcode:
<?php
/*
Plugin Name: EasyCodingWithAI - Star Rating Shortcode
Description: A shortcode to display a 5-star rating with customizable colors.
Version: 1.2
Author: EasyCodingWithAI
*/
function easycoding_star_rating_shortcode($atts) {
// Set default rating and color
$atts = shortcode_atts(array(
'rating' => 0,
'color' => 'black', // Default color
), $atts);
$rating = intval($atts['rating']);
$color = sanitize_text_field($atts['color']);
// Map color to CSS class
$color_class = 'rating-' . $color;
$output = '<div class="easycoding-star-rating ' . esc_attr($color_class) . '">';
// Generate stars based on rating
for ($i = 1; $i <= 5; $i++) {
if ($i <= $rating) {
$output .= '<span class="star filled">★</span>'; // Filled star
} else {
$output .= '<span class="star unfilled">★</span>'; // Unfilled star
}
}
$output .= '</div>';
return $output;
}
// Register the shortcode
add_shortcode('easycoding_star_rating', 'easycoding_star_rating_shortcode');
// Enqueue the CSS file
function easycoding_star_rating_enqueue_styles() {
wp_enqueue_style('easycoding-star-rating-css', plugins_url('easycoding-star-rating.css', __FILE__));
}
add_action('wp_enqueue_scripts', 'easycoding_star_rating_enqueue_styles');
?>
- Explanation:
- Plugin Header: Defines the plugin name, description, version, and author.
- Shortcode Function: Registers the
[easycoding_star_rating]
shortcode. The rating
and color
attributes specify the number of filled stars and the colour scheme. - Color Class: Adds a class based on the color chosen, which links to styles in the CSS file.
- Enqueue CSS: Loads easycoding-star-rating.css so the plugin can apply custom styles.
Step 3: Write the CSS File (easycoding-star-rating.css)
- Open easycoding-star-rating.css.
- Add the following CSS code to style the stars based on the
color
attribute:
<?php
/*
Plugin Name: EasyCodingWithAI - Star Rating Shortcode
Description: A shortcode to display a 5-star rating with customizable colors.
Version: 1.2
Author: EasyCodingWithAI
*/
function easycoding_star_rating_shortcode($atts) {
// Set default rating and color
$atts = shortcode_atts(array(
'rating' => 0,
'color' => 'black', // Default color
), $atts);
$rating = intval($atts['rating']);
$color = sanitize_text_field($atts['color']);
// Map color to CSS class
$color_class = 'rating-' . $color;
$output = '<div class="easycoding-star-rating ' . esc_attr($color_class) . '">';
// Generate stars based on rating
for ($i = 1; $i <= 5; $i++) {
if ($i <= $rating) {
$output .= '<span class="star filled">★</span>'; // Filled star
} else {
$output .= '<span class="star unfilled">★</span>'; // Unfilled star
}
}
$output .= '</div>';
return $output;
}
// Register the shortcode
add_shortcode('easycoding_star_rating', 'easycoding_star_rating_shortcode');
// Enqueue the CSS file
function easycoding_star_rating_enqueue_styles() {
wp_enqueue_style('easycoding-star-rating-css', plugins_url('easycoding-star-rating.css', __FILE__));
}
add_action('wp_enqueue_scripts', 'easycoding_star_rating_enqueue_styles');
?>
- Explanation:
- Default Star Styles: Sets the star font size and applies low opacity to unfilled stars.
- Color Classes: Each rating color class (
rating-black
, rating-red
, etc.) applies a specific color to filled stars based on the shortcode’s color
attribute.
Step 4: Test the Addon
- Activate the Plugin:
- Go to your WordPress Dashboard > Plugins, find EasyCodingWithAI – Star Rating Shortcode and activate it.
- Test the Shortcode:
- Add the shortcode to a post or page to verify it displays correctly.
- Example:
[easycoding_star_rating rating="3" color="yellow"]
– This should display 3 filled yellow stars.
Examples of the Shortcode
Here are a few examples with different colors:
[easycoding_star_rating rating="3" color="black"]
[easycoding_star_rating rating="1" color="red"]
[easycoding_star_rating rating="2" color="orange"]
[easycoding_star_rating rating="3" color="yellow"]
[easycoding_star_rating rating="4" color="green"]
[easycoding_star_rating rating="5" color="blue"]
Each example will apply the specified color to the filled stars while keeping the remaining stars at a low opacity.
Disclaimer
The coding tips and guides provided on this website are intended for informational and educational purposes only. While we strive to offer accurate and helpful content, these tips are meant as a starting point for your own coding projects and should not be considered professional advice.
We do not guarantee the effectiveness, security, or safety of any code or techniques discussed on this site. Implementing these tips is done at your own risk, and we encourage you to thoroughly test and evaluate any code before deploying it on your own website or application.
By using this site, you acknowledge that we are not responsible for any issues, damages, or losses that may arise from your use of the information provided herein.