I'm trying to create a shortcode that takes a single string as an argument, and then returns a hyperlink based on the string. In the code below, the value of $content passed to my function is always null and I can't figure out why. Where am I going wrong?
function add_objective_hyperlink($atts, $content = null) {
// var_dump($content);
$the_objective = strtolower($content);
$the_objective = str_replace(' ','-', $the_objective);
$the_objective = preg_replace('/[^a-z0-9-]+/', '-', $the_objective);
$the_hyperlink = '<a href="https://example.com/objective/' . $the_objective . '">' . $content . '</a>';
return $the_hyperlink;
}
add_shortcode('obj', 'add_objective_hyperlink');
I'm passing my shortcode in like this within my post content: [obj "The Thing I Want to Do"], and I'm expecting to see a hyperlink appear in my post. My var_dump($content) always returns string(0) "", though, which of course means there's no hyperlink in my page.
return sprintf('<a href="https://example.com/objective/%s">%s</a>', preg_replace('/[^a-z\d\- ]+/', '-', strtolower($obj)), $obj);