WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
allowed? line of PHP code? what are you trying to do? – Bainternet Jun 16 '11 at 13:28
    
edit my question..thanks! – jilseego Jun 16 '11 at 13:46
up vote 3 down vote accepted

$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');
share|improve this answer
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

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');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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