Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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
1  
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
add comment

hi jilseego first of all your question is not clear.I Don't Have permission to Edit This Question.Anyhow i tried to solve. I guess, you want to Execute Php code using Shortcode? This is possible by this plugin PHP Exec . Just Follow the Instruction There.I hope You will get Answer.if not means tell,what exactly you want...:)

share|improve this answer
    
edit my question..thanks! –  jilseego Jun 16 '11 at 13:48
add comment

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
add comment

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.