• Resolved roclimb

    (@roclimb)


    I just made my first theme and added a functions.php file after testing it because I needed to add a custom navigation menu plugin. All I put in the function.php file was

    <?php

    /* Theme Mega Menu. */
    function register_my_menus() {
    register_nav_menus(
    array( ‘header-menu’ => __( ‘Header Menu’ ) )
    );
    }
    add_action( ‘init’, ‘register_my_menus’ );

    ?php>

    this is all that is in the themes functions.php and everything worked fine to let me add the menu plugin but when I go into my editor to update a page of my theme, it updates but makes the whole page in my wordpress dashboard go completely blank(white). If I go back everything on my site updates and is fine but anytime I update this bug happens. If I remove functions.php from my theme everything is back to normal.

    What should I do to fix this? Do I need to cenfig my functions file with something or add more statements of some sort.

    Thanks for your patience as this is my first theme ever.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi,
    functions.php is the only theme file that does not require a closing php tag <?php ?>

    You have some bad php code the last line ?php>, a closing tag would be ?>, but not required in this file!

    Does the plugin say you need to add this code to functions.php, if so which plugin is it?

    It should be more like this:

    <?php
    /* Theme Mega Menu. */
     function register_my_menus() {
     register_nav_menus(
    	array( 'header-menu' => __( 'Header Menu' ) )
    	);
     }
     add_action( 'init', 'register_my_menus' );

    Do you really need a plugin or is this something you can do without, adding the styles etc, here is my example child theme?

    HTH

    David

    functions.php is the only theme file that does not require a closing php tag

    Um – I beg to differ. No .php file actually needs a closing ?> at the very end of it. ??

    Hi Esmi,
    The OP did have a bad tag in the code.

    Closing php tag is not a php requirement but a WordPress Coding Standard

    Quote from Nancin

    It is the WordPress coding standard to include closing PHP tags on files that are not expected to be edited.

    That excludes a theme’s functions.php and wp-config.php.

    The ‘headers already sent’ argument only works for those files. All core files should not be edited, and should have closing tags.

    WordPress Codex headers already sent errors

    1. Download the file mentioned in the error message via FTP or the file manager provided in your host’s control panel.
    2. Open that file in a plain text editor (NOT MS Word or similar. Notepad or BBEdit are fine).
    3. Check that the very first characters are <?php
    4. Check that the very last characters are ?>

    I learnt php via WordPress and do not use it outside of WordPress, so that is where I picked up the advice in the past, but it is good to know that they are not required Per se.

    Cheers

    David

    It’s a standard for core devs but not for themes & plugins. As one of the Trac commentators said, it’s a coding style. And one that has a valid argument in it’s favour – it avoid “header already sent” issues. But either way, it’s certainly not a PHP requirement and its absence will not cause any issues.

    Cool,
    It seems a standard that most plugin and theme developers seem to use, I did not know it was not a requirement, but it seems to be a good practice.

    If it is a WordPress coding standard then themes and plugins should really follow suit, for code consistancy when other developers are starting out and looking at code.

    Back to the topic ?php> is not valid in this case.

    David ??

    Thread Starter roclimb

    (@roclimb)

    Thnaks so much for the help. I took out the closing tag and everything works perfectly now.

    Thanks both Digital Raindrops and Esmi for taking the time to help.
    Cheers
    Rob

    I am so glad I found this thread as this just happened to me moments ago, I was pulling my hair out! After reading here I yanked the closing tag and my Update and Publish buttons in admin worked like they’re supposed to. Having done just enough PHP outside of WP to be dangerous I was totally unaware that you could leave out closing tags.

    Gotta love www.ads-software.com Support Forums;)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘functions.php Bug in theme’ is closed to new replies.