I’m trying to write a shortcode, but it has quotes within quotes within quotes because it has a shortcode in it. This link is breaking and not rendering the href correctly. Here:
<?php
function worksheet($atts){
$default = array(
'year' => 'Year-1',
'title' => 'Writing Numbers',
'level' => 'Easy',
);
$attributes = shortcode_atts($default, $atts);
return "<a href='[s2File download='access-s2member-ccap-free/" . $attributes['year'] . "/" . $attributes['title'] . ".pdf' download_key='true' /]&s2member_file_inline=yes'>" . $attributes['title'] . " (" . $attributes['level'] . ")</a>";
}
add_shortcode('worksheet', 'worksheet');
?>
I’ve read the other solutions about escaping th quotation marks but it still doesn’t work with the following code:
<?php
function worksheet($atts){
$default = array(
'year' => 'Year-1',
'title' => 'Writing Numbers',
'level' => 'Easy',
);
$attributes = shortcode_atts($default, $atts);
return "<a href="[s2File download='access-s2member-ccap-free/" . $attributes['year'] . "/" . $attributes['title'] . ".pdf' download_key='true' /]&s2member_file_inline=yes">" . $attributes['title'] . " (" . $attributes['level'] . ")</a>";
}
add_shortcode('worksheet', 'worksheet');
?>
I’m expecting the same as if I just write the link as follows:
<a href="[s2File download='access-s2member-ccap-free/Year-1/Counting with 10 pence (A).pdf' download_key='true' /]&s2member_file_inline=yes">TEST LINK</a>
I’ve also tried to use " . chr(39) . ", replacing the quotes. DOesn’t work. I have read all the other solutions.
2
Answers
You’re trying to render nested shortcodes. The way you’re doing it has less to do with quotes, and more to do with how to handle a shortcode within another shortcode. You’ll need to render out the inner shortcode using do_shortcode before returning the output of your own wrapper.
I tend to also prefer the use of sprintf to help with quoting and concatenation:
Your code looks mostly correct, but there’s a small issue in how the link is generated with the [s2File] shortcode within your worksheet function. WordPress doesn’t automatically parse shortcodes embedded in href attributes, so using do_shortcode is necessary to ensure that the [s2File] shortcode is processed properly. Here’s an updated version of your code that should work as expected:
This update uses do_shortcode to process the [s2File] shortcode, ensuring the proper URL is generated and placed within the href attribute.