0

I've encountered an issue that I can't seem to figure out.

I am using require_once to place content before and after the page content on my homepage. The before content works perfectly and displays fine. If I use text for the after content such as $aftercontent: 'this is my text';

However, if I attempt another require_once call the after content is situated directly beneath my before content, and before The $content. Code below.

// -----------------------------------------
// HOMEPAGE HERO & FOOTER CALL

function wpdev_before_after($content) {
    if ( is_user_logged_in() && is_front_page() ) {
        $beforecontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/hero-registered-include.php' );
        $aftercontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/footer-registered-include.php' );
        $fullcontent = $beforecontent . $content . $aftercontent;
    } else {
        $beforecontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/hero-unregistered-include.php' );
        $aftercontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/footer-unregistered-include.php' );
        $fullcontent = $beforecontent . $content . $aftercontent;
    }

    return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');

1 Answer 1

0

If the file you want to attach (eg. footer-registered-include.php) print/display some text, this text will be displayed in the time you include the file.

To assign to the variable the content displayed by the included file you should:

  1. turn on output buffering
  2. include file
  3. contents of the output buffer and end output buffering
function wpdev_before_after($content)
{
    if ( is_user_logged_in() && is_front_page() )
    {
        ob_start();
        require_once( get_stylesheet_directory() . '/includes/hero-registered-include.php' );
        $beforecontent = ob_get_contents();
        ob_clean();
        // -- OR --
        // $beforecontent = ob_get_clean();
        // ob_start();

        include_once( get_stylesheet_directory() . '/includes/footer-registered-include.php' );
        $aftercontent = ob_get_clean();

        $fullcontent = $beforecontent . $content . $aftercontent;
    }
    // else {
    // ...
    // }
    return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');

References:

6
  • This half worked. It placed the hero at the top, but then duplicated the hero and then placed the footer beneath that.
    – CongoDeuce
    Aug 1, 2019 at 7:09
  • This works! However, it is missing the Else statement that indicates to load 2 different includes if the user is not logged in. Can I just copy this code to the Else statement or? What is happening here exactly? What was stopping my code from working? Thanks!
    – CongoDeuce
    Aug 1, 2019 at 7:38
  • Do the same - start buffering on before include/require and read buffer contents after.
    – nmr
    Aug 1, 2019 at 8:23
  • Got it, thank you! Can you elaborate on why the buffering plays such a big role in making this work? For example, if I used text instead of an include no buffering is required. Just trying to figure out what is happening.
    – CongoDeuce
    Aug 1, 2019 at 22:41
  • The the_content filter accepts the content of the post as a parameter and returns it after an optional modification. It (post content) will be displayed later. Let's say you have echo 'text from hero file'; in hero-registered.php and echo 'something in footer file'; in footer-registed.php. The code: $before= require_one('hero-registered.php'); $after= require_one('hero-registered.php'); $content= $before.$content.$after; will load and execute the first script, the text will be displayed "text from hero file", to $before variable will be [...]
    – nmr
    Aug 2, 2019 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.