when i try to add an element to a site, that contains a php snippet, it is commented out and not working:

function phpTest() {
    return "<p><?php echo 'some text';?></p>"; 
}

add_shortcode("test", "phpTest"); 

when i add [test] to a site now and inspect the result i see that

<p><!--?php echo 'some text';?--></p>

has been generated. Why is that so and what can i do?

You don't display anything in shorcode's function, you just return what has to be displayed. Your code should be:

function phpTest() {
    return '<p>some text</p>'; 
}
add_shortcode('test', 'phpTest');

Update: if your function contains anything, which displays data, use it as follows:

function phpTest() {
    ob_start();
    // your code goes here
    return ob_get_clean(); 
}
add_shortcode('test', 'phpTest');
  • well of course that is how i can replace the echo command, but what if i want to have more complex php-functions inside my html like query an sql database? – Ginso89 Jan 31 at 15:41
  • read update to my answer. – Frank P. Walentynowicz Jan 31 at 15:59

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.