WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to add a function to functions.php? (26 posts)

  1. codex22
    Member
    Posted 1 year ago #

    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

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    What theme are you using?

  3. codex22
    Member
    Posted 1 year ago #

    I use my own theme. (http://ammantempel.net) and now I want to add this code to my functions.php

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Do you have any knowledge of PHP?

  5. codex22
    Member
    Posted 1 year ago #

    Just a little. I am not that good at php.

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    What is the exact error message?

  7. codex22
    Member
    Posted 1 year ago #

    This is the message I got.

    >Fatal error: Cannot redeclare mh_superfish() (previously declared in C:\xampp\htdocs\wordpress\wp-content\plugins\superfish\superfish.php:13) in C:\xampp\htdocs\wordpress\wp-content\themes\artist_theme\functions.php on line 18

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    Your new functions.php code is conflicting with your superfish plugin. You cannot use both at the same time.

  9. codex22
    Member
    Posted 1 year ago #

    Okai I deactivated the plugin, but now I get this message:

    Fatal error: Call to undefined function template_url() in C:\xampp\htdocs\wordpress\wp-content\themes\NTT-Cindy-Theme\functions.php on line 16

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    You're using a completely different theme now! Where did you download this theme from?

  11. codex22
    Member
    Posted 1 year ago #

    That was a theme from a tutorial (newthinktank.com). But even I swtich back to my theme, I get this message:

    Fatal error: Call to undefined function template_url() in C:\xampp\htdocs\wordpress\wp-content\themes\artist_theme\functions.php on line 16

  12. esmi
    Forum Moderator
    Posted 1 year ago #

    Try removing the template_url() function.

  13. codex22
    Member
    Posted 1 year ago #

    I tried, but it is a bit hard. How do i rewrite this code:

    plugins_url( 'js/superfish.js', __FILE__ ), array('jquery'), '1.4.8', TRUE)

    so it will look in the theme folder???

    [No bumping. If it's that urgent, consider hiring someone.]

  14. esmi
    Forum Moderator
    Posted 1 year ago #

    I thought you created this theme?

  15. codex22
    Member
    Posted 1 year ago #

    Yes I did, but this code is form a plug-in called "superfish menu". I wanted to integrate this plug-in into my theme. So I thought I could do this by add this code to the functions.php. I don't want the plug-in to be activated, I want it to be active straight from the beginning.

  16. esmi
    Forum Moderator
    Posted 1 year ago #

    Check the plugin's documentation.

  17. codex22
    Member
    Posted 1 year ago #

    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.

  18. esmi
    Forum Moderator
    Posted 1 year ago #

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

  19. codex22
    Member
    Posted 1 year ago #

    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.

  20. patrickd
    Member
    Posted 1 year ago #

    @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?

  21. codex22
    Member
    Posted 1 year ago #

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

  22. hoosoft
    Member
    Posted 1 year ago #

    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);
    }
  23. codex22
    Member
    Posted 1 year ago #

    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

  24. hoosoft
    Member
    Posted 1 year ago #

    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);
    }
  25. patrickd
    Member
    Posted 1 year ago #

    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.

  26. codex22
    Member
    Posted 1 year ago #

    @hoosoft thanks a lot!!!! This works. Superb! :-)

    And of course also thanks to patrickd@firebranddesigns.net and esmi for your tries to help me!

Topic Closed

This topic has been closed to new replies.

About this Topic