Site logo

Archived topic

Creating a specialized template for a specific page that uses sections

7 replies · Started by Anis on December 1, 2017

Viewing posts 1–8 of 8

Hi,

I have to create a specialized template for a specific page that has sections enabled.

This is because I want to set a cookie on this page.
eg. setcookie('page1', 1, time() + 60, COOKIEPATH, COOKIE_DOMAIN);

... And on the following page, I have to check the expiry status of the cookie and then redirect to specific pages using php redirection.
eg. header('Location:https://www.mysite.com/mypage/');

(Php redirection has to be the first code on a Php page, just after the first <?php opening, i.e. before <HTML> and all other tags. This is what I understand. So hooks or plugins won't work.)

When tested on a site with GP free template installed, it worked. eg. I used a specialized template for the page, like page-mypage.php. I set the cookie as above, and it worked, and was able to use another specialized template for the following page and the redirection worked without any problems.

But on another site with GP Premium 1.5.5 installed and sections enabled, it didn't work. After several attempts and hours of troubleshooting, I think the problem might be that when a page uses sections, it uses a different template, right? It does not use the default WP page.php or in my case, the specialized template page-mypage.php, right?

In that case, how can I create a specialized template for a specific page that uses sections?

Thanks.

Anis

Additionally, I tested on the GP Premium-installed site with a page that doesn't use sections. And of course, it worked!

So a page that uses sections is the concern...

I look forward to your resolution with (sample) code soon. Thanks.

Anis

Thanks, Tom for the guidance. But I am pressed on time. The site is planned to be launched this Tuesday. So I request detailed help with code asap.

Is this the way I should be implementing?

(1) Create a custom post type. (How to do that? Please help me.)

(2) Create a custom template that uses sections for the custom post type created in step 1. (I definitely need CODE here! :( )

Thanks in advance.

Anis

And... Where in the file system do I create a custom sections template? Any pre-existing (or default) template that I can get started with to apply my additions/modifications?

:(

1. Copy the file in wp-content/plugins/gp-premium/sections/functions/tempates

2. Paste this file into your child theme and make any adjustments. Name it generate-sections-page-template.php.

Then use this function to target the page you want to use the custom template on:

add_filter( 'template_include', 'tu_sections_custom_page_template', 20 );
function tu_sections_custom_page_template( $template ) {
    global $post;
    $use_sections = ( isset( $post ) ) ? get_post_meta( $post->ID, '_generate_use_sections', TRUE) : '';

    if ( isset( $use_sections['use_sections'] ) && 'true' == $use_sections['use_sections'] && is_page( 'Your Custom Page' ) ) {	
		
        $new_template = get_stylesheet_directory() . '/generate-sections-page-template.php';
		
        if ( '' != $new_template ) {
            return $new_template;
        }
    }

    return $template;
}

Adjust the Your Custom Page to the name of the page you're targeting.

That should do it :)

Thanks a lot, Tom! It worked... :)

Anis

You're welcome :)

This archived topic is closed to new replies.