0

I am using a php plugin to insert php in my code.

I have a form and I want to make it disappears once it is submitted, but even the most simple condition I am trying to make is not working (he always prints the header):

[insert_php]
   echo $_REQUEST['code'];
   if ($_REQUEST['code'] == 'aba'){

[/insert_php]


   <h2>HEADER</h2>


[insert_php]    
    }
[/insert_php]

The php is working normally because if I change it a little it prints what I want:

 [insert_php]
       echo $_REQUEST['code'];
       if ($_REQUEST['code'] == 'aba'){
            echo 'example phrase'

        }
    [/insert_php]

There is anything I am missing ?

4 Answers 4

2

I wouldn't recommend writing PHP code in the content this way, e.g. for security reasons.

Most likely the shortcode is using eval() and you need a valid PHP code snippet in each such call.

The PHP documentation has this warning:

Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

You should consider writing your own shortcode instead, keeping the PHP code away from the users and the content editor.

0

I assume your GET-ting/POST-ting on the same page? (Due to the use of $_REQUEST).

But anyway, you might need to create your own shortcode for your custom content, here is an example to help you get started.

You can paste it in your theme's functions.php

// Attach callback method to the wordpress hook 
add_shortcode('my_shortcode', 'unique_key_my_shortcode');

// Define the callback
function unique_key_my_shortcode() {

    $code = $_REQUEST['code'];

    // Catch echoed values
    ob_start();
    ?>
        <!-- Create DIV container, just in case -->
        <div class="my_shortcode_container">

            <!-- Just like in your sample code -->
            <?php echo $code; ?>

            <!-- If code == 'aba' then include the html below -->
            <?php if ($code == 'aba'): ?>

                <h2>Code is equal to "aba"</h2>

            <?php endif; ?>
        </div>

    <?php

    // Store the html in a variable
    $html = ob_get_contents();

    // Clean
    ob_end_clean();

    // return
    return $html;
}

and then place the shortcode in a text widget or something as [my_shortcode]

0

The best way you could do it is:

[insert_php]
   echo $_REQUEST['code'];
   if ($_REQUEST['code'] == 'aba'){

   echo $header = '<h2>HEADER</h2>';

    }
[/insert_php]

or if you have lot of HTML code you can use concatenation. For ex:

[insert_php]
   echo $_REQUEST['code'];
   if ($_REQUEST['code'] == 'aba'){

    $header='<h1>';
    $header.='Header';
    $header.='</h1>';
    echo $header;

    }
[/insert_php]
0

I think the most useful answer is the one provided by tan05. And the simplest but useful the answer from nitingsingh.

Here is another one, a different (and worse than the shortcode solution). Use alternate syntax.

Try with:

[insert_php]
 echo $_REQUEST['code'];
 if ($_REQUEST['code'] == 'aba'):
[/insert_php]

<h2>HEADER</h2>

[insert_php]    
 endif;
[/insert_php]

Note that instead of using:

if(condition){
   do_something
}

You can use:

if(condition): 
   do_domething
endif;

Check out http://php.net/manual/en/control-structures.elseif.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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