【发布时间】:2009-07-21 19:11:18
【问题描述】:
最终编辑:我已经将这个疯狂的问题总结并简化为一个新问题,所以我们可以关闭这个 Q 或任何必要的东西来标记它已解决。再次感谢!
新:
您能否告诉我您对此的看法,并可能看看您是否可以重新创建它:
目前,$post->post_content 变量包含:
"before <img src="/path/to/valid_img.gif" /> after"
这段代码放在主题header.php的顶部...
------ Code --------
1: $str = $post->post_content;
2: assert( isset( $str ) );
3: assert( is_string( $str ) );
4: echo $str;
5: $str = 'before <img src="/path/to/nonexistant.gif" /> after';
6: assert( isset( $str ) );
7: echo $str;
--------------------
输出这个...
------ Output ------
before <img src="/path/to/valid_img.gif" /> after
before <img src="/path/to/nonexistant.gif" /> after
--------------------
有了这些警告...
--- PHP Warnings ---
PHP Warning: assert() [<a href='function.assert'>function.assert</a>]: Assertion
failed in /path/to/header.php on line 2
PHP Warning: assert() [<a href='function.assert'>function.assert</a>]: Assertion
failed in /path/to/header.php on line 3
--------------------
当我知道 100% 应该成功时,如果 assert() 失败两次,为什么第 4 行会正确地回显 $str?
损坏的图像 src 与设置或取消设置变量 $str 有什么关系? WordPress 错误/奇怪?
现在是疯狂的部分......
当第 5-7 行被注释掉时,从而消除了 nonexistant.gif,assert() 警告不会出现并且输出仍然正确地产生这个......
------ Output ------
before <img src="/path/to/valid_img.gif" /> after
--------------------
你有没有机会复制这个并告诉我你的想法?我是 PHP 新手,但我很确定这很疯狂。 :)
旧:
我对 PHP 比较陌生,我有这个代码:
$str = $post->post_content; // hi -- [hw] -- bye <img src="foobar.png" />
$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 <img src="foobar.png" />".
当我运行上面的代码时,它工作正常(foo-shortcode 正在做它的工作)。输出是:
hi -- Hello World -- bye <img src="foobar.png" />
但是我总是收到此警告消息,告诉我 foo-shortcode 似乎正在尝试第二次运行自己,除了第二次,$str 不存在。
还有一个问题...当 $str 包含指向不存在的 src 的 HTML img 标记时,我只会收到此警告消息。
PHP Warning: Missing argument 1 for foo_shortcode() in ...
这里是 core-wp() 和 foo-shortcode():
function core_wp( $function, $a = NULL, $b = NULL )
{
$wrap = array
(
'foo_shortcode' => '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 );
}
首先,为什么代码会正常工作,然后似乎同时尝试再次运行自己?
关于包含无效 img 的 $str,我怀疑这与 WordPress 生成 $post->post_content 的方式有关。
编辑 1(7 月 22 日,星期三,上午 8:55)
我按照您的指示添加了 error-log(),如下所示:
error_log( 'done1' );
$str = $post->post_content;
error_log( 'done2' );
$str = core_wp( 'foo_shortcode', $str );
error_log( 'done3' );
...它在我的错误日志中产生了这个:
[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
...发送到浏览器的输出(正确)是:
hi -- Hello World -- bye <img src="foobar.png" />
...但警告信息仍然产生。
编辑 2(7 月 22 日,星期三,上午 9:18)
然后我尝试了这个断言:
59: $str = $post->post_content;
60: assert( isset( $str ) );
61: $str = core_wp( 'foo_shortcode', $str );
62: assert( isset( $str ) );
... PHP 日志显示:
[22-Jul-2009 09:16:55] PHP Warning: assert() [<a
href='function.assert'>function.assert</a>]: 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
...但同样,输出是正确的,但仍然发出警告。
在我看来,PHP 最初没有设置 $str = $post->post-content,然后它运行 core-wp('foo-shortcode', $str) 并对自己说“哦!我真的需要to set $str = $post->post-content..." 返回并设置它,然后输出正确的数据。
【问题讨论】:
-
感谢 Eineki,将 $args 初始化为数组并没有成功。
-
你确定 $str = $post->post_content;评估为与 Null 不同的东西?
-
是的,100% 确定,正如您在上面看到的 $post->post_content 包含一个字符串,“hi -- [hw] -- bye ”
-
我想知道,任何安装了 WordPress 工作副本的人都能够重现这个吗?我目前已将代码粘贴到我的主题的 header.php 中。
-
我无法重现此警告...但是我有一件小事:在您的情况下,$content 变量是一个数组,因此 str_replace() 将返回一个数组-这是故意的吗?