0

I'm working with the paid child Wordpress theme which I can not change because after it's update all my changes would be lost. I need to change post template (for castom post type, in my case it's single-listing.php). How can I add some blocks to template without changing template file?

0

I'm not sure you can - if you can't make a child of a child theme ( which I'm fairly certain you can't ) and the main theme doesn't supply any hooks - the only real thing you could do is hook into get_footer(); but that may be too late.

0

Filter template_include, use output buffering to catch the rendered file content and DOMDocument to change it.

Untested example:

add_filter( 'template_include', function( $template ) {

    // Bail out early.
    if ( 'the-file-you-want-to-filter' !== $template )
        return $template;

    get_header();

    ob_start();

    include $template;

    $content = ob_get_clean();

    // See user notes on http://php.net/manual/en/domdocument.loadhtml.php
    $doc = new DOMDocument();
    $doc->loadHTML( '<?xml encoding="UTF-8">' . $content );

    // dirty fix
    foreach ($doc->childNodes as $item)
        if ($item->nodeType == XML_PI_NODE)
            $doc->removeChild($item); // remove hack
    $doc->encoding = 'UTF-8'; // insert proper  

    /**
     * Edit the file content here.
     */
    echo $doc->saveHTML();  

    get_footer();

    return FALSE;
});

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.