WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to dynamically load the wp_menu with enquire.js but it just doesn't work. If the screen is < 1080px the wp_menu(mobile) should be loaded, otherwise the wp_menu(desktop).

In my homepage-template.php I regularly call the header and then I use enquire.js as per instructions.

<script type="text/javascript">
enquire.register("screen and (max-width:1080px)", {

// If supplied, triggered when a media query matches.
match : function() {
    document.write('meno di 1080px<br/>');

// If supplied, triggered when the media query transitions
// *from a matched state to an unmatched state*.
unmatch : function() {
    document.write('OLTRE i 1080px<br/>');

// If supplied, triggered once, when the handler is registered.
setup : function() {},

// OPTIONAL, defaults to false
// If set to true, defers execution of the setup function
// until the first time the media query is matched
deferSetup : true,

// If supplied, triggered when handler is unregistered.
// Place cleanup code here
destroy : function() {}


Enquire works, but Wordpress doesn't and kicks out this error message:

Fatal error: Call to undefined function wp_nav_menu() in /kunden/372333_10437/webseiten/dev.andreapuiatti.com/dev4/wp-content/themes/spk/templates/headernav-computer.php on line 20

Any Idea on how to dynamically load that menu?

follows the content of headernav-computer.php:

<?php wp_nav_menu( array('link_after' => '<div class="one"></div>', 'menu' => 'primary', 'container_class' => 'menu') ); ?>
share|improve this question
Where exactly are you loading that script? The error comes from a PHP file while you showed us Javascript code... – kaiser Aug 26 '13 at 15:39
the js code has been placed directly into the homepage-template.php. Enquire.js has been regularly enqueued through wp_enqueue_script in functions.php – Andrea Puiatti Aug 26 '13 at 15:45
You can't load a template file directly, there is no WordPress environment in that context. have a look at the AJAX API. – Milo Aug 26 '13 at 15:49
Not sure if it makes any difference but 1) enquire.register registers a breakout resolution and execute a function when this breakout is reached; 2) the function call ajax that load html and put in the body – Andrea Puiatti Aug 26 '13 at 15:55
Like @milo said, wp_nav_menu() is undefined because it is being called outside the Wordpress environment. If you build this as a plugin you would have access to the Wordpress environment. – epilektric Aug 26 '13 at 16:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.