WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Reading header.php/footer.php to a text string (2 posts)

  1. UnlockTheMind
    Member
    Posted 1 year ago #

    I am attempting to read the results of the executed header.php/footer.php files as a string of html. Here's the scenario:

    There are pages in the site that are developed in a .net environment but they want to share common headers/footers across the entire domain. They wish to have WordPress be the repository for this code and any time there is an update have a PHP cURL call to a .net web service and feed it the new HTML for the header/footers.

    I tried calling get_header() but that does not return a string (as I anticipated) so then I tried this test solution in functions.php:

    function write_header() {
    	$header_content = file_get_contents(get_bloginfo('wpurl').'/index.php' );
    	$fp = fopen('c:\header.txt', 'a+');
    	fwrite($fp, $header_content);//just testing the output, this will be a cURL call eventually.
    	fclose($fp);
    }
    add_action( 'wp_update_nav_menu', 'write_header' );

    It seems to be a very heavy handed method of getting the HTML since I'll have to do a lot of string manipulation to parse out the pieces I want. Is there a simpler way of doing this that I'm missing?

  2. UnlockTheMind
    Member
    Posted 1 year ago #

    In case anyone comes across this thread looking to do the same thing. I worked through an answer on stackoverflow
    Basically create a page that contains just the header/footer and then read it using file_get_contents(get_bloginfo('wpurl').'/update/' ) where /update/ is the page with just the header/footer. Alternately you could use wp_remote_get()

Topic Closed

This topic has been closed to new replies.

About this Topic