How to apply the_content filter in get_the_content Wordpress function?

Asked

Viewed 888 times

3

I am developing a theme for Wordpress, for hobby and to study a little PHP, Javascript etc. and I am facing a small problem.

I’m working with the controller concept where all logic will be processed and then sent to a view. To make it clearer how my theme is structured I will pass my directory structure:

inserir a descrição da imagem aqui

The controller-single.php will control the data that will be rendered in single.php in it I define the class Controller_Single that will manage the content of the page/posts and then I call the necessary parts for and display the content:

<?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 = get_the_content(); // Conteúdo do Post
        return $single_content;
    }
}

new Controller_Single();

And in my single.php I perform the call of content:

<?php
get_header();
the_post();
$single_post = apply_filters( 'get_single_content', false );
?>

<header class="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, how I’m using get_the_content(); in the controller.php instead of the_content(); Wordpress does not perform the auto embeds what is a problem for me since I will work with shortcodes, galleries etc.

However, if I use the the_content(); the content of the post appears right after opening the tag <body> and therefore above all page elements (header, Nav etc.).

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

1 answer

1


In your controller you can apply the filter the_content as soon as it searches 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.