I have looked over this more times than I can possibly count, but cannot for the life of me figure out what I am doing wrong. I have written shortcode functions many times in the past, also, more times than I can count. For whatever reason, this one refuses to work correctly. After an hour of trying over and over again, I have not found success. Please help.
The defaults appear in the html just fine, but will not reflect the parameters set within the shortcode.
- I’ve tried treating ID as a string and an integer.
- Maybe wordpress doesn’t like me trying to to use the attr
ID
… TriedlistID
, still failing. - I have tried using
extract
vs saving the result ofshortcode_attr()
into a variable. - Thought maybe a plugin or the post_type was preventing me from winning, but upon taking the example shortcode API code from WordPress.org and inserting it into functions.php and placing their shortcode in my post, it worked. I’m going mad
- I’ve cleared Object Cache, to debug this, I’ve changed the defaults in the function, which appear client-side immediately upon change.
- Maybe Shortcode API doesn’t like me building strings. Saved it into a buffer using
ob_start()
andob_get_clean()
(Like, take a shower?) … no joy - No,
var_dump
andprint_r
do nothing to help me, there is no output. Even when I try tovar_dump(array('foo' => 'bar'))
orprint_r(array('foo' => 'bar'))
My sanity is now being tested beyond reasonable measure and I’m fantasizing about DDOSing Matt Mullenweg’s smartphone
Please cure my madness, and point out the tiny detail I am obviously overlooking so I can move on with my day.
Shortcode
[subscription ID="1" referrer="overlay-promotion" buttonText="Send it to me" buttonBGHex="#000c49"]
Function
function subscription_form($atts){
//Yes, I tried extract to, and yes, I changed the variables in the html below to reflect this change.
$a = shortcode_atts( array(
"ID" => "1",
"referrer" => "not-what-you-think-this-should-be",
"buttonText" => "Subscribe",
"buttonBGHex" => "#78C138"
), $atts, "subscription" );
$html = '<form id="subscribe" name="email_signup" class="form-inline validate" action="http://www.example.com/subscribe" method="post">';
$html .= '<input type="hidden" name="referrer" value="'.$a["referrer"].'">';
$html .= '<input type="hidden" name="success_url" value="http://www.example.com/subscribe/success">';
$html .= '<input type="hidden" name="error_url" value="http://www.example.com/subscribe/error">';
$html .= '<input type="hidden" name="list_id" value="'.$a["ID"].'">';
$html .= '<input type="text" placeholder="Enter your email address" class="input-large" name="email">';
$html .= '<input type="submit" class="btn btn-primary" style="background-color:'.$a["buttonBGHex"].';" name="submit" value="'.$a["buttonText"].'">';
$html .= '</form>';
return $html;
}
add_shortcode( 'subscription', 'subscription_form' );
Output
<form id="subscribe" name="email_signup" class="form-inline validate" action="https://www.example.com/subscribe" method="post">
<input type="hidden" name="referrer" value="HP-overlay-2015">
<input type="hidden" name="success_url" value="https://www.example.com/subscribe/success">
<input type="hidden" name="error_url" value="https://www.example.com/subscribe/error">
<input type="hidden" name="list_id" value="1"><input type="text" placeholder="Enter your email address" class="input-large" name="email">
<input type="submit" class="btn btn-primary" style="background-color:#78C138;" name="submit" value="Subscribe">
</form>
According to the Codex for
add_shortcode
the attribute names for a shortcode are converted to lowercase.That would mean you need to access your array keys with the lowercase version for the correct output.