2

I need to create a frontend page (and sub-pages) which will display custom data that is saved in the database but is not part of wordpress, it is not a post or page as such but API called data. I do not want to use a short code because of the need of sub-pages.

I have looked at the following but don't seem to be able to make it work as required: Dynamic URL, not a physical page within the database

What I need is to have for example site.com/mypage/ but I do not want 'mypage' to exist in the backend.

I have achieved this with:

public function mypage_rewrite_ext() {
    global $wp_rewrite;
    $plugin_url = plugins_url( 'mypage.php', __FILE__ );
    $plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );
    // The pattern is prefixed with '^'
    // The substitution is prefixed with the "home root", at least a '/'
    // This is equivalent to appending it to `non_wp_rules`
    $wp_rewrite->add_external_rule( 'mypage$', $plugin_url );
}

But I need my page to still be within WordPress as I need to use the header and footer.

I have tried the following from the above linked question, but this just redirects to the home page (even after re-saving permalinks):

public function add_query_vars( $query_vars )
{
    $query_vars[] = 'test';
    return $query_vars;
}

public function add_endpoint()
{
    add_rewrite_rule('^mypage/?', 'index.php?__test=1', 'top');
    flush_rewrite_rules(false); //// <---------- REMOVE THIS WHEN DONE
}

public function sniff_requests($wp_query)
{
    global $wp;

    if(isset($wp->query_vars[ '__test' ])) {
        add_filter('template_include', function ($original_template) {

            // change the default template to a google map template
            return plugin_dir_path( __FILE__ ) . 'mypage.php';
        });
    }
}

Any ideas on 1) what I am doing wrong and/or 2) how I can achieve this?

This is the result from the analyser: enter image description here

  • Just curious, why are you averse to having actual WP 'pages' for these? You could assign them each a custom Page template that pulls in your non-WP data (as well as WP header/footer) and note in the content editor that the page content is controlled in the theme, not in content editor. – WebElaine Apr 13 '17 at 15:39
  • Because it's going to be a custom hierarchy for 1000's of items, so I don't want to have to make a page per one, or use a shortcode as I will need to eventually have /mypage/myotherpage/mylastpage/ – Aravona Apr 13 '17 at 15:41
  • Have you tested with monkeyman rewrite rules analyser plugin to verify that the URL you expected actually matches the rewrite rule? Note that you added test as a query variable, but you used instead __test which isn't the same – Tom J Nowell Apr 13 '17 at 16:16
  • @TomJNowell I have it there but to be honest I'm not sure what to expect from it. See attached image (Just updating with the correct var as well, didn't notice that - which now make 'mypage' go to the archive page) – Aravona Apr 13 '17 at 16:21
  • 1
    @Aravona Don't delete the question, answer it! Who knows who else might have the same issue but not spot the problem as you did – Tom J Nowell Apr 13 '17 at 16:29

Your Answer

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

Browse other questions tagged or ask your own question.