0

function custom_post_type(){//example} function custom_taxonomy(){//example}

Should add code filter hook or action hook custom page, custom post type, custom taxonomy, etc.. in functions.php or create plugins.What is the better method? Is there the way better than this ways?

1

Both methods are correct and won't have effect in performance or functionality of the code you put into the the plugin or functions.php.

Any theme related code, like hooks and filters manipulating output of that theme only should be kept in functions.php and other important custom code better be in a custom plugin.

Maintaining a custom plugin to hold all yourcustom code is rather a better idea than stuffing it into your functions.php file. Benefits:

  1. If you're using a commercial or any third party theme and it gets updated, your code in functions.php will be lost, this is in case you're not using a Child Theme.
  2. In case of theme change, you don't need to get into hassle of copy pasting the custom code to your new theme's functions.php file.
  3. Code will be organised and code maintenance will become easy.
  4. Code like registering CPTs and custom taxonomies is intended to stay despite of theme change, so it better be in a plugin so it stays functional even after theme change.

Other than these, every developer has their own reasons for keeping code in functions.php or custom plugin.

| improve this answer | |
  • Create folder inc or lib to put theme related code.Is it good or bad option? – Rei Jul 3 '17 at 14:07
  • Yes, it's a good practice to create an inc folder in theme directory and include it in functions.php – Junaid Jul 3 '17 at 14:09
  • Welcome. If you find this answer helpful and agree with my explanation, then mark it as the right answer. – Junaid Jul 3 '17 at 14:21
  • Maintaining a custom plugin to hold all custom code when using a commercial or any third party theme And created Child themes putting theme related code in functions.php and other important custom code better be in a custom plugin. – Rei Jul 3 '17 at 14:30
0

It depends on the situation, I tend to create them in the functions.php file if the theme has templates that need the custom post type installed. But if my theme works fine without the custom post type and it's something I may need in the future for other themes I'd just make a plugin for it.

| improve this answer | |

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