0

I would want to show some HTML code in my WordPress page. So in my child theme functions.php, I have written an action like this one:

add_action('wp', function() {
    if(is_admin()) {
        return;
    }
    
    $html = '<div class="home_immodvisor">';    

    echo $html; 
});

However, a warning is displayed when I go to the back-office (admin) login page:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/themes/hello-elementor-child/functions.php:88) in /var/www/html/wp-includes/pluggable.php on line 1296

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/themes/hello-elementor-child/functions.php:88) in /var/www/html/wp-includes/pluggable.php on line 1299

My question is: is it the good action to hook? Or should I use another one than wp?

JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70

3 Answers3

1

From my understanding wordpress sends headers after wp so this is the wrong place, since you cannot echo before.

maggiathor
  • 174
  • 6
  • Thank you maggiathor for your answer. In fact, I would want to `echo` on the front page, not the admin one. What action could I use? – JarsOfJam-Scheduler Sep 08 '20 at 09:15
  • Where exactly in the theme do you want to echo? Every hook within the body is defined by the theme. Wordpress general template is using wp_body_open and most themes use that too, but you cannot bet on it's existence in a custom theme. Normally I would recommend adding something like that in the header.php directly. – maggiathor Sep 08 '20 at 09:20
  • Thank you again. The content should be echoed as one root element in the body element. :-) – JarsOfJam-Scheduler Sep 08 '20 at 09:37
  • Then after wp_body_open should work, if it doesn't not exists in your theme you can just add do_action('wp_body_open'); – maggiathor Sep 08 '20 at 09:45
0

There is no standardized hook/action for inserting a html in a theme. Every theme could handle this differently, some might not even have a hook.

But if you must the wp_head hook will work, but it will not be valid HTML.

The better thing todo is check the documentation of your thema. Or make a child-theme and adjust the tempaltes) you need

janw
  • 6,672
  • 6
  • 26
  • 45
-2

Warning: Cannot modify header information - headers already sent by

To solve this add the below code in theme function.php file

ob_start();

It will remove your error.