0

I was asked by a customer about the possibility to put a menu from a WP installation into another WP, whereas are independent installations, each one for its domain. The point is: I developed a website in WP (using Woocommerce) and months after we developed a blog, with another layout structure but keeping the visual ID for the brand. Now, she wants to put the main menu from her main website into the blog, and all the options on that menu needs to be dynamic, so, if she decides to add another category to that menu, it should be shown on the blog too, automatically.

Is it possible to do something like that, such as integrate a WP installation with another? Basically I need to put a menu from a WP installation into another, each one on a separated domain. Can someone help me with this big problem??

Thanks at all !

3 Answers 3

0

This is actually quite possible, however, there are some specific things that you need to do to get it to work.

First, create a file on your wordpress installation that currently has that menu, for the demonstration, we're calling it myoutsidermenu.php, in a theme called theme_name. Once you are done that, edit the php code so that it echoes your menu item into that file using the wordpress engine. I suppose you could include the CSS styling for the menu itself, but that isn't entirely necessary. Remember, this file should only contain the menu, and no other wordpress elements, or they'll be included on the receiving end which might cause you problems.

Once you are done that, on the receiving end, where you want your menu to show up, put the following code:

<?php 
$doc = new DOMDocument();
$doc->loadHTMLFile('http://example.com/wp-content/themes/theme_name/myoutsidermenu.php');
echo $doc->saveHTML();
?>

Where the URL corresponds to the URL of the file that echoes the menu item. Now, remember to make sure to include CSS styling somewhere on your receiving end to make it display how you want it to display.

I'm not sure about any best-practices here, but it should get the job done.

7
  • And where would these menu items point? To the old domain.
    – vancoder
    Jun 27, 2014 at 17:43
  • 1
    Sounds great, I will test it. Just a question: is the DOMDocument()" function native from PHP? Jun 27, 2014 at 17:44
  • Jao, I believe it comes standard with php 5 and up. @vancoder Isn't that the point? They want their customers to easily get to their webstore, not to re-create the webstore on their new domain.
    – Adam
    Jun 27, 2014 at 17:47
  • "jao" is horrible in Brazil, it's "joao"! Jun 27, 2014 at 19:52
  • @joaosvu my bad, typo... did not realise - sorry! No wonder your name didn't pop up to tag you, haha
    – Adam
    Jun 27, 2014 at 21:28
0
<?php 
  error_reporting(0);
  $doc = new DOMDocument();
   $doc->loadHTMLFile('http://example.com/wp-content/themes/theme_name/myoutsidermenu.php');
  echo $doc->saveHTML();
 ?>

note, I added the removal of error reporting as this above method output warnings when pulling data across domains.

0

Instead of passing static files around as the other answers suggest, you can use custom feeds to enable communication between sites. On a multisite instance I worked on, the site providing the menu had a class that did this:

    public function add_custom_menu_feed() {
        add_feed( 'feed_path_here', [ $this, 'menu_feed' ] );
    }

    public function menu_feed() {
        header( 'Content-Type: application/json' );
        echo json_encode( $this->menu );
    }

The add_custom_menu_feed function needs to be added as an action on the init hook.

Those functions provide a JSON object with menu links; that $this->menu variable is built by the function below on the source site. But you could easily have it echo an entire HTML menu if you change the Content-Type header and echo markup instead of JSON.

Then, all sites to use the menu do this:


    public function get_menu() {

        if ( ! $this->menu ) {
            // If we're on the site that actually has the menu,
            // grab it directly. How the construct_local_menu method
            // works isn't important to the feeds concept; it just
            // gets what we need from the menu with the slug main-menu.
            $this->menu = this->construct_local_menu( 'main-menu');
        }

        if ( ! $this->menu ) {
            // If the menu is still empty, we're not on the site  
            // that hosts it, so we should grab our custom feed.
            $this->menu = json_decode(
                wp_remote_retrieve_body(
                    wp_remote_get('https://site-providing-the-menu.example.com/feed_path_here')
                ),
                true
            );
        }

        return $this->menu;
    }

The if( ! $this->menu) just ensures that we only retrieve this once per page load, no matter how many times we use that menu. That logic may not be necessary if you're fetching rendered HTML and immediately printing it. Also of course the json_decode is not necessary if you're not retrieving JSON data. But the wp_remote_retrieve_body(wp_remote_get()) stuff is where we fetch the feed so we can then do what we want with it.

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.