Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP function called twice, funny warning message
    text
    copied!<p><strong>FINAL EDIT: I've summarized and simplified this crazy question into a new question, so we can close this Q or whatever is necessary to mark it resolved. Thanks again!</strong></p> <p><strong>NEW:</strong></p> <p>Can you tell me your opinion of this, and possibly see if you can recreate it:</p> <p>Currently, the $post->post_content variable contains:</p> <pre><code>"before &lt;img src="/path/to/valid_img.gif" /&gt; after" </code></pre> <p>This code placed at the top of the theme header.php...</p> <pre><code>------ Code -------- 1: $str = $post-&gt;post_content; 2: assert( isset( $str ) ); 3: assert( is_string( $str ) ); 4: echo $str; 5: $str = 'before &lt;img src="/path/to/nonexistant.gif" /&gt; after'; 6: assert( isset( $str ) ); 7: echo $str; -------------------- </code></pre> <p>Outputs this...</p> <pre><code>------ Output ------ before &lt;img src="/path/to/valid_img.gif" /&gt; after before &lt;img src="/path/to/nonexistant.gif" /&gt; after -------------------- </code></pre> <p>With these warnings...</p> <pre><code>--- PHP Warnings --- PHP Warning: assert() [&lt;a href='function.assert'&gt;function.assert&lt;/a&gt;]: Assertion failed in /path/to/header.php on line 2 PHP Warning: assert() [&lt;a href='function.assert'&gt;function.assert&lt;/a&gt;]: Assertion failed in /path/to/header.php on line 3 -------------------- </code></pre> <p>Why would line 4 properly echo the $str if assert() fails twice when I KNOW 100% it should succeed?</p> <p>What does a broken image src have to do with setting or unsetting the variable $str? WordPress bug/oddity?</p> <p><strong>Now the crazy part...</strong></p> <p>When lines 5-7 are commented out, thus eliminating the nonexistant.gif, the assert() warning does NOT appear and the output STILL properly produces this...</p> <pre><code>------ Output ------ before &lt;img src="/path/to/valid_img.gif" /&gt; after -------------------- </code></pre> <p>Any chance you can reproduce this and tell me what you think? I'm new to PHP, but I'm pretty sure this is crazyness. :)</p> <p><strong>OLD:</strong></p> <p>I'm relatively new to PHP and I have this code:</p> <pre><code>$str = $post-&gt;post_content; // hi -- [hw] -- bye &lt;img src="foobar.png" /&gt; $str = core_wp( 'foo_shortcode', $str ); echo $str; // return $str; produces the same warning message // If there is no echo or return, the warning message is not produced. // The warning disappears when I statically set (as object) the initial $str to // "hi -- [hw] -- bye &lt;img src="foobar.png" /&gt;". </code></pre> <p>When I run the above code, it works fine (foo-shortcode is doing it's job). The output is:</p> <pre><code>hi -- Hello World -- bye &lt;img src="foobar.png" /&gt; </code></pre> <p>However I always get this warning message, telling me that foo-shortcode seems to be trying to run itself a second time, except the second time around, $str does not exist.</p> <p>And the catch... I only get this warning message when $str contains an HTML img tag pointing to a non-existant src.</p> <pre><code>PHP Warning: Missing argument 1 for foo_shortcode() in ... </code></pre> <p>And here is core-wp() and foo-shortcode():</p> <pre><code>function core_wp( $function, $a = NULL, $b = NULL ) { $wrap = array ( 'foo_shortcode' =&gt; 'foo_shortcode', ); $args = array(); if ( isset( $a ) ) $args[] = $a; if ( isset( $b ) ) $args[] = $b; return call_user_func_array( $wrap[ $function ], $args ); } function foo_shortcode( $content ) { return str_replace( '[hw]', 'Hello World', $content ); } </code></pre> <p>First, why would the code work fine, and then simultaneously seem to try to run itself a second time?</p> <p>And regarding the $str containing an invalid img, I suspect this has something to do with the way WordPress generates $post->post_content.</p> <p>EDIT 1 (Weds, July 22 @ 8:55am)</p> <p>I added error-log() per your instructions like this:</p> <pre><code>error_log( 'done1' ); $str = $post-&gt;post_content; error_log( 'done2' ); $str = core_wp( 'foo_shortcode', $str ); error_log( 'done3' ); </code></pre> <p>... and it produced this in my error log:</p> <pre><code>[22-Jul-2009 08:52:49] done1 [22-Jul-2009 08:52:49] done2 [22-Jul-2009 08:52:49] PHP Warning: Missing argument 1 for foo_shortcode() in /home/path/to/header.php on line 23 [22-Jul-2009 08:52:49] done3 </code></pre> <p>... and the output sent to the browser (correctly) was:</p> <pre><code>hi -- Hello World -- bye &lt;img src="foobar.png" /&gt; </code></pre> <p>... but the warning message was still produced.</p> <p>EDIT 2 (Weds, July 22 @ 9:18am)</p> <p>I then tried this assertion:</p> <pre><code>59: $str = $post-&gt;post_content; 60: assert( isset( $str ) ); 61: $str = core_wp( 'foo_shortcode', $str ); 62: assert( isset( $str ) ); </code></pre> <p>... and the PHP log shows:</p> <pre><code>[22-Jul-2009 09:16:55] PHP Warning: assert() [&lt;a href='function.assert'&gt;function.assert&lt;/a&gt;]: Assertion failed in /home/path/to/header.php on line 60 [22-Jul-2009 09:16:55] PHP Warning: Missing argument 1 for foo_shortcode() in /home/path/to/header.php on line 23 </code></pre> <p>... but again, the output is correct, yet the warning is still issued.</p> <p>It seems to me that PHP is initially not setting $str = $post->post-content, then it runs core-wp( 'foo-shortcode', $str ) and says to itself "oh! I really need to set $str = $post->post-content..." goes back and sets it, and then outputs the correct data.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload