Wow, you have used Insert PHP beyond my anticipation.
Yes, a variable set in one [insert_php][/insert_php] block can be read in a subsequent [insert_php][/insert_php] block on the same page; at least on the version of WordPress and PHP installation I’m using.
However, I was unable to duplicate the other functionality. Non-PHP code errors out, including “<?php“.
Perhaps another plugin is assisting, running the PHP code before it gets to the WordPress rendering engine. Or perhaps you’re using the code in a template.
Would you let me know where you’re using it and some live code, please? If posting code you’re using in this public forum isn’t acceptable, please send it to me via the contact form at http://www.willmaster.com/contact.php
The Insert PHP plugin scans the post or page for [insert_php][/insert_php] blocks. When one is found, the entire block is processed with the PHP eval() function. Output from the function is echo-ed to the browser.
The behavior of the PHP code is entirely dependent on the behavior of the eval() function.
Considering that each block is put through the eval() function, have a look at the PHP code you’re using to determine if it would be more efficient to have one block or several. If the amount of code is huge or has lots of loops, it may be more efficient to break it up. Otherwise, it may be more efficient to let eval() deal with the the whole thing in one go.
Will
Will,
I’m a WordPress and PHP novice, so I know just enough to be dangerous and to get me in trouble (and to try things that probably shouldn’t work, but do). Below is a sample for you to try, with multiple PHP script blocks. In this example, the first and last PHP tags on this page use your [insert_php][/insert_php] shortcodes. The rest use standard ending and starting PHP tags. I’m using WP Version 4.4.2, PHP Version 5.4.45 and Version 1.3 of your plugin.
[insert_php]
if (empty($_POST['survey'])) {
$survey = 'A';
} else {
$survey = $_POST['survey'];
}
?>
<h3>Choose your option</h3>
<form action='#' method='post' id='testform'>
<input form='testform' onChange='this.form.submit();' type='radio' name='survey' value='A' <?php if ($survey == 'A') { ?>checked='checked' <?php } ?> > A
<input form='testform' onChange='this.form.submit();' type='radio' name='survey' value='B' <?php if ($survey == 'B') { ?>checked='checked' <?php } ?> > B
</form>
<h3 style='margin-top: 2em;'>You selected Option <?php echo $survey; [/insert_php] </h3>
Will,
I converted a substantial page of html and php scripting to a single starting and ending [insert_php][/insert_php] with nested ending and starting ‘?>’ ‘<?php’ tags. Everything works fine. The only difference that I noticed is that I need to escape the ‘$’ character (replacing instances of ‘$’ with ‘\$’ in my html). I”m guessing the eval() preprocessor needs this. I find using ‘<?php ?>’ instead of ‘[insert_php] [/insert_php]’ easier to type and easier to read. I can’t say that I noticed a performance difference in the way the page updates.
Again, I’m a WordPress, HTML and PHP novice, so others should take that into consideration when reviewing this post, but it’s working for me.
Thanks again for a great plugin.
I appreciate the time you took with sharing the code and letting us all know what you did. I’m unable to duplicate it on a new WordPress installation with only the plugins that come with the download and Insert PHP.
From what you describe, it seems the post or page is being processed for PHP code before Insert PHP sees it. But maybe not. I’m glad it’s working for you.
Will
Will,
Sorry I sent you on a fruitless search. I did disable all plugins (except Insert PHP) and the page still works fine with the code above. Note that I’m pasting the code into the “Text” view (not the “Visual” view) which I’m sure is what you did to test. This works for me with two free themes: WooShopLite and CyberChimps Responsive. Note also that this code fails if I use only standard PHP tags (replacing the starting and ending [PHP_INSERT][/PHP_INSERT] with <?php ?>. If I discover anything unique about my configuration, I’ll post here.
Hi, Will – you may want to take another look at this technique. I’m now using it successfully on multiple sites with multiple themes and plugin combinations, hosted on GoDaddy Managed WordPress, GoDaddy cPanel, and on my own LAMP server – all running PHP 5.4.45 and WordPress 4.5. In a nutshell, your plugin fully supports nested ‘<?php’ ‘?>’ within the enclosing outer [insert_php] [/insert_php] shortcodes. If it’s not working for you, it’s my fault since I probably described it wrong. Try this simple example and let me know what you find:
[insert_php] $test = "test"; ?>
<p>This is a <?php echo $test; [/insert_php] </p>
Well, that is really cool.
Is this in templates or within the content of a post or page?
Will
I have only needed to use this technique in WordPress pages. Because of this, I never use the ‘Visual’ tab when editing pages (only the ‘Text’ tab). For complex html/php combinations, it is much easier to use <?php and ?> (and much easier to read) inside of the outer [insert_php] [/insert_php] shortcodes. Your plugin is now at the top of my list of favorites! Thank you!
Yes, it works exactly like you say it does. It’s so counter-intuitive for me to work with unbalanced tags that it took my mind a while to accept exactly what you posted. I kept putting <?php above [insert_php] and ?> below [/insert_php] in order to balance the tags; and of course that didn’t work.
You’ve made a wonderful discovery.
My assumption is that the eval() command, which Insert PHP uses to process the code between [insert_php] and [/insert_php], assumes <?php at the beginning and ?> at the end. But that may be incorrect.
However exec() handles it, what you discovered works. Thank you.
Will
Will,
Glad it worked for you! Now try the slightly more complex example that I gave previously and you’ll see just how powerful this can be. Works great! Thanks again for a great plugin.