Discuss your project
Wordpress Front

WordPress: Override a template when a variable is present in the URL

In very rare cases, one might need to override the default template (although it's a bit dirty to do that).In the functions.php file, add the following code: function wpa_overload_template( $template ) { if( isset( $_GET['parameter'] ) ) { $template = loc...

WordPress: Override a template when a variable is present in the URL

In very rare cases, one might need to override the default template (although it's a bit dirty to do that).

In the functions.php file, add the following code:

function wpa_overload_template( $template ) {

if( isset( $_GET['parameter'] ) ) {

$template = locate_template('template-overload.php', false );
}

return $template;
}
add_filter( 'template_include', 'wpa_overload_template' );

Reproduce this customization today

The principle of the hook remains useful, but the administration screens and WordPress APIs have evolved since the publication. Test the code in a staging environment with the exact version of the site's core, theme, and plugins.

  • Save the files and the database before making any changes.
  • Control user capabilities, nonces, input validation, and output escaping.
  • Place customizations in a dedicated plugin or a child theme to preserve updates.

Reference: official WordPress plugin development manual.

Share this article