2

I want to adjust a comparison based on attributes I define in my shortcode. As example I use the following short code:

[comparison phonePrefix="0581",phoneFlatOption="true", 
accessModeDsl="true", accessModeCable="false", accessModeLte="false", 
accessModeSat="false"] 

Now here is my shortcode function:

function comparison($atts){

$atts = shortcode_atts(
array(
        'phonePrefix' => '030',
        'customerType' => 'existingCustomer',
        'remainingContractDuration' => '6',
        'phoneFlatOption' => 'true',
        'minSpeed' => '50000',
        'accessModeDsl' => 'true',
        'onlyProductsWithoutTrafficLimitation' => 'true',
        'accessModeCable' => 'true',
        'accessModeLte' => 'true',
        'accessModeSat' => 'true',
        'contractDuration' => '-1',
        'mergeDefaults' => 'false',
    ), $atts, 'dslVergleich');
 }

As I understood the shortcode_atts() function it should now replace the default value of 'phonePrefix' with 0581. But after I use the function there is still the default value of 030.

This is a var_dump before I used shortcode_atts():

array(5) { 
  [0]=> string(42) "phonePrefix="0581",phoneFlatOption="true"," 
  [1]=> string(21) "accessModeDsl="true","
  [2]=> string(24) "accessModeCable="false"," 
  [3]=> string(22) "accessModeLte="false"," 
  ["accessmodesat"]=> string(5) "false" 
} 

And this is after I used shortcode_atts():

array(12) {
  ["phonePrefix"]=> string(3) "030" 
  ["customerType"]=> string(16) "existingCustomer"     
  ["remainingContractDuration"]=> string(1) "6" 
  ["phoneFlatOption"]=> string(4) "true"
  ["minSpeed"]=> string(5) "50000" 
  ["accessModeDsl"]=> string(4) "true" 
  ["onlyProductsWithoutTrafficLimitation"]=> string(4) "true" 
  ["accessModeCable"]=> string(4) "true" 
  ["accessModeLte"]=> string(4) "true" 
  ["accessModeSat"]=> string(4) "true" 
  ["contractDuration"]=> string(2) "-1" 
  ["mergeDefaults"]=> string(5) "false"
}

Thanks for the help!

  • you don't have to use , for argument separation. try [comparison phonePrefix="0581" phoneFlatOption="true" accessModeDsl="true" accessModeCable="false" accessModeLte="false" accessModeSat="false"] – mmm Jul 12 '18 at 14:30
3

The var_dump() before using shortcode_atts() reveals the problem. Lets look at that output in a more readable format:

array(5) { 
    [0] => string(42) "phonePrefix="0581",phoneFlatOption="true"," 
    [1]=> string(21) "accessModeDsl="true"," 
    [2]=> string(24) "accessModeCable="false"," 
    [3]=> string(22) "accessModeLte="false"," 
    ["accessmodesat"]=> string(5) "false"
} 

As you can see, the attributes are not being parsed correctly. Instead of 'attribute' => 'value' you're getting a numerically indexed array of broken bits of the shortcode.

This is because of the way you've written the shortcode, not anything in the shortcode code itself.

[comparison phonePrefix="0581",phoneFlatOption="true", 
accessModeDsl="true", accessModeCable="false", accessModeLte="false", 
accessModeSat="false"] 

The lack of spaces between the attributes and the use of commas is incorrect formatting for shortcodes. It should be formatted like this:

[comparison phonePrefix="0581" phoneFlatOption="true" 
accessModeDsl="true" accessModeCable="false" accessModeLte="false" 
accessModeSat="false"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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