我的插件将页面的执行冻结在调用以下函数的位置。
我有两个问题。。。
1) 如何重新编码函数,以便在出现错误时,插件不会停止页面加载,而是返回错误消息?
2) 我如何反馈可能的错误?它只是冻结,但没有输出错误。
function rseo_get_seo($check, $post){
//return false;
switch ($check)
{
case "h1": return rseo_doTheParse(\'h1\', $post);
case "h2": return rseo_doTheParse(\'h2\', $post);
case "h3": return rseo_doTheParse(\'h3\', $post);
case "img-alt": return rseo_doTheParse(\'img-alt\', $post);
}
}
function rseo_doTheParse($heading, $post)
{
$content = $post->post_content;
if($content=="") return false;
$keyword = trim(strtolower(rseo_getKeyword($post)));
@$dom = new DOMDocument;
@$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath(@$dom);
switch ($heading)
{
case "img-alt": return $xPath->evaluate(\'boolean(//img[contains(@alt, "\'.$keyword.\'")])\');
default: return $xPath->evaluate(\'boolean(/html/body//\'.$heading.\'[contains(.,"\'.$keyword.\'")])\');
}
}
这是我尝试用try-catch更改第二个函数,但在插件激活时出现了一个致命错误。。。
function rseo_doTheParse($heading, $post){
try { //I get a FATAL error here. unexpected \'{\'
$content = $post->post_content;
if($content=="") return false;
$keyword = trim(strtolower(rseo_getKeyword($post)));
@$dom = new DOMDocument;
@$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath(@$dom);
switch ($heading)
{
case "img-alt": return $xPath->evaluate(\'boolean(//img[contains(@alt, "\'.$keyword.\'")])\');
default: return $xPath->evaluate(\'boolean(/html/body//\'.$heading.\'[contains(.,"\'.$keyword.\'")])\');
}
}
catch (Exception $e)
{
echo \'Exception caught: \', $e->getMessage(), "\\n";
}
}