Skip Navigation

[Resolved] Preselect specific Layout for “pages” created by NextGen Gallery

This thread is resolved. Here is a description of the problem and solution.

Problem: I have Layout A assigned as the Layout for the Pages post type. However, under certain circumstances I would like to preselect Layout B when a Page is being created in wp-admin.

Solution: There's not a filter available to manipulate the selected Layout here, but you could use the WordPress "save_post" hook to override the User's Layout selection when the post is saved.

function override_default_layout( $post_id ) {
    if( get_post_type($post_id) == 'page' && wp_get_post_parent_id( $post_id ) == 1234 ) {
           // this page is a child of the page with ID 1234 so we need to update the _layouts_template postmeta
           update_post_meta( $post_id, '_layouts_template', 'template-slug');
        }
}
add_action( 'save_post', 'override_default_layout', 100 );

Change '1234' to match the numeric ID of the Photos page, and change 'template-slug' to match the slug of the correct Layout. This way, no matter what is selected, the correct Layout will be applied.

Relevant Documentation: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by webD-3 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#589566

As part of the "Add Gallery" workflow in NextGen Gallery/NextGen Plus, a user has the ability to "Create new page" (upon creation of the gallery a new Page with shortcode is created.)

To make the workflow as smooth as possible I'd like for my client to be able to simply "Create new page" from within NextGen while creating the gallery. The issue is that I have a custom Layout for galleries, but by clicking "create new page" Layouts is automatically assigning the default Layout I have specified for the generic "Pages" post type.

Is there a way that I can set a separate default Layout for Pages that meet specific conditions? In this case the new page being created is a child of existing parent page "Photos." Could all children of Photos get the Gallery Layout by default? Or is there some other condition that could enable Toolset to apply the desired Layout upon creation of the gallery page?

Please let me know if I have not described this issue clearly.

#589627

The default Layout will always be selected by default in the new Page editor, but you could override that selection using the save_post hook whenever the Page is saved:

function override_default_layout( $post_id ) {
	if( get_post_type($post_id) == 'page' && wp_get_post_parent_id( $post_id ) == 1234 ) {
           // this page is a child of the page with ID 1234 so we need to update the _layouts_template postmeta
           update_post_meta( $post_id, '_layouts_template', 'template-slug');
        }
}
add_action( 'save_post', 'override_default_layout', 100 );

Change '1234' to match the numeric ID of the Photos page, and change 'template-slug' to match the slug of the correct Layout. This way, no matter what is selected, the correct Layout will be applied.

#589642

Christian,

Thank you. I have added the function you provided, created a new gallery via NextGen, and the generated page correctly applied the desired Layout. I appreciate your quick and accurate reply!