2017-03-14 8 views
0

않는 str_replace 함수에서 문자열 변환에 정의되지 않은 변수와 배열을 수정 :나는이 기능을 가지고

// load styles asynchronously - Transform stylesheet markup to loadCSS compatible 
add_filter('style_loader_tag', 'style_transform_loadCSS', 10, 2); 

function style_transform_loadCSS($html, $handle) { 
    if ($handle == CHILD_THEME_NAME ) 

     $search = array("rel='stylesheet' id='$handle-css'", "type='text/css' media='all'"); 
     $replace = array("rel=\"preload\"", "as=\"style\" onload=\"this.rel='stylesheet'\""); 

    return str_replace($search, $replace, $html)."<noscript>{$html}</noscript>"; 

} 

오기 '가 작동을하지만, 디버깅하는 동안이 오류 참조 :

Notice: Undefined variable: search in /home3/me/public_html/wp-content/themes/child/functions.php on line 362

Notice: Array to string conversion in /home3/me/public_html/wp-content/themes/child/functions.php on line 362

라인 (362)입니다 :

return str_replace($search, $replace, $html)."<noscript>{$html}</noscript>"; 

어떻게 해결할 수 있습니까?

답변

2

일부 대괄호가 누락되었습니다. 귀하의 예에서 진술은 첫 번째 다음 줄로 가고 이것은 $search 변수입니다. 귀하의 진술이 거짓 인 경우 귀하의 변수가 정의되지 않은 경우 문자열을 대체 할 다음 줄에 문제가 발생합니다.

function style_transform_loadCSS($html, $handle) { 
    if ($handle == CHILD_THEME_NAME) { 
     $search = array("rel='stylesheet' id='$handle-css'", "type='text/css' media='all'"); 
     $replace = array("rel=\"preload\"", "as=\"style\" onload=\"this.rel='stylesheet'\""); 
     $html = str_replace($search, $replace, $html)."<noscript>{$html}</noscript>" 
    } 
    return $html; 
} 
+0

검색 및 바꾸기가 이제는 'if'문과 다른 곳에서 정의됩니다. 이제는 누락 된 var마다 하나씩 두 개의 오류가 발생합니다. –

+0

예 : 나는 더 많은 코드를 추가했습니다. – Stony

+0

테마가 일치하지 않으면'$ html' (str_replace와 같지 않음)을 반환하고 싶다고 가정합니다. 하지만 그것은 단지 가정 일뿐입니다. –