0

I wanted to insert a form with a line of PHP code via shortcode. I am wondering if it's allowed? Has anyone tried it? Take the following for example:

function get_form($atts) {
    return '<form method="post" action="">
            <input type="input" name="myinput" value=""></input>
            <input type="hidden" name="myvar" value="<?php echo $current_user->ID; ?>">
            </form>';
}
add_shortcode('myshortcode', 'get_form');

I hope it's clear now...

  • allowed? line of PHP code? what are you trying to do? – Bainternet Jun 16 '11 at 13:28
3

$current_user isn't declared in the scope of that function. You'd want to modify the code to be more like this:

function get_form($atts) {
    $current_user = wp_get_current_user();
    return '<form method="post" action="">
            <input type="input" name="myinput" value=""></input>
            <input type="hidden" name="myvar" value="' . $current_user->ID . '">
            </form>';
}
add_shortcode('myshortcode', 'get_form');
  • 2
    Of course, you should probably check to see if the user is even logged in at all with if( is_user_logged_in() ){} before assuming they are. – John P Bloch Jun 16 '11 at 14:25
0

no that wont work!

you need to return all of the value as string/html:

function get_form($atts) {
    return '<form method="post" action="">
            <input type="input" name="myinput" value=""></input>
            <input type="hidden" name="myvar" value="'.$current_user->ID.'">
            </form>';
}
add_shortcode('myshortcode', 'get_form');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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