• Resolved codex22

    (@codex22)


    Hello dear wordpressoholics,
    I am stuck with a problem. I want to add the following code to my functions.php:

    add_action('wp_enqueue_scripts', 'mh_superfish');
    function mh_superfish() {
    	if(!is_admin()) wp_enqueue_script('superfish', template_url( 'js/superfish.js', __FILE__ ), array('jquery'), '1.4.8', TRUE);
    	if(!is_admin()) wp_enqueue_script('superfish-args', template_url( 'js/superfish_args.js', __FILE__ ), array('jquery'), '1.4.8', TRUE);
    }

    But every time I try something, I always got a fatal error message. Please help! Thank you!

    BR
    Codex22

Viewing 9 replies - 16 through 24 (of 24 total)
  • Thread Starter codex22

    (@codex22)

    I did, and it will work, if I can change this code:
    plugins_url( 'js/superfish.js', __FILE__ ), array('jquery'), '1.4.8', TRUE

    so the script will look in the theme directory.

    That line by itself doesn’t mean anything, It’s incomplete.

    Thread Starter codex22

    (@codex22)

    add_action('wp_enqueue_scripts', 'mh_superfish');
        function mh_superfish() {
    	if(!is_admin()) wp_enqueue_script('superfish', plugins_url( 'js/superfish.js', __FILE__ ), array('jquery'), '1.4.8', TRUE);
    	if(!is_admin()) wp_enqueue_script('superfish-args', plugins_url( 'js/superfish_args.js', __FILE__ ), array('jquery'), '1.4.8', TRUE);
        }

    That’s the complete code.

    [email protected]

    (@patrickdfirebranddesignsnet)

    @codex22 That function is currently looking for those scripts in the plugins directory. It sounds to me like you want to move those scripts to your theme directory somewhere (maybe in a folder called js or something) and use get_stylesheet_directory_uri() in the function above instead of plugins_url(). This way it will look for those scripts in your theme’s newly created js directory. Does that make since?

    @esmi Am I on the right track with this?

    Thread Starter codex22

    (@codex22)

    Hi,
    this will only go to the themefolder, but won’t go into the js folder.

    Because your function mh_superfish already exists, it cannot redeclare the function mh_superfish, you should change it’s name , for example

    add_action('wp_enqueue_scripts', 'my_mh_superfish');
    function my_mh_superfish() {
    	if(!is_admin()) wp_enqueue_script('superfish', template_url( 'js/superfish.js', __FILE__ ), array('jquery'), '1.4.8', TRUE);
    	if(!is_admin()) wp_enqueue_script('superfish-args', template_url( 'js/superfish_args.js', __FILE__ ), array('jquery'), '1.4.8', TRUE);
    }
    Thread Starter codex22

    (@codex22)

    If I use this code, I get an error message:
    Fatal error: Call to undefined function template_url() in C:\xampp\htdocs\wordpress\wp-content\themes\my_theme\functions.php on line 16

    Try this

    add_action('wp_enqueue_scripts', 'my_mh_superfish');
    function my_mh_superfish() {
    	if(!is_admin()) wp_enqueue_script('superfish', get_template_directory_uri(). '/js/superfish.js', array('jquery'), '1.4.8', TRUE);
    	if(!is_admin()) wp_enqueue_script('superfish-args', get_template_directory_uri().'/js/superfish_args.js', array('jquery'), '1.4.8', TRUE);
    }
    [email protected]

    (@patrickdfirebranddesignsnet)

    this will only go to the themefolder, but won’t go into the js folder.

    Correct, but with the rest of the string you get to the js folder. πŸ™‚

    @hoosoft has the right idea.

Viewing 9 replies - 16 through 24 (of 24 total)

The topic ‘How to add a function to functions.php?’ is closed to new replies.