0

I would like to move my large php functions out of the index or template files and into the function.php file. Would it be something like this?

function grab_code(???) {
    ??? .= '<div>some HTML-PHP code I monkeyed together</div>';
      return ????;
}
add_filter('???', 'grab_code'); 

Then in the template files i would add?

echo grab_code(???);

I found answers here that look close to what I am asking but the seem specific to a function.
Thanks.

5
  • What happened with you tried doing it?
    – anu
    Apr 10, 2013 at 16:47
  • This is a PHP question, not a WordPress question. Apr 10, 2013 at 16:49
  • @ChipBennett not really, as it concerns the use of functions.php directly.
    – vancoder
    Apr 10, 2013 at 16:51
  • 1
    Please refer to the FAQ: "Note that we do not handle questions...not specific to WordPress (even if they happen in its context)...generic PHP...issues and development" Apr 10, 2013 at 16:53
  • The use of, and load order or, functions.php, is specific to WordPress.
    – vancoder
    Apr 10, 2013 at 17:15

2 Answers 2

0

If you move the function and the add_filter function to the functions file, the you don't need to echo the function after. If the function doesn't involve using add_filter then you could echo out the function in your template file. Placing it in the functions.php file, will run on every page/post load unless specified otherwise.

1
  • Thank you for all your answers and debate. I was looking for correct hooks and method to move WordPress functions out of the theme and only call them when needed from the function file. I read all the links and looking into what was said. For my purposes it doesn't matter if it was a plugin or not. Maybe this was to broad a php question and Chip is correct. Thanks @MikeLewek I will look in to if add_filter and echo was what I am needing.
    – user8514
    Apr 11, 2013 at 17:34
0

Your functions.php file is loaded prior to your template files. So just migrate it into your functions.php and you'll be able to call it normally in your template files. Better yet, why not create a custom functions plugin as suggested by Justin Tadlock:

Creating a custom functions plugin for end users

Most of the WordPress tutorials I write mention adding custom functions to your theme’s functions.php file. Many of the other tutorials around the Web will use this same technique for adding custom code. However, there are different ways of handling custom functions.

4
  • I'm not I agree with the functions plugin avenue - it would really depend on the function involved. If it is intimately tied in to the specific theme, it should reside in functions.php.
    – vancoder
    Apr 10, 2013 at 18:07
  • @vancoder & Andrew, this is the reference Q&A here in WPSE: Where to put my code: plugin or functions.php?
    – brasofilo
    Apr 10, 2013 at 18:41
  • @brasofilo Good post.
    – vancoder
    Apr 10, 2013 at 18:44
  • @brasofilo & vancoder Not surprising the definitive answer came from Chip Bennett Apr 10, 2013 at 19:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.