• Nick

    (@nickmenster)


    Hello,

    I recently dove straight in to WordPress (was creating with Drupal) and had to convert an HTML/CSS website, to a WordPress theme.. Well, I managed to break the site up into three sections (Index, Footer, Template page) so all would work. However, none of the JS that I try to add is working. I’ve managed to get it where I can use a plugin for the menu, but need JS to make it link to other pages!

    I can’t publicly post the URL, as it’s for work, however, if we can private message or anything of that sort on the forum, or email, I’ll happily share the URL, just can’t publicly.

    I’m not sure what to put in the functions.php or what-not to set up javascript correctly, so that may be my issue. Kind of a rough way to jump into WordPress, but the more I learn the better!

    Thanks!

Viewing 15 replies - 1 through 15 (of 20 total)
  • add_action( 'wp_enqueue_scripts', 'enqueue_my_script' );
    
    function enqueue_my_script() {
    
        wp_register_script( 'my-script', '//yoursite.com/js/code.js' );
        wp_enqueue_script('my-script');
    
    }

    Use wp_register_script & wp_enqueue_script to enqueue the scripts the right way.

    Thread Starter Nick

    (@nickmenster)

    Thank you very much for the reply Samuel!

    With the wp_register_script & wp_enqueue_script, would I do it like the following?

    add_action( 'wp_enqueue_scripts', 'enqueue_my_script' );
    
    function enqueue_my_script() {
    
        wp_register_script( 'my-script', 'URL/wp-content/plugins/responsive-select-menu/sparkoptions/sparkoptions.js' );
        wp_enqueue_script('my-script');

    I did that in the functions.php, I’m not quite familiar with WordPress (not creating themes from scratch anyways. The JS isn’t working after doing that, so I suppose something isn’t set up correctly.

    Again, thank you very much! Trying to do this the best I can, just not familiar with WordPress yet!

    You’re welcome.

    By the way, you’re missing an } in the above code. if the script is Jquery don’t forget to enqueue as well //ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js if not enqueued in the page already by other plugins, better do it.

    check out this answer: http://wordpress.stackexchange.com/q/185579/#answer-185585

    Don’t forget to let us know what isn’t working in case it didn’t work..

    Thread Starter Nick

    (@nickmenster)

    Thank you again for the help!

    When I added the code for the jquery, the entire page went white. I went to the page you linked to, and tried that, and the page was still white..

    With the code I showed above (I forgot to copy the }) it still doesn’t make the plugin work, it’ll show up, but the javascript won’t work.

    Am I missing something in particular?

    Thanks again!

    Hi Nick!

    Actually I have no idea what is not working for you, maybe you should provide a link to your site, or look through the source code to see if scripts are enqueued, etc..

    Explaining more will help others understand your issue.

    Thank you!

    Thread Starter Nick

    (@nickmenster)

    Hi Samuel!

    As I explained in the thread, I can’t publicly post the URL, as it’s for work. If there is a way I can send it to you directly, I can do that.

    The menu (Responsive-select-menu) will show up, but none of the links are clickable. They are supposed to be targeted by JavaScript, which will take you to the URLs.

    This is the code I put into functions.php on the theme I created.

    <?php
    add_action( 'wp_enqueue_scripts', 'responsive-select-menu' );
    
    function enqueue_my_script() {
    
        wp_register_script( 'responsive-select-menu', '//fineframegallery.com/wp-content/plugins/responsive-select-menu/sparkoptions/sparkoptions.js' );
        wp_enqueue_script('my-script');
    
    }
    ?>

    I have tried changing my-script to the plugin name, however, it just makes the page white, with no source code whats-so-ever.

    Thread Starter Nick

    (@nickmenster)

    Just tried the following things as well, but with no working luck.

    <?php
    wp_register_script( $handle, $src, $deps, $ver, $in_footer );
    wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
    ?>
    
    <?php
    
    function wptuts_scripts_with_the_lot()
    {
        // Register the script like this for a plugin:
        wp_register_script( 'responsive-select-menu', plugins_url( '/sparkoptions/sparkoptions.js', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
        // or
        // Register the script like this for a theme:
        wp_register_script( 'responsive-select-menu', get_template_directory_uri() . '/sparkoptions/sparkoptions.js', array( 'jquery', 'jquery-ui-core' ), '20120208', true );
    
        // For either a plugin or a theme, you can then enqueue the script:
        wp_enqueue_script( responsive-select-menu );
    }
    add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_the_lot' );
    
    ?>

    Not quite sure what to do now, any help would be amazing. πŸ™‚

    Hi Nick.

    sorry for the delay. I tried to access your JS direct link ( fineframegallery.com/wp-content/plugins/responsive-select-menu/sparkoptions/sparkoptions.js ) but it returned a 404 web page. Are you trying to enqueue the right ones ?

    Anyways, why are you trying to enqueue the scripts for such a popular plugin ? I know if you activate the plugin it should do everything..

    This code should work, if you put it in the plugin file :

    add_action('admin_enqueue_scripts', 'enqueue_my_script');
    
    function enqueue_my_script() {
    		wp_register_script('responsive-select-menu', plugin_dir_url( __FILE__ ) . '/sparkoptions/sparkoptions.js', array('jquery'));
    		wp_enqueue_script('responsive-select-menu');
    }

    or give a direct link ( fineframegallery.com/wp-content/plugins/responsive-select-menu/sparkoptions/sparkoptions.js ) if you put the code in functions.php file..

    Is this your website ? fineframegallery.com

    Regards.

    Thread Starter Nick

    (@nickmenster)

    Hi Samuel,

    That’s not the right URL, (not my website) but I have a similar code in the functions.php. When I go to the URL I have enque’d, I see the code.

    Like I said, none of the Javascript works on the site, so just activating the plugin doesn’t work, it’ll show up, yet not go to any page.

    Where in the plugin file do I put that exactly? In the actual JS for that file?

    Sorry Nick I used admin_enqueue_scripts which enqueues scripts in the admin page, tough we should be more specific to enqueue them in the plugin page only.

    You don’t need to edit the plugin to add that, because adding this in your functions.php will do the job ONLY if the slug of the plugin is responsive-select-menu and the plugin is activated:

    add_action( 'wp_enqueue_scripts', 'enqueue_my_script' );
    
    function enqueue_my_script() {
    
        wp_register_script( 'my-script', esc_url( home_url( '/' ) ) .'wp-content/plugins/responsive-select-menu/sparkoptions/sparkoptions.js', array('jquery') );
        wp_enqueue_script('my-script');
    
    }

    You can access the plugin JS file directly here http://yoursite.com/wp-content/plugins/responsive-select-menu/sparkoptions/sparkoptions.js

    Please post your support questions in the official plugin support forum here on wp.org forums: http://wordpress.org/support/plugin/responsive-select-menu

    Thread Starter Nick

    (@nickmenster)

    Samuel,

    I would post in the official plugin support, however, this isn’t the only plugin that won’t work right.. It’s like my theme isn’t set up for Javascript correctly.

    No plugin that requires Javascript or anything of that sort will work.. I had to convert the theme from HTML/CSS to work with WordPress, so I’m sure I didn’t set up some necessary component.

    Thank you again for the assistance.

    Well, if that’s the case, you need to worry more about the theme you are developing. here’s your guide:

    Theme Development – Codex

    Because in WP all you need to do is install and activate the plugin, and it is going to do everything automatically. seems like your theme isn’t set up correctly yet.

    if you switch to another default theme, you’ll notice that the plugin is going to work.

    You have to work more on the theme you are converting. because WP themeing is something more than CSS/HTML..

    πŸ™‚

    Thread Starter Nick

    (@nickmenster)

    Samuel,

    By chance, could you assist me with setting up some “hidden” things? I looked at the page linked, but only thing in reference to JavaScript was to be in CDATA to help with older browsers.

    I’ve been stuck with this website for over a week.. The menu is quite important.

    Could you email me so we can discuss more in-depth with URL’s? [Moderated]

    Thanks! πŸ™‚

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    It’s really nice that Samuel has taken the time to help you out, what a great guy πŸ™‚ Though he’s really doing it out of the kindness of his heart – and that can run out if it’s pushed too far by asking for more than he signed up. Let’s continue this on the forums.

    Thread Starter Nick

    (@nickmenster)

    Alright, sorry about that Andrew.

    I just can’t release the website URL publicly on the forum, and that’s something that will be needed to help set up something like this. :L

    What in particular relates from a page to the javascript function of WordPress? Is there a certain page that is supposed to contain a code, or something of that nature?

Viewing 15 replies - 1 through 15 (of 20 total)

The topic ‘Custom Theme Javascript Not Working (Responsive-Select-Menu)’ is closed to new replies.