Function to place meta charset="UTF-8" first position in head

Hello, I am receiving a html validator error indicating “charset element found after the first 1024 bytes”.

W3TC lazy loading or EWWW lazy loading is placing a large script in the head prior to meta charset=“UTF-8”.

I tried adding the code below to functions.php. It adds the meta charset in the correct location, however it creates a duplicate.

What would be the correct code to place the existing meta charset=“UTF-8” to the top of head?

add_action('wp_head', 'add_header_code',  -100);
function add_header_code(){
?>
    meta charset="UTF-8"

<?php
};

Hello @jnlinn,

Thanks for writing in! You are inserting the incorrect code. Please refer to this article:

It should be:

add_action('wp_head', 'add_header_code',  -100);
function add_header_code(){
?>
    <meta charset="UTF-8">

<?php
};

The code above serves as an example code. Feel free to modify it when needed. Please note that custom coding is beyond the scope of our support. You will have to maintain any custom coding to make sure that it will still work after any updates or does not create any issues or incompatibility in the future.

Best Regards.

Thank you for the quick response! The code you provided almost works. It inserts <meta charset=“UTF-8”> in the first position in the head.

However, the original <meta charset=“UTF-8”> is still remains in the head. In other words, the code inserts a duplicate. It looks like this:

<head>
<meta charset=“UTF-8”>
<script> very large lazy load script…</script>
<meta charset=“UTF-8”>
etc…
<head/>

How do I remove the second charset? Or, add a function that moves the original meta charset to the first position instead of inserting a second one?

Best regards

Hi @jnlinn,

It is not very clear why the UTF-8 code is coming twice and also not aware of the issue you are having with the W3TC or any other 3rd Party plugin. I would suggest you contact the plugin author as we don’t have control over the code.

Thanks

The <meta charset=“UTF-8”> is coming up twice because I added add_action (‘wp_head’, ‘add_header_code’, -100) to functions.php

If I remove the code, X Theme - Integrity places <meta charset=“UTF-8”> in the head as it should. However, it is placing it at the end of the head section. That’s the issue. I need it to be at the begging of the head section.

How do I get <meta charset=“UTF-8”> to be inserted in the beginning of the head section?

Hi @jnlinn,

By default, the UTF-8 code was added just after the Head as I mentioned in my previous comment that it might be the 3rd Party plugin which injects the code before it. As I said before, we don’t have any control over their code, and you need to contact the Plugin Author on this. They can help you to adjust the priority of the action hook which injects their code after the UTF-8.

Default:

Your Site:

NOTE: You can also deactivate the plugins which inject the code to avoid such problem.

Hope it helps.
Thanks

I understand what you are saying.

This problem is I cannot deactivate the plugins and it would be more complicated to customize their code.

I cannot find in X Theme files where the action hook is located to inject <meta charset=“UTF-8”> into the head. Do you have a possible add_action function I can try adding to functions.php that would change the priority?

Hello @jnlinn,

Please be advised that WordPress and our theme has properly placed the <meta charset="UTF-8"> in the correct place in the header. It is your 3rd party plugin has been causing your issue. The only way to find out which plugin is it is to deactivate your plugin one at a time. Regretfully we do not have any control over this plugins. If you want the line <meta charset="UTF-8"> be inserted before the code inserted by your 3rd party plugin, it is best that you contact the creators of that plugin for further assistance.

Thank you for your understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.