0

I am trying to set a single page template that will apply to any page which URL begins with confirmation-.

For example (page names):

confirmation-tt5,
confirmation-556,

Should I just create a single-confirmation or will I need a custom filter?

2

We can make use of the page_template filter to achieve this. All we need to do is to make sure that confirmation is the first word in the page page title

add_filter( 'page_template', function ( $template )
{
    // Get the current page object
    $post = get_queried_object();

    /**
     * Get the first instance of confirmation, if it is not 0, bail
     * We will be using the page name, which is the slug
     */
    if ( 0 !== stripos( $post->post_name, 'confirmation' ) )
        return $template;

    // We have confimation as first word, lets continue
    $locate_template = locate_template( 'page-confirmation.php' );

    // Check if $locate_template is not empty, if so, bail
    if ( !$locate_template )
        return $template;

    // page-confirmation.php exist, return it
    return $locate_template;
});
  • 1
    Thanks very much Pieter, you are fast becoming my own personal WP Jedi :) – James Feb 9 '16 at 14:03
  • Ah, this code is overriding all other page templates.. – James Feb 9 '16 at 14:36
  • We just needed a stricter check, !== works, check updated code – Pieter Goosen Feb 9 '16 at 14:54
  • Glad it worked. Enjoy ;-) – Pieter Goosen Feb 9 '16 at 15:03
0
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'confirmation') == true) {

    include(locate_template('page-confirmation.php'));

}

I managed to come up with this solution, not very elegant though as this code needs to be put into default page.php, I would prefer a filter of some sort that does this dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.