PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] WordPress: Loading another page/post inside the "the_content" filter creates an 500 Internal Server Error

 February 17, 2022     elementor, filter, php, wordpress     No comments   

Issue

I would like to modify a posts content with the "the_content" filter. Inside this filter I would like to load another posts content.

This is my code:

add_filter( 'the_content', 'my_content_filter' );

function my_content_filter( $content ){

    if ( !is_singular( 'my-custom-post-type' ) ){
        return $content;
    }

    ob_start();

    echo '<p>SOME HTML BEFORE THE CONTENT</p>';

    echo $content;

    echo '<p>SOME HTML AFTER THE CONTENT</p>';

    $module = get_post( 12345 ); // load specific post
    echo apply_filters( 'the_content', $module->post_content );

    echo '<p>SOME MORE HTML</p>';

    $html = ob_get_contents();
    ob_end_clean();

    return $html;

}

Sadly this creates a 500 Internal Server Error. I guess because I created an endless loop. Do you have any idea how to get the formatted content of another post inside the "the_content" filter?

Thanks :-) Jan

EDIT

A bit more details: I created a custom post type called "sidebars" where I edit the content with a page builder. I would like to add this sidebars with PHP.


Solution

SOLUTION

With the "Elementor" page builder you can do this:

add_filter( 'the_content', 'my_content_filter' );

function my_content_filter( $content ){

    if ( !is_singular( 'my-custom-post-type' ) ){
        return $content;
    }

    ob_start();

    echo '<p>SOME HTML BEFORE THE CONTENT</p>';

    echo $content;

    echo '<p>SOME HTML AFTER THE CONTENT</p>';

    $elementor = \Elementor\Plugin::instance();
    echo $elementor->frontend->get_builder_content( 12345 );

    echo '<p>SOME MORE HTML</p>';

    $html = ob_get_contents();
    ob_end_clean();

    return $html;

}


Answered By - Jan K
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing