0

Could someone have the same error when im trying to register a menu?

function.php

function register_my_menu() {
  register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );

error

function register_my_menu() { register_nav_menu('header-menu',__( 'Header Menu' )); } add_action( 'init', 'register_my_menu' ); Warning: Cannot modify header information - headers already sent by (output started at /home/milibyte/public_html/wp-content/themes/test/functions.php:4) in /home/milibyte/public_html/wp-admin/includes/misc.php on line 1126

GitHub

Regards!

6

3 Answers 3

1
<?php
function register_my_menu() {
    register_nav_menu( 'header-menu',__( 'Header Menu' ) );
}
add_action( 'after_setup_theme', 'register_my_menu' );
2
  • is not nesesary to close ? "?>"
    – fdesu
    Aug 22, 2018 at 22:12
  • Not unless you need to switch over to HTML mode. I never end a file with ?> too often there is a hidden space or too many carrage returns after, and that can cause similar troubles like you had
    – De Coder
    Aug 22, 2018 at 22:14
1

The error may display itself when there are:

  • Blank lines at the top or bottom of a document
  • Too many spaces before, in between or after the beginning PHP tags
  • There are too many unneeded spaces or blank lines in a specific part of a document

Just to debug, try to do something like this.

function register_my_menu() { 
     //do nothing 
} 
add_action( 'init', 'register_my_menu' );

If you Google that issue you will get amazed by how many people have that just because of extra spaces or wrong things on the functions.php.

6
  • but my functions.php is so small!!
    – fdesu
    Aug 22, 2018 at 21:15
  • It doesn't matter, we just need one character in a wrong position to make it it fails, did you check //do nothing? Aug 22, 2018 at 21:16
  • exist something to check the lines?
    – fdesu
    Aug 22, 2018 at 21:16
  • ahh sorry, yes i check and the problem persist!
    – fdesu
    Aug 22, 2018 at 21:17
  • So, we know is something wrong in you functions.php, since it's small you can remove everything and start pasting back small pieces of codes to see when the error shows up again. Aug 22, 2018 at 21:18
0

I believe you are running your action too soon.

Don't run it at 'init', use 'after_setup_theme' instead.

add_action( 'after_setup_theme', 'register_my_menu' );

6
  • thanks, but the problem persist.
    – fdesu
    Aug 22, 2018 at 20:42
  • Do you have php wrappers around the code? If not, that could cause this.
    – De Coder
    Aug 22, 2018 at 20:52
  • i did, I use notepadd c++ with no BOM and check wrappers.
    – fdesu
    Aug 22, 2018 at 21:00
  • The way you list the message, it looks like it's spitting out your php code, then saying headers are already sent. This sounds like you did not start the file with <?php
    – De Coder
    Aug 22, 2018 at 21:05
  • i put it in github.
    – fdesu
    Aug 22, 2018 at 21:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.