Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#51726 new enhancement

proposal: make specialized header name available inside header.php

Reported by: jules-colle's profile Jules Colle Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.5.2
Component: Themes Keywords: needs-patch
Focuses: Cc:

Description

You can call get_header() with an extra argument. For example: get_header('wp-signup'). This allows theme authors to create a specific header: header-wp-signup.php.

But it would be nice to also this name inside the regular header.php file, to allow for a small tweak instead of completely duplicating the header.php file.

My proposal is to modify this function in wp-includes/general-template.php line 27:

<?php
function get_header( $name = null, $args = array() ) {
        /**
         * Fires before the header template file is loaded.
         *
         * @since 2.1.0
         * @since 2.8.0 The `$name` parameter was added.
         * @since 5.5.0 The `$args` parameter was added.
         *
         * @param string|null $name Name of the specific header file to use. Null for the default header.
         * @param array       $args Additional arguments passed to the header template.
         */
        do_action( 'get_header', $name, $args );

        $templates = array();
        $name      = (string) $name;
        if ( '' !== $name ) {
                $templates[] = "header-{$name}.php";
        }

        $templates[] = 'header.php';

        if ( ! locate_template( $templates, true, true, $args ) ) {
                return false;
        }
}

We could call locate_template like this:

<?php
locate_template( $templates, true, true, array_merge($args,[ '__name' => $name ]) )

Then, inside header.php it would be possible to access the header name like this:

$header_name = $args['__name']

Change History (5)

#2 @SergeyBiryukov
4 years ago

  • Keywords reporter-feedback added

Hi there, welcome back to WordPress Trac! Thanks for the ticket.

If I understand correctly, you'd like to use header.php instead of header-wp-signup.php, and just pass an additional parameter to it to make some tweaks?

In that case, you can utilize the $args parameter added in [48370] / #21676.

If you call get_header() like this:

get_header( null, array( '__name' => 'wp-signup' ) );

you'll have access to $args['__name'] in header.php. Would that accomplish the task?

#3 @Jules Colle
4 years ago

Thanks for the quick reply Sergey. Not really. The proposed change is aimed at default wordpress pages, like wp-activate.php and wp-signup.php, these files are calling resp. get_header( 'wp-activate' ); and get_header( 'wp-signup' );

Now, there seems to be no way to detect this parameter inside header.php (without changing the core file.)

Actually, I just realized that the 2 pages mentioned are about the only pages where this applies... So the severity of this ticket can be lowered even more if you want.

#4 @SergeyBiryukov
4 years ago

Ah, thanks for the clarification, it makes more sense to me now :)

#5 @SergeyBiryukov
4 years ago

  • Keywords reporter-feedback removed
Note: See TracTickets for help on using tickets.