1

I have an external website that needs to load the header and footer from another site in Wordpress.

I've managed to have this done with the following code:

<?php
require_once("wp-blog-header.php"); /* setup the wordpress environment */     
require_once(dirname(__FILE__) . "/includes/head.php"); 
?>

This code outputs the correct header. Inside head.php I call wp_head(); and all works as excpected.

When I do the same thing for the footer, the wp_footer(); function doesn't output the scripts.

It only outputs the scripts if I call wp_head(); right before wp_footer();

This works but it also outputs all of the header inside my footer which is not something I want.

How can I execute wp_footer() without having to call wp_head() or how can I just output the scripts that are loaded via wp_enqueue_script() in my functions.php page to the footer?

Any ideas? Thank you in advance.

0

2 Answers 2

1

For what it's worth, I found a solution to my problem and posting it here for other lost souls :)

<?php
require_once("wp-blog-header.php"); /* setup the wordpress environment */ 

// This is to fool the system
// In order for wp_footer() to execute, we need to run wp_head() but since we don't want the output of the header again,
// we put it in a buffer, execute the function and then clear the buffer resulting in what we need
ob_start();
wp_head();
ob_clean();

require_once(dirname(__FILE__) . "/includes/footer.php"); 
?>
0

There are probably many ways in which this kind of code may fail, as wordpress will most likely assume it displays the home page and therefor add (or not) scripts and styling based on it.

There are probably many filters and actions that are triggered or their execution result is based on the page type being served. You might to set the $_SERVER variable properly to get the results you want.

OTOH it sounds like you are doing it the wrong way. If wordpress supply the visual framework, then the other site should be embedded into it, not the other way around. If for whatever reason you just can't you should consider just not being "clever" and embed directly the CSS and JS you need

3
  • Thank you for your answer. I completely agree but unfortunately, I can't integrate it directly in the Wordpress. The reason for this is not just for getting the scipts and CSS, but also to show the navigation. We basically have the wordpress site and an independant e-commerce website in a subdomain. This e-commerce website needs to have the same look and feel as the site. Problem is, if the client updates the menus, the e-commercer website is no longer valid. This is why we've opted for this solution.
    – VVV
    May 16, 2017 at 4:51
  • this is IMHO not a very smart thing from the technical POV. You are going to have all kinds of global variables floating around, global cookies, global CSS rules, global JS. The chances for something unexpected going wrong are very high. You can't just easily mix two different non trivial applications and expect it to "just work" May 16, 2017 at 6:00
  • I agree. But for now it's our only option (temporary). We will rebuild the e-commerce app in the near future. Thank you for your time. I appreciate it.
    – VVV
    May 16, 2017 at 9:52

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .