0

I have already solved my actual problem, but the reason that led to that error I do not understand. I am writing a wordpress child theme. In the functions.php a file is required.

require_once('templates/header.php');

in the file there are several functions:

<?php
add_action('generate_menu_bar_items', 'add_account_icon_menu');
function add_account_icon_menu()
{ ?>
    <span class="menu-bar-item">
        <a href="/my-account">
            <span class="gp-icon">
                <?= get_svg(["name" => "circle-user.svg"]) ?>
            </span>
        </a>
    </span>
<?php } ?>
<?php
 add_filter(
     'generate_navigation_search_output',
     function () {
         printf(
             '<form method="get" class="search-form navigation-search" action="%1$s">
     <input type="search" placeholder="Enter your search term..." class="search-field" value="%2$s" name="s" title="%3$s" />
     <input type="hidden" name="post_type" value="product" />
 </form>',
             esc_url(home_url('/')),
             esc_attr(get_search_query()),
             esc_attr_x('Search', 'label', 'generatepress')
         );
     }
 );
?>

The point is on my local environment this worked great, but on my hosters websever I got the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home/www/dev/wp-content/themes/generatepress-child/templates/header.php:1) in /home/www/dev/wp-includes/functions.php on line 6584

Warning: Cannot modify header information - headers already sent by (output started at /home/www/dev/wp-content/themes/generatepress-child/templates/header.php:1) in /home/www/dev/wp-admin/includes/misc.php on line 1310

Warning: Cannot modify header information - headers already sent by (output started at /home/www/dev/wp-content/themes/generatepress-child/templates/header.php:1) in /home/www/dev/wp-admin/admin-header.php on line 9

It took me a long time to figure out that empty lines between and/or before the functions cause these errors. Why this? Am I not allowed to have empty lines between PHP blocks?

Homer Pitt
  • 25
  • 6
  • 1
    A common culprit is the final closing `?>` of a PHP file which is never needed, and most, if not all style guides actively recommend to never use – Chris Haas Sep 02 '21 at 17:04
  • Empty lines before or after the functions won't make a difference. It's empty lines before ` – aynber Sep 02 '21 at 17:07
  • 1
    Also, if you are on Windows, a common problem is a [BOM in the file](https://stackoverflow.com/a/2558793/231316) – Chris Haas Sep 02 '21 at 17:10
  • yeah, my machine is windows and i use XAMPP as local enviroment. – Homer Pitt Sep 02 '21 at 17:15
  • Get a good hex editor such as [XVI32](http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm) and inspect your file. Then get a good IDE/text editor that can ignore the BOM. – Chris Haas Sep 02 '21 at 17:21

0 Answers0