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:
/
/
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:
https://artisansweb/how-to-use-jquery-ajax-wordpress/
https://premium.wpmudev/blog/using-ajax-with-wordpress/
1 Answer
Reset to default 0Here is a general scenario for adding PHP code snippets in WordPress theme or plugin.
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).
Scenario 2: Theme is either premium or downloaded from wordpress/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).
Scenario 3: Plugin is your custom built.
In this case, you can add code anyway you like.
Scenario 4: Plugin is premium or downloaded from wordpress/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!
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 infunctions.php
. – Pat J Commented Jan 22, 2021 at 4:28