Is it possible to return a variable from a php file that includes a combination of html & php code and a shortcode, that will be used and run in a woocommerce product description?
(I have an extension installed that allow execution of php code inside the wysiwyg editor)
The code that i have so far is:
function age ($example) {
$x = "<b>Alter:</b> [insert_php]global $product; $dobs = get_the_terms( $product->id, "pa_geburtsdatum"); foreach ($dobs as $dob) { $from = new DateTime($dob->name); $to = new DateTime("today"); echo $from->diff($to)->y; }[/insert_php]";
return $x;
}
UPDATE: I have created a custom shortcode, doing all the calculation behind the scenes, which is way more comfortable and secure. In between the shortcode I have the dob, and the age is then given back after calculating it with a specific function.
You could use the ever-unsafe and incredibly dangerous eval()
function to take the value of your shortcode and run it as PHP. This will run anything you pass to it as PHP, so:
eval('global $product; $dobs = get_the_terms( $product->id, "pa_geburtsdatum"); foreach ($dobs as $dob) { $from = new DateTime($dob->name); $to = new DateTime("today"); echo $from->diff($to)->y; }');
would produce the output you're looking for. [pontification]I stress, however, that this would be a terrible idea on a forward-facing site.[/pontification] If it were me (and it's not, to each their own), I'd make a shortcode that took the arguments you want to work with and process them behind the scenes.