0

I am trying to display state's names responsively based on what country is selected from a list (via a library).

I reckon I needed to use WP AJAX and Jquery to achieve this goal. I am totally new to WP AJAX and Jquery. For the past two days, I have been reading blogs and forums to achieve the responsiveness. Here I came across two files to add my PHP code snippets which are functions.php AND plugin files.

Question: Should I add the PHP code snippets in themes/functions.php OR plugins/MYPLUGIN/myplugin.php or either one? Why?

Two instances of the blogs I've read:

2
  • 1
    The functions.php file in a theme is, in essence, a mini-plugin. If you'd like the functionality to be independent of your theme, you should create a plugin; if you want the functionality to be part of your theme, then put it in functions.php.
    – Pat J
    Commented Jan 22, 2021 at 4:28
  • Hi Pat. Thanks for your explanation. Cleared my doubts.
    – 302Beep
    Commented Jan 22, 2021 at 5:23

1 Answer 1

0

Here is a general scenario for adding PHP code snippets in WordPress theme or plugin.

  1. Scenario 1: Theme is your custom made theme.

    In this case, you can add code snippets in your own desired way (and obviously in standard way since you are the author of the theme).

  2. Scenario 2: Theme is either premium or downloaded from wordpress.org/themes

    In this case you may want to create a child theme and add the PHP code snippet in functions.php file or create new files and include them in functions.php file (point is it's up to you as long as you maintain the coding standards). This is because if you don't create a child theme and add PHP code snippet directly in parent theme's functions.php file, you will lose all the added code snippets once you update the theme. If you update any Theme or Plugin, all the changes made within their core file will be deleted (since they are replaced by updated plugin or theme files).

  3. Scenario 3: Plugin is your custom built.

    In this case, you can add code anyway you like.

  4. Scenario 4: Plugin is premium or downloaded from wordpress.org/plugins

    you can not add any code snippet to this plugin file. All you do is override the functionality if the plugin allows to do so by providing appropriate hooks (action or filter). And to use these hooks (action or filter), you will still need to add the code in child theme's functions.php file.

I hope this answer helps you to write your desired code snippet in proper way.

All the best!

2
  • I got to vote for this. Is there any good blog somewhere or question from WP Stack Exchange that can help me to achieve my goal?
    – 302Beep
    Commented Jan 22, 2021 at 6:34
  • you can submit questions piece by piece here on this stack exchange (along with your tried code as well) and I am sure a lot of developers around world will help you.
    – maverick
    Commented Jan 22, 2021 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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