PHP函数内部的ECHO语句

Rob*_*ick 0 php wordpress html5

我在下面的Wordpress中使用此功能:

function wpstudio_doctype() {
  $content = '<!DOCTYPE html>' . "\n";
  $content .= '<html ' . language_attributes() . '>';
    echo apply_filters( 'wpstudio_doctype', $content );
}
Run Code Online (Sandbox Code Playgroud)

问题是该函数显示$content<!DOCTYPE html>标记上方,而不是在标记内添加字符串HTML.

我在这做错了什么?

Joh*_*nde 6

language_attributes() 不返回属性,它回应它们.

// last line of language_attributes()
echo apply_filters( 'language_attributes', $output );
Run Code Online (Sandbox Code Playgroud)

这意味着它将在您的字符串组装之前显示.您需要使用输出缓冲捕获此值,然后将其附加到您的字符串.

// Not sure if the output buffering conflicts with anything else in WordPress
function wpstudio_doctype() {
  ob_start();
  language_attributes();
  $language_attributes = ob_get_clean();

  $content = '<!DOCTYPE html>' . "\n";
  $content .= '<html ' . $language_attributes . '>';
    echo apply_filters( 'wpstudio_doctype', $content );
}
Run Code Online (Sandbox Code Playgroud)