0

I am looking for a solution to reuse the header and footer navigation links (with style, of course) in one of my WordPress website for several other WordPress sites.

Please note that I'm trying to share header and footer among WordPress sites, not from WordPress site to a PHP page. The sites I'm referring to are on the same server. I have the following directory structure:

example.com/ #main site is here
   some-other-site/
      wp-admin/
      wp-content/
      wp-include/
      ...
   wp-admin/
   wp-content/
   wp-include/
   ...

I would really appreciate some direction on how to achieve this goals and best practices, if possible since I am still new to WordPress. I have a few ideas in mind but I am not sure which one is best programming practice or how much effort each approach requires (for cost benefit analysis)

1) Write a custom get_header() function in the main site's functions.php to allow extraction of navigation links file_get_contents() to get the navigation links from wp-content/themes/my-theme/inc/footer.php in some-other-site/ I use

define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');

Currently, I get "<a href="&lt;?php bloginfo('siteurl'); ?&gt;/articles/category/editorial_team">Background</a>" as output so it doesn't work for me yet.

I found one similar topic but the question is a bit unclear to me and the solution of using absolute urls is not a good practice, I was told.

2) Expose those navigation links as web service. I have a feeling that web service is not even relevant here but I still put it here just in case.

3) Use Multisite settings or create a network for all my WordPress sites. While this appears to be the best way, it seems quite complicated and there are actually issues with my main site being setup in a network currently. I doubt it's necessary to got through this complication to achieve my goal.

As far as I know, sites in WordPress network shares certain databases and therefore I'm so afraid of losing some or whole of the huge data in my main site.

It would definitely be relevant to point out the best practices in sharing CSS stylesheets and Javascripts file among WordPress sits as well, if you are kind enough :)

Updates
I've decided to

  • Stick to wordpress multisite as much as possible
  • Abandoned the poor practice hierarchy mentioned above - nested WordPress directories
Community
  • 1
  • 1
ericn
  • 12,476
  • 16
  • 84
  • 127
  • From what I understand, Eric, you have a main site. We'll call it "domain.com". Within www.domain.com's main folders, you have a WP blog set up. So going to www.domain.com shows us your WordPress blog (custom theme, etc). Within your main site folders, you have additional folders, ie `domain.com/site2/` with the entire WordPress folder structure. *If that is correct... why?* –  Sep 19 '11 at 07:23
  • Your understanding is absolutely correct. This is an existing structure that I took over and I didn't have chance to figure out all reasoning behind. My interpretation is that we have different domains for different countries. For Singapore, we have the main blogs and forum and deals site etc. So the forum and deals are setup inside the folder for Singapore blog because they are not relevant for other countries :) – ericn Sep 19 '11 at 07:32
  • Also, there is nothing wrong with using absolute URLs as long as you know what you are doing. The blog post you linked said not to, because the author had directly copied one site and was surprised that the links/etc still went to the other one (after using absolute paths). AKA, he didn't know what he was doing. –  Sep 19 '11 at 07:33
  • I see. That's an awfully awkward situation, considering you can just use categories within WP and filter out what categories display... **Anyways:** You have a `header.php` and `footer.php` file that is structured how you want it, and you're going to re-use those two files on every single wordpress blog - root and child? –  Sep 19 '11 at 07:35
  • Yes, Vonkly. I would like to reuse part of header.php and footer.php e.g. some #navigation div (note that header.php and footer.php are not well-formed xhtml i.e. opening
    without closing) in every single wordpress blog, root and child :)
    – ericn Sep 19 '11 at 07:42

1 Answers1

1

If you'd like to include the entire header.php and footer.php files in the parent and all child wordpress blogs, the only way of doing that without uploading copies would be to use an absolute path, or create a nested relative path individually for each area that you'd like to include it in.

The way your site hierarchy is laid out is generally considered... poor practice. Perhaps I am misunderstanding the reasoning behind it, but I would suggest evaluating the current situation with the site owner and suggesting an alternative (with some major benefits - ux and structurally).

  • Woah, woah. In your last comment, you said `I would like to reuse part of header.php and footer.php e.g. some #navigation div`. You'd like to only include certain parts of each file? –  Sep 19 '11 at 07:55
  • Yes, only certain parts of them, Vonkly :) I have strong reasons not to use the whole header.php or footer.php as stated above :) – ericn Sep 19 '11 at 08:36
  • Um.. what you want is to look through a file and pull out only certain lines of code and text. This is not possible. –  Sep 19 '11 at 08:42
  • Using approach 1) above and changing the links to absolute ones, it actually does what I need. It looks like I have to go for WordPress Network as it is still a lot of hardcoding at the moment. Thanks a lot anw :) – ericn Sep 19 '11 at 09:34