E
E
Elena2020-06-23 12:01:53
WordPress
Elena, 2020-06-23 12:01:53

Why is the shortcode displayed twice on the page?

function rating_function($atts) {
    extract(shortcode_atts(array(
    'rating' => 5, 
    'type' => 'rating', 
    'number' => 0 
  ), $atts));
return wp_star_rating(array('rating' => $rating, 'type' => $type, 'number' => $number));
}
add_shortcode('rating', 'rating_function');

On the page, for example, [rating rating="3"] is added.
As a result, it displays two lines with a rating, what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Ermilov, 2020-06-23
@sergeiermilov

Do not use the extract() function (works similar to array_merge ). It is considered bad style and its use is deprecated, even for core Wordpress. This is not safe, often creates a lot of conflicts, and also overwrites some data. It is likely that this is the problem. Better this way:

$args = shortcode_atts( 
    array(
    'rating' => 5, 
    'type' => 'rating', 
    'number' => 0,
    ), 
    $atts
);

wp_star_rating has the last element in the 'echo' array, which is true by default . I have not tested these functions, but you can try to make it false .
Also, wp_star_rating() is supposed to be a built-in function. Maybe instead of overriding the wp_star_rating() function, just use the built-in function?
In order to use this feature, your template must include the wp-admin/includes/template.php file . The documentation has examples.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question