0

My shortcode always uses the default value for the single attribute, reviewstoshow, instead of the incoming attribute value. The incoming value has the correct number (per the second half of the dump included below) but the $numberofreviewstoshow var is always set to the default value (6).

Here is the shortcode being used:

[tdp_random_reviews reviewstoshow="8"]

This is the shortcode code:

add_shortcode( 'tdp_random_reviews', 'show_tdp_random_reviews' );

function show_tdp_random_reviews($atts) {
    $a = shortcode_atts( 
            array(
                'reviewstoshow' => '6'
            ), 
            $atts 
    );

var_dump($a);
var_dump($atts);

    $numberofreviewstoshow = $a['reviewstoshow'];

<snip>

}

Here is the output from the two var_dump() lines:

array(1) { ["reviewstoshow"]=> string(1) "6" }
array(1) { [0]=> string(30) "reviewstoshow="8"" }

Can you tell me where my mistake is? Thanks!

5
  • Have you tried a var_dump() of $numberofreviewstoshow? It looks right - $a['attributename']. You might also want to double-check types (string vs. integer) and quotes (smart vs. plain) if that's not giving you the value you expect.
    – WebElaine
    Apr 30, 2020 at 18:56
  • Just tried that again to confirm my memory: var_dump($numberofreviewstoshow); Returns: string(1) "6" I replaced all of the quotes to ensure they are dumb. "It looks right" — thanks for making me feel less dumb! Have any more ideas?
    – BCarnathan
    Apr 30, 2020 at 20:33
  • That second var_dump should be outputting an array, not a string. Are you sure your shortcode itself is correct? You're not using weird quotes around the 8, or something? Because WP is not recognizing the parameter.
    – vancoder
    Apr 30, 2020 at 22:08
  • You solved the problem, vancoder. I replaced all quotes in the code per WebElaine but did not change out the quotes in the call. I don't know how wonky quotes got in there in the first place but thanks for the help both of you!!!
    – BCarnathan
    Apr 30, 2020 at 22:38
  • One other things to mention is that you can't use camelCase names for attribute values. So fooBar is not allowed, foobar is. Feb 4 at 22:02

1 Answer 1

2

The quotes in the shortcode call were not standard (despite them looking so). Replacing the old double quote characters with typed-into-notepad, copied, and pasted new quotes in the theme editor resolved the problems.

Thanks to @WebElaine and @vancoder for the help!

1
  • This also was the case for me, was pulling my hair out for about an hour. After looking at the code again. It was the "typed-into-notepad quotes". Which is weird, cause I copy and pasted the shortcode in the "text" view inside the Wordpress post. Which I now can safely say that the "text" view is not "plain text" inside a Wordpress post Feb 4 at 21:55

Your Answer

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

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