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.

user8514
  • 19
  • 1
  • 4

2 Answers2

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.

Andrew Bartel
  • 1,845
  • 2
  • 16
  • 21
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.

Mike
  • 96
  • 3