Display long code via shortcode in Wordpress

Viewed 25

I want to display some html/php code via shortcode. After looking it up, I ended up with this :

add_shortcode('myshortcode', 'frontend_testshortcode');
        
function frontend_testshortcode() {
    $email = get_field("field_email");
    $name = get_field("field_name"); 
        return  $email;
    }

I then insert [myshortcode] and the email is displayed on frontend, no problem.

However, the code I want to insert is much more complex, with dozens of variables and a lot of html (it is already written - until now i've been displaying it via shortcode but using a specific plugin. I want to get rid of the plugin). If i do the following :

function frontend_testshortcode() {
    $email = get_field("field_email");
    $name = get_field("field_name"); 
        <h1><?php echo $name; ?></h1>
        <p><?php echo $email; ?></p>                       
    }

It won't work. I understand that a shortcode requires "return $variable;" but i can't rewrite the whole code so as it can be inserted in a variable. What would be the correct way to proceed?

Thank you!

0 Answers
Related