First things first. You shouldn’t really use echo
inside a shortcode, unless there is NO way to return the value. In your case your can simply convert it to a function that returns a value:
function date_important_info(){
return display_date($date);
function display_date($date){
$data = "
<div id='my-shortcode' style="">
<span>Date:</span>
<span> {$date} </span>
</div>";
return $data;
}
}
add_shortcode('date_info','date_important_info');
Now, about your problem with appending the data. You can select the HTML output of your shortcode and append it to another element by using jQuery. Here’s a quick example:
// Get shortcode's content
var content = $('#my-shortcode').outerHTML();
// Add it after the element you want
$('#the-element').after(content);
I’ve added an ID to your shortcode’s wrapper, to be able to select it via jQuery. You can also use .append()
or .before()
, based on your needs.