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...

1
  • allowed? line of PHP code? what are you trying to do? Commented Jun 16, 2011 at 13:28

2 Answers 2

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');
1
  • 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. Commented Jun 16, 2011 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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.