2

This is a followup to my question, "How can I have two different urls for the same page that load two different templates?"

Assuming I implement Milo's answer (registering a new rewrite endpoint, load a different template if endpoint is present), how can I then have links within the page rewritten on the fly to include the endpoint (e.g. app-view).

The page the initial endpoint would be added to is: https://liquidchurch.com/messages/

Based on Milo's instructions using the rewrite endpoint and page template filter, the url I'd pass from the mobile app would be: https://liquidchurch.com/messages/app-view/

Now on this page say I click on one of the series, e.g.:

https://liquidchurch.com/sermon-series/divine-direction/

Problem is this will load with the regular template, not the template created particularly for the mobile view. So somehow I have to pass on to this page that it should be displayed in app-view. So the actual URL the individual should be redirected to would be:

https://liquidchurch.com/sermon-series/divine-direction/app-view/

In what way could one accomplish this? Thanks!

1
  • Shouldn't the suggestion given by Milo work on all your pages for the in-app version?
    – Brent
    Commented Jan 16, 2018 at 19:06

1 Answer 1

1

You can use the page_link filter with the same logic as the page_template filter in the other answer. If the endpoint query var is set, then any link to a page gets the app-view/ appended:

function wpd_page_link( $link, $post_id ){
    global $wp_query;
    if( isset( $wp_query->query_vars['app-view'] ) ){
        return $link . 'app-view/';
    }
    return $link;
}
add_filter( 'page_link', 'wpd_page_link', 10, 2 );
4
  • Hi Milo - This doesn't seem to be working for me. The page_template filter works, but when I click on a link from liquidchurch.com/messages/app-view it displays the page as: liquidchurch.com/sermon-series/dream-team Not as: liquidchurch.com/sermon-series/dream-team/app-view as I expected?
    – davemackey
    Commented Jan 22, 2018 at 19:09
  • 1
    page_link only works for the page post type. Each type of content has its own filter- post_link, post_type_link, term_link.
    – Milo
    Commented Jan 22, 2018 at 20:03
  • So, messages is a page, but then the content on it is primarily from a plugin. Is there a way to have it affect a plugin? And what about custom post types?
    – davemackey
    Commented Jan 22, 2018 at 21:38
  • I found the post_type_link filter, and tried this: function wp_messages_link( $link, $post_id ) { if ( 'lqd-messages' == get_post_type( $post_id ) ) { return add_query_arg( $_GET, $link ); } return $link; } add_filter( 'post_type_link', 'wp_messages_link', 10, 2 ); But this doesn't seem to be working. Any ideas what I'm doing wrong?
    – davemackey
    Commented Jan 22, 2018 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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