How to apply the_content filter in function get_the_content WordPress?

3

I'm developing a theme for WordPress, for hobby and for studying a little PHP, JavaScript etc. and I'm having a little problem.

I'm working with the controller concept where all logic will be processed and then sent to a view. To make it clear how my theme is structured I'll go over my directory structure:

Thecontroller-single.phpwillcontrolthedatathatwillberenderedinsingle.phpinitIdefinetheController_Singleclassthatwillmanagethecontentsofthepage/postsandthenIcallthenecessarypartstoanddisplaythecontents:

<?phprequire_once__DIR__.'/../security.php';classController_Single{publicfunction__construct(){add_filter('get_single_content',array($this,'get_single_content'));}publicfunctionget_single_content(){$single_content=newStdClass();$single_content->title=get_the_title();//TítulodoPost$single_content->author=get_the_author();//AutordoPost$single_content->date=get_the_date();//Datadepublicação$single_content->content=get_the_content();//ConteúdodoPostreturn$single_content;}}newController_Single();

Andinmysingle.phpIperformthecallofcontent:

<?phpget_header();the_post();$single_post=apply_filters('get_single_content',false);?><headerclass="glory-header">
    <div class="glory-header-title">
        <h2><?php echo $single_post->title ?></h2>
        <p>Publicado por: <span><?php echo $single_post->author ?></span> em <?php echo $single_post->date ?></p>
    </div>
    <div class="glory-header-overlay"></div>
</header>

<div class="glory-page-body">
    <?php echo $single_post->content ?>
</div>

<?php get_footer() ?>

The problem is that since I'm using get_the_content(); in controller.php instead of the_content(); WordPress does not do auto embeds which is a problem for me since I go work with shortcodes, galleries etc.

However, if I use the_content(); the content of the post will appear soon after the opening of the <body> tag and, consequently, above all page elements (header, nav etc).

I've read about the possibility of circumventing this, however, I could not find a functional solution to the problem.

    
asked by anonymous 11.10.2016 / 23:52

1 answer

1

In your controller you can apply the filter the_content as soon as you search for the contents, for example:

<?php

require_once __DIR__ . '/../security.php';

class Controller_Single {
    public function __construct() {
        add_filter( 'get_single_content', array( $this, 'get_single_content' ) );
    }

    public function get_single_content() {
        $single_content = new StdClass();
        $single_content->title = get_the_title(); // Título do Post
        $single_content->author = get_the_author(); // Autor do Post
        $single_content->date = get_the_date(); // Data de publicação
        $single_content->content = apply_filters( 'the_content', get_the_content() ); // Conteúdo do Post
        return $single_content;
    }
}

new Controller_Single();
    
19.10.2016 / 14:40