WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

#18390 closed enhancement (wontfix)

apply_filters for a theme's functions.php

Reported by: ofshard Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2.1
Component: Themes Keywords: close
Focuses: Cc:

Description

My primary intention with this suggestion is for switching a page's theme on the fly. Currently the only method I know of for this is to change the active theme before functions.php is included and then switching back sometime after, which I don't find very thread safe.

Anyway, I suggest that lines 278-283 in wp-settings.php which currently read:

if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
	if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
		include( STYLESHEETPATH . '/functions.php' );
	if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
		include( TEMPLATEPATH . '/functions.php' );
}

be altered to implement the 'theme_functions' and 'theme_parent_functions' filters as below:

if ( !defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ){
	$path = (file_exists(STYLESHEETPATH . '/functions.php')) ? STYLESHEETPATH . '/functions.php' : '';
	$functions = apply_filters('theme_functions', $path);
	if( $functions != '' )
		include($functions);
	
	$path = (TEMPLATEPATH !== STYLESHEETPATH && file_exists(TEMPLATEPATH . '/functions.php')) ? TEMPLATEPATH . '/functions.php' : '';
	$functions = apply_filters('theme_parent_functions', $path);
	if( $functions != '' )
		include($functions);
}

Change History (6)

comment:1 in reply to: ↑ description ofshard3 years ago

Well, I believe you can alter the functions.php included by filtering 'stylesheet_directory' and 'template_directory' in the action 'setup_theme' as well (which is thread safe). The suggestion still stands though.

comment:2 dd323 years ago

See also: #18298 - Deprecate those constants and use the function counterparts. Those functions have filters which would also kick in for this scenario I believe.

comment:3 ofshard3 years ago

Incorporating that, my suggestion would look like this instead:

if ( !defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ){
	$stylesheet_dir = get_stylesheet_directory();
	$template_dir = get_template_directory();

	$path = (file_exists($stylesheet_dir . '/functions.php')) ? $stylesheet_dir . '/functions.php' : '';
	$functions = apply_filters('theme_functions', $path);
	if( $functions != '' )
		include($functions);
	
	$path = ($template_dir !== $stylesheet_dir && file_exists($template_dir . '/functions.php')) ? $template_dir . '/functions.php' : '';
	$functions = apply_filters('theme_parent_functions', $path);
	if( $functions != '' )
		include($functions);
}

comment:4 dd323 years ago

Why exactly do you need to filter the Functions.php location if you can completely change the active theme on the fly using filters that already exist such as template_directory and those within get_stylesheet_directory()?

The Theme Switcher Plugin does exactly that.

comment:5 solarissmoke2 years ago

  • Keywords close added

comment:6 SergeyBiryukov2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

As dd32 pointed out, it's possible to switch a theme on the fly using existing filters.

Note: See TracTickets for help on using tickets.