Integrating Superfish Menu Into a Template

Integrating Superfish Menu Into a Template

Tutorial Details
  • Program: WordPress
  • Version: 3+
  • Difficulty: Easy
  • Estimated Completion Time: 10 minutes

This is a short and easy step-by-step guide to using the jQuery menu plugin, Superfish, developed by Joel Birch.


Step 1 Downloading

Download the Superfish pack zip from the official website.


Step 2 File Structure

Copy the CSS and JS to the theme’s folder. For our tut, the target is the twentyeleven folder. The directory names are superfish-css and superfish-js.


Step 3 Import Commands

Import the CSS & JS files to header.php with the wp_enqueue_script and wp_enqueue_style WordPress functions.

<?php
add_action( 'wp_enqueue_scripts', 'superfish_libs' );
function superfish_libs()
{
	// Register each script, setting appropriate dependencies
	wp_register_script('hoverintent', get_template_directory_uri() . '/superfish-js/hoverIntent.js');
	wp_register_script('bgiframe',    get_template_directory_uri() . '/superfish-js/jquery.bgiframe.min.js');
	wp_register_script('superfish',   get_template_directory_uri() . '/superfish-js/superfish.js', array( 'jquery', 'hoverintent', 'bgiframe' ));
	wp_register_script('supersubs',   get_template_directory_uri() . '/superfish-js/supersubs.js', array( 'superfish' ));

	// Enqueue supersubs, we don't need to enqueue any others in this case, as the dependencies take care of it for us
	wp_enqueue_script('supersubs');

	// Register each style, setting appropriate dependencies
	wp_register_style('superfishbase',   get_template_directory_uri() . '/superfish-css/superfish.css');
	wp_register_style('superfishvert',   get_template_directory_uri() . '/superfish-css/superfish-vertical.css', array( 'superfishbase' ));
	wp_register_style('superfishnavbar', get_template_directory_uri() . '/superfish-css/superfish-navbar.css', array( 'superfishvert' ));

	// Enqueue superfishnavbar, we don't need to enqueue any others in this case either, as the dependencies take care of it
	wp_enqueue_style('superfishnavbar');
}
?>

Step 4 Class Setting

Search the line shown below and change as indicated. This way, we make WordPress to use the Superfish version for the main menu:

// Change from this:
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

// To this:
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'sf-menu', ) ); ?>

Step 5 Creating a Menu

Make and save a menu system in the WordPress admin, then it will show up in the appropriate part of the site. The picture below shows the site before the menu added:


Step 6 Menu Added

After the menu is added:


Step 7 Styling

Modify the Superfish styles to match the Twenty Eleven theme. The file is called superfish.css. Below are the details:

/* Change this: */
.sf-menu a, .sf-menu a:visited  { /* visited pseudo selector so IE6 applies text colour*/
	color:			#13a;
}
.sf-menu li {
	background:		#BDD2FF;
}

/* To this: */
.sf-menu a, .sf-menu a:visited  { /* visited pseudo selector so IE6 applies text colour*/
	color:			#999; /*#13a;*/
}
.sf-menu li {
	background:		#ccc;/*#BDD2FF;*/
}
/* Change this: */
.sf-menu li:hover, .sf-menu li.sfHover,
.sf-menu a:focus, .sf-menu a:hover, .sf-menu a:active {
	background:		#CFDEFF;
	outline:		0;
}

/* To this: */
.sf-menu li:hover, .sf-menu li.sfHover,
.sf-menu a:focus, .sf-menu a:hover, .sf-menu a:active {
	background:		#eee;
	outline:		0;
	color: 			#111;
}
/* Change this: */
.sf-shadow ul {
	background:	url('../images/shadow.png') no-repeat bottom right;
	padding: 0 8px 9px 0;
	-moz-border-radius-bottomleft: 17px;
	-moz-border-radius-topright: 17px;
	-webkit-border-top-right-radius: 17px;
	-webkit-border-bottom-left-radius: 17px;
}

/* To this: */
.sf-shadow ul {
	/*background:	url('../images/shadow.png') no-repeat bottom right;
	padding: 0 8px 9px 0;
	-moz-border-radius-bottomleft: 17px;
	-moz-border-radius-topright: 17px;
	-webkit-border-top-right-radius: 17px;
	-webkit-border-bottom-left-radius: 17px;*/
}

Step 8 Finished Version

Here is how it will look like when it’s finished:

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://about.me/mikerudolph Mike

    One big thing that is done wrong in this is the way scripts are en-queued in the header.php file, any scripts or styles should be brought into wordpress from the functions.php this makes use of the wp_enqueue_scripts hook which when using enequeue_script function properly will bring scripts into the footer as they should and styles into the header where wp_head is called in header.php

  • Pingback: Integrating Superfish Menu Into a Template | Shadowtek | Hosting and Design Solutions

  • http://www.seokamrul.com/ seoKamrul

    Wow! nice and easy.

  • http://www.paulund.co.uk Paul

    Nice tutorial be good to have a demo page to see this.

  • dj

    You don’t think a sentence or two (or twelve) in the post to explain what superfish is, how it is different than what is already available and why one would even want to bother with it might be in order for a tut about a not-universally-known plug-in would be in order? If we’re supposed to go to the plug-in’s home page to see about it then why bother with a tut?

    • http://www.giulianoliker.com Giuliano

      That would be little bit too much. If he would go that way than he might explain all functions used, Worpress and Notepad++ in details as well. Superfish is well known javascript menu based on Suckerfish pure css menu. Both Superfish and Suckerfish exist for years and are widely used scripts known to every developer. My guess is that author assumes you already know some basics since you are reading tutorial that involves toying with WordPress code behind.

  • http://mandiwise.com Mandi

    This is exactly the tutorial I was looking for about 3 months ago, but I’ll definitely cross-reference to see if the solution I ended up hacking out is similar to this one.

  • http://www.webtemplates-creare.com/ Paul Weston

    Thought this was a great little tutorial. I am still learning the ropes when it comes to WordPress so this tutorial is very useful to me not just for the menu but for giving me an heads up on how to work with WordPress and get around the platform. I thought there was some great detail in this post and this is something I will be trying out and expanding on through css on my next project.

  • http://intronex.pl/ Tablice interaktywne

    That would come in handy few weeks ago but I did it on my won already so it is not so complicated.

  • http://v-otpuske.com Artem

    Hi!
    After the third step, when I insert the code in header.php my site is completely disappears. Does anyone know what I did wrong?

    • Guest

      Same thing happened to me, it’s a very unclear and poorly made tutorial. Seems more like he just screenshotted his computer at random points while installing the scripts, and then uploaded them all here. Tutorials should teach and be a guide.

  • Ar-G

    Nice tut! What if I want to have 2-3 levels submenu?

    • Michael

      +1, how to enable the submenu’s?

      • Carlos

        how to enable the submenu’s?

  • jeff

    Great tutorial, but you forgot one important step: To initialize the superfish jQuery function itself.

    add_action( ‘get_footer’, function(){ ?>

    jQuery(document).ready(function($){
    $(‘ul.sf-menu’).superfish();
    });

    <?php });

    Without it, there’s really no point of using superfish. ;)

    • Pascal

      I get an “Parse error: syntax error, unexpected T_FUNCTION” if i add this to the “Import Commands” Snippet in header.php

  • Mattes

    Hey.. there’s a little mistake. the Header-snippet in line 6: there’s a ) at the end causing the overkill/error..

    • http://wp.envato.com/ Japh Thomson
      Staff

      Thanks for pointing that out, Mattes! I’ve fixed it up now :)

  • Pingback: Как подключить меню Superfish Menu в тему WordPress -

  • Guest

    Broke my website, not really any explanation of whats going on. I think I’ll be better off figuring this out on my own, instead of looking at random screenshots with no explanations.

  • Pingback: 佈景主題:Whatever Lab | Lab Crocodile

  • bswen

    Thanks, helpful tut!

  • Pingback: Discover 20 new Joomla Templates for 2012

  • http://www.facebook.com/shearamariz Mariz Quintanilla Papas

    wasted my entire time.. ugh! Please correct your codes…