• I have defined a child theme bases on twenty twenty-three with the aim of defining a bunch of dynamic virtual pages ( adding some add_rewrite_rule at init, some query_var and trapping an add_filter(‘template_include’ …?) )

    in this ‘template_include’ filter, I load my own PHP template as I did pre WP 6.3

    ?
    I succeed in loading the “template” I have designed in WP, and everything is correctly process ( shortcodes, header and footer blocks ) but it lacks the “top” of the file ( <html><head>…<body> and thus the formatting… )

    I do this in my virtual template page :

    $modelTemplate = get_post( 766 ); // id of the template
    $content = apply_filters( 'the_content', $modelTemplate->post_content );
    echo $content;

    What is a correct way to do it now ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter solariane

    (@solariane)

    BTW I forgot to mention that calling
    get_header() before and get_Footer(); after the previous code doesn’t work as expected because it doesn’t strictly apply / respect the theme configuration

    Hi @solariane,

    I’m checking this further and we’ll follow up here as soon as possible. ??

    Thread Starter solariane

    (@solariane)

    @foosantos thank you for your help.

    Two things that might help :

    • action wp_body_open is not called
    • the “basic / default” WP visual header and footer – I mean the one inside <body> are called with my above “solution”

    So for now, I do some “post-processing” : get rid of those html fragment through DOMXpath and rewrite some tags in the <head>

    A cleaner way to do it, might be to build a $post object and than apply the model on it…

    If these are PHP-based templates that you are including, you need to add the wrapping code yourself. Something like this at the top of the file:

    <!doctype html>
    <html <?php language_attributes(); ?>>
    <head>
    	<meta charset="<?php bloginfo( 'charset' ); ?>" />
    	<meta name="viewport" content="width=device-width, initial-scale=1" />
    	<?php wp_head(); ?>
    </head>
    
    <body <?php body_class(); ?>>
    <?php wp_body_open(); ?>

    And something like this at the bottom of the file:

    <?php wp_footer(); ?>
    
    </body>
    </html>

    You could also stick that code into a header.php and footer.php file and call them with get_header() and get_footer(), respectively.

    Thread Starter solariane

    (@solariane)

    @greenshady yes it’s way cleaner with the header.php and footer.php trick.

    Still have some incorrect stuff in the <head> ( title, canonical ) which might be set manually

    Do you know if it’s possible from PHP to something even “cleaner” in my eyes : Create a $p = WP_Post ( with my virtual URL and the correct Title + the info specifying which wp_template to apply ) and then do :
    get_header();
    apply_filters( 'the_content', $p->post_content )
    get-footer();

    I’m not 100% sure on what you’re asking. If you can explain in a bit more detail, I may be able to help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Virtual Page + template’ is closed to new replies.