• Resolved Adam Michael

    (@adammichael)


    Hello,

    I would like to change my template for every page after click on the link.

    For example: I have a page with name for example Page 1 with URL domain.com/page1/ and with basic template page.php
    I want the following thing: after click on the button in this page, the content remains the same (still “Page 1”, but the template file will be changed to newtemplate.php

    Is there any possible way to do that?

    PS: I don’t want to create two different pages with same content, because I want to apply this rule to all pages…

    Thanks a lot for help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,
    It sure is possible, however, you need some custom code for it. You will probably need to use the page_template filter. Here is some example code to start with, you can add it to the functions.php file of your theme:

    function mts_set_new_template( $template ) {
        if ( isset( $_GET['pagetemplate'] ) && $_GET['pagetemplate'] == '1' ) {
            $template = dirname( __FILE__ ) . '/newtemplate.php';
        }
    
        return $template;
    }
    add_filter( 'page_template', 'mts_set_new_template' );

    Then you need to create the button/link that adds the pagetemplate URL parameter: yourdomain.com/yourpage?pagetemplate=1
    Hope that helps.

    Thread Starter Adam Michael

    (@adammichael)

    Hi,
    this solution works absolutly perfect!

    Thanks a lot.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change page template after click’ is closed to new replies.