wordpress plugin unexpected characters

The problem is that we want to write some style code into the frontend but WordPress was complaining that the headers had already been sent.

This means that something had echoed characters before WordPress had got round to setting up the headers. It turned out that the problem was sending some CSS too early.

We want this CSS to be in the <head>and WordPress provides us with a way of doing that with one of its action hooks, wp_head.

What the plugin needs to do is ask WP to add a style element with its attendant styling into the head element at the appropriate time. This is achieved by adding the action like this:

function pluginprefix_hook_css() {
    ?>
        <style>
          all the style we want to add here
        </style>
    <?php
}
add_action('wp_head', 'pluginprefix_hook_css');

Note: I have added ‘pluginprefix’ to the name of the function, and in fact you can call the function what you like, but it is advisable to have a unique prefix for your own plugin so that it cannot clash with any other name used either by WordPress itself or by another plugin.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top