-1

EDIT 2: How would I iFrame wp_nav_menu contents while maintaining the styles and script associated with their display? (Please ignore all of the below seemingly confusing context of why someone would want to do this.)

EDIT 1: This question is related to WordPress's wp_nav_menu.

My client is very interested in sculpting their page rank by iframing internal links such as the entire header and footer.

I've tried to accomplish this by creating a php file in the root of the site and in it, changing WP_USE_THEMES to false, requiring the wp-blog-header.php file, then calling that wp_nav_menu. This SORTA works, but I'm left with a 404 and the links aren't styled.

Is there some elegant way to accomplish this? Just the idea of it feels very hackish and I've been adamant in sharing my thoughts. But maybe there's some SEO guru out there who's done this... ?

7
  • I don't have enough expertise in the area, but you may want to post this question at webmasters.se or stackoverflow if you don't find what you're looking for here.
    – mor7ifer
    May 18, 2012 at 20:26
  • OK. Are either of those the correct forums in which to ask WordPress questions? May 18, 2012 at 20:34
  • It depends on the nature of the question. As I said, I'm not familiar enough with the area of iframes, but if you can ask the same question and not have wordpress knowledge be necessary then they are certainly welcome there.
    – mor7ifer
    May 18, 2012 at 20:36
  • 3
    "My client is very interested in sculpting their page rank by iframing internal links such as the entire header and footer." Can you explain how this is WordPress-related? I don't even really know what it means. May 18, 2012 at 20:50
  • I am not sure if an iframe ever helps on seo. Perhaps you could edit your question further more and be more clear what you are trying to do.
    – Sisir
    May 18, 2012 at 21:21

1 Answer 1

3

To get a working nav menu WordPress needs to be set up complete. My suggestion is to use add_feed(). Ignore the name, you get text/html as output.

Let's start with code

<?php # -*- coding: utf-8 -*-
/*
Plugin Name: T5 Iframe Nav Menu
Description: Display a nav menu in an iframe.
Version:     2012.05.18

Refresh the permalinks after activation!
*/
add_action( 'init', 'wpse_52623_register_nav_menu_iframe' );

function wpse_52623_register_nav_menu_iframe()
{
    add_feed( 'navmenuiframe', 'wpse_52623_callback' );
}

function wpse_52623_callback()
{
    // if you don't use a separate stylesheet change this.
    $stylesheet = get_stylesheet_directory_uri() . '/menu.css';
    ?>
<!doctype html>
<base target='_top'>
<title>menu</title>
<link rel='stylesheet' href='<?php echo $stylesheet; ?>'>
    <?php
    wp_nav_menu(
        array ( // Adjust the location value
            'theme_location' => 'top-menu',
        )
    );
}
  • On 'init' you register the pseudo-feed. The first argument is the URI, the second argument the callback function to print the output.
  • After plugin activation (or after you have added the functions to your theme's functions.php) visit the permalink settings page to let WordPress refresh the rewrite rules. The menu has a real URL now; visit /navmenuiframe/ to see it.
  • Now you can use an iframe with /navmenuiframe/ as the value of the src attribute.
  • Make sure the 'theme_location' and the stylesheet URI are correct!
  • Repeat the procedure for the footer.

Other notes

  • Be aware that your pages will load slower now. Caching may help, but don't be surprised if Google takes performance into account. Even if there are real positive effects because of the iframe (I have strong doubts) they might not be visible.
  • The current page will not get special classes now, since WordPress doesn't know the page that is embedding the special menu page. You could pass a GET parameter to the iframe URL and filter the nav menu output accordingly in your callback function to fix that.
  • If a visitor has iframes disabled (I do that sometimes) she will get no navigation.
1
  • 1
    Nice! This is great. Thank you for such a thorough answer and your ability to see through all of the confusion in my question. May 19, 2012 at 0:07

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.