PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

putting action/filter hooks in a different file other than functions.php

  1. run23
    Member

    This is a PHP question rather than WP or GF...

    Does anyone know how to put custom action/filter hooks in a different file other than functions.php?

    I tried to put a hook function in different file (my_hooks.php), then simply include that file in functions.php, but I'm getting an undefined function error. My whole purpose in doing this is to reduce the amount of custom code I put in functions.php.

    Ideally, I would like to only register the hooks in functions.php, but all the useful code live elsewhere.

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  2. David Peralty

    You should be able to use

    require_once('includes/function-details.php');

    to pull in functions into your functions.php file without any issue. As long as your add action code was in the functions.php file and not the include.

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  3. elizabethanne
    Member

    i made a site-specific plugin for my sites - works like a charm and is way easier than putting things in functions.php - here's an article about it from Otto: http://ottopress.com/2011/creating-a-site-specific-snippets-plugin/

    one thing to note - sometimes code put in there doesn't fire at the right times to work, so it still needs to go in functions.php. i'm not advanced enough to know WHY, but i try my snippet in my site plugin first and then if the code isn't "working" put it in functions. php.

    hope this helps!

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  4. Thanks for the tip. That's a good way to handle it.

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink