5

I have the following page:

http://liquidchurch.com/messages/

It appears fine on desktop and mobile web.

I'm working on a mobile app and using a web view to encapsulate the page within the app. Problem is I don't want the header/footer to be part of the page when it is viewed in the mobile app, but I do want them to be visible when using either desktop or a mobile browser outside the app.

Any ideas on how this can be accomplished or if I'm going about it all wrong? Thanks!

7

You could accomplish this with a rewrite endpoint and a template filter.

First, register a new rewrite endpoint for the page post type:

function wpd_app_view_rewrite_endpoint() {
    add_rewrite_endpoint( 'app-view', EP_PAGES );
}
add_action( 'init', 'wpd_app_view_rewrite_endpoint' );

Don't forget to flush rewrite rules after adding this (or just visit the Permalinks > Settings page to do this without code).

Now you will be able to add app-view/ on the end of any page permalink URL, for example, your page will be domain.com/messages/app-view/.

The next step is to detect when app-view/ is present, and load a different template in that case. For the page post type we use the page_template filter:

function wpd_app_view_page_template( $template ) {
    global $wp_query;
    if( isset( $wp_query->query_vars['app-view'] )  ) {
        $template = locate_template( array( 'app-view-template.php' ) );
    }
    return $template;
}
add_filter( 'page_template', 'wpd_app_view_page_template' );

This checks if query_vars['app-view'] is set in the global $wp_query object, and loads the app-view-template.php template in this case. This will only be true when the app-view rewrite rule matches the current request.

  • Hi Milo! Thanks so much for this answer, it is very helpful! – davemackey Jan 16 '18 at 17:51
  • Do I need to create separate functions for each type of template? e.g., archive, taxonomy? – davemackey Jan 23 '18 at 21:24
  • You can hook the same function with multiple filters, or you can use template_include hook, which fires for all content types. – Milo Jan 23 '18 at 22:46
  • Thanks Milo! I am having an issue when I try to set this up using filters for a custom taxonomy, any thoughts on what I'm doing wrong? wordpress.stackexchange.com/questions/291987/… – davemackey Jan 23 '18 at 22:49

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.