Domanda

Il mio plug-in è il congelamento l'esecuzione della pagina nel punto in cui si chiama le funzioni di seguito.

Ho due problemi ...

1) Come posso ricodificare la funzione in modo che se si verifica un errore, il plugin non si ferma il caricamento della pagina, ma piuttosto, restituisce un messaggio di errore?

2) Come posso valutazioni quale potrebbe essere l'errore? Il suo solo il congelamento, ma non emettere l'errore.

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.'")])');
        }
}

Ecco il mio tentativo di cambiare la 2 ° funzione con try cattura ma ottengo un errore fatale sul plugin di attivazione ...

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";
    }
}
È stato utile?

Soluzione

Questa linea è sbagliata:

$xPath = new DOMXPath(@$dom);

Dovrebbe essere questo:

$xPath = new DOMXPath($dom);

Altri suggerimenti

Per voi il debug potrebbe anche utilizzare la seguente sintassi invece di try / catch:

if (!$x) {
   throw new Exception('Division by zero.');
}
else return 1/$x;

o l'uso buon vecchio var_dump ($ x); che spesso si dice abbastanza per farti andare di nuovo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top