0

I'm using this function below in Wordpress:

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

Problem is that the function displays $content above the <!DOCTYPE html> tag, instead of adding the string inside of the HTML tag.

What am I doing wrong here?

2 Answers 2

6

language_attributes() does not return the attributes, it echos them.

// last line of language_attributes()
echo apply_filters( 'language_attributes', $output );

This means it will be displayed before your string is assembled. You need to capture this value using output buffering and then append it to your string.

// 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 );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi John, thanks for the answer. I understand, but an example would help - would you be willing? Thanks either way and I will now be able to fix this.
Hi John, this already worked perfect - accepting answer here in a few minutes - thanks so much
0

Instead of output buffering, just use get_language_attributes() inside an ECHO statement. In this specific case:

function wpstudio_doctype() {
  $content = '<!DOCTYPE html>' . "\n";
  $content .= '<html ' . get_language_attributes() . '>';
    echo apply_filters( 'wpstudio_doctype', $content );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.