WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#41152 new feature request

New Function to Require a file

Reported by: rinkuyadav999 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Themes Keywords: close
Focuses: Cc:
PR Number:

Description

Right now theme developers need to use php require and file path to require a file. It will better, WordPress code provide a function to supply path of file in a function and it will do reset of work. Like:

<?php
/**
 * Require the file of parent theme.
 *
 * @since 4.9.0
 *
 * @param string $file Required. File to require.
 * @return required the file.
 */
function wp_require_parent_theme_file( $file = '' ) {
        $file = ltrim( $file, '/' );

        if ( ! empty( $file ) ) {
                $path = get_template_directory() . '/' . $file;
        } else {
                return;
        }

        require $path;
}

Attachments (1)

link-template.php (136.3 KB) - added by rinkuyadav999 2 years ago.
Functino in Core file

Download all attachments as: .zip

Change History (16)

@rinkuyadav999
2 years ago

Functino in Core file

#2 follow-up: @SergeyBiryukov
2 years ago

  • Keywords close added
  • Version trunk deleted

Hi @rinkuyadav999, thank you for the ticket!

This is already possible with get_template_part(), what's the benefit of the new function?

#3 @joyously
2 years ago

No new function is needed. Several useful functions were added in 4.7.
One is here: https://developer.wordpress.org/reference/functions/get_theme_file_uri/

The reason we don't need another function to do a require is that files included/required inside of a function have that function as its namespace. This is good for template files, but not much else.

#4 @rinkuyadav999
2 years ago

Hi @joyously

get_theme_file_uri function does not do what i need. and load_template does that task https://developer.wordpress.org/reference/functions/load_template/ :) ( ! Retrieve the name of the highest priority template file that exists. )

wp_require_parent_theme_file will be more user friendly and dedicated if it adds.

Last edited 2 years ago by rinkuyadav999 (previous) (diff)

#5 in reply to: ↑ 2 @rinkuyadav999
2 years ago

Seems developer uses get_template_part() function to add part of template instead part of functions.php file. Expecting a dedicated function to require a file (part of functions.php).

Replying to SergeyBiryukov:

Hi @rinkuyadav999, thank you for the ticket!

This is already possible with get_template_part(), what's the benefit of the new function?

#6 @joyously
2 years ago

wp_require_parent_theme_file will be more user friendly and dedicated if it adds.
Seems developer uses get_template_part() function to add part of template instead part of functions.php file. Expecting a dedicated function to require a file (part of functions.php).

You missed the point about the variables available when require is used inside a function. It is better without the function, because the environment is as expected instead of limited to that function.
The new functions added in 4.7 check the child first and then the parent, so no new functions are needed. They make it easier for the child to replace individual files of the parent.

#8 @rinkuyadav999
2 years ago

wp_require_parent_theme_file function and it's code does not matter.

Question: I need a function to do:

<?php
// Now i need to do
require get_template_directory() . '/inc/init.php';

//Expecting
some_func( '/inc/init.php' );

//some_func will do reset of work.
Last edited 2 years ago by rinkuyadav999 (previous) (diff)

#9 @joyously
2 years ago

Yes, it helps to explain why you should not do what you are suggesting!

The include set of directives generate a runtime ZEND_INCLUDE_OR_EVAL operation which calls the Zend compiler to compile the referenced file. So in general you should not embed include statements in a function, as:

  • The include will be executed every time that code path is taken when the function is called. Compiling the same bit of code 100s of times is a bad idea.
  • If the code contains elements of global scope (e.g. function or class declarations) then executing that declaration even twice will cause compiler errors.

So don't unless you know what you are doing. Use techniques such are those described by Albatrosz. Incidentally his __autoload() function is just the sort of example of an exception where this is valid to do.

#10 @rinkuyadav999
2 years ago

You are not pointing what i am suggesting. Simply: https://core.trac.wordpress.org/ticket/41152#comment:8

#11 @joyously
2 years ago

Well, before you edited it, I was. And why introduce a problem when two lines of code does the job without the problem?

#12 @rinkuyadav999
2 years ago

  • Keywords dev-feedback added

Hi @joyously

Please do not move on problem and code. Right now i think we should discuss about 'do we need a function to require a file in theme (part of theme functions.php file)'. also 1-2 line of code does not matter because we have 'absint' function which is also 1 line function.

#13 @swissspidy
2 years ago

  • Keywords reporter-feedback added; dev-feedback removed

absint() is not a great example because it's often used as a callback for things like array_map( 'absint', $numbers ).

Looking at the example code, I don't think we need (yet another) a function to require a file in a theme. We have get_template_part() (which does a bit more than just a require). You can use require get_template_directory() . '/file.php';. You can also use require dirname( __FILE__ ) . '/file.php;.

Looking at the example code, this wouldn't allow me to do something like require wp_require_parent_theme_file( 'file.php' ); because the function can return null. That doesn't seem user-friendly to me.


@rinkuyadav999 Since you opened the ticket, can you perhaps show a few examples where such a function might be really useful? For example, would the default themes greatly benefit from something like that?

#14 @rinkuyadav999
2 years ago

Hi @swissspidy

Right now theme developer needs to use php require and file path. like require get_template_directory() . '/file.php'; OR require get_parent_theme_file_path( '/inc/icon-functions.php' );

It will better, WordPress provide us a function to just add location of file inside a function and it will require. This is to include part of functions.php file. get_template_part() used to add part of a theme file like get_template_part( 'template-parts/post/content', get_post_format() );

get_parent_theme_file_path function do almost same task but it does not require passed file instead it return proper path of file.

PS: i am expecting a function like wp_require_theme_file( '/inc/icon-functions.php' ); and it will do require get_template_directory() . '/inc/icon-functions.php';

#15 @rinkuyadav999
2 years ago

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