What I need done:

The mobile menu on the site (let me know if you want to see the dev site, so I don't get marked as spam for posting the link) is loaded by a script that hides the main menu when the browser width falls below 1030px. Before then the mobile menu div doesn't exist. I have a shopping cart link on the desktop site that I'm trying to insert into that mobile menu div automatically whenever that div is generated by the script. Then I'll hide the one in the desktop code, etc.

I'm using the following to enqueue some jQuery, which in turn loads the custom "cart link" PHP plugin into a mobile menu div that is loaded by a separate js file only when the browser width is less than 1030px. Still with me? Here's the code:

In functions.php:

function mobile_cart_script() {

<script type="text/javascript">
  var pluginDir = <?php echo json_encode(bloginfo('url')); ?>;


wp_register_script( 'mobile_cart_script', '', false, false, false );
wp_enqueue_script( 'mobile_cart_script' );



This isn't working.

I had first tried including the script right in the header, but the script required the mobile menu script to load before it. So I put the script in that file right after the script that calls the mobile menu, taking care of that dependancy. Then I discovered that this needed me to require_once = wp-load.php in the php plugin, which isn't ideal.

After searching around, I came across the wp_enqueue_scripts method above. It doesn't need the plugin to require_once = wp-load.php(I don't think), and it eliminates putting the code in head and overbearing the server, but it's still not working.

Any ideas?

  • Honestly, I think you are in the wrong direction; it seems that responsive CSS could do the job easier in this case. Anyway, note that wherever you put the javascript, you are loading a PHP file directly bypassing WordPress totally, so it dosen't matter where you put the javascript, the PHP script will needs to manually load WordPress anyway. – cybmeta Mar 31 '15 at 7:52
  • @cybmeta, yea I hear you on needing to load WP regardless of where I load the separate PHP file. Unfortunately, this is an existing site/mobile menu I'm trying to "hack" into, so as much as I'd rather use responsive CSS, it's not an option. That's why I'm asking on here. Any ideas? – dmoz Mar 31 '15 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.