最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Where to add my PHP codes for AJAX Jquery to work?

programmeradmin0浏览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:

  • /

  • /

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/

Share Improve this question asked Jan 22, 2021 at 4:09 302Beep302Beep 12 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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/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/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!

发布评论

评论列表(0)

  1. 暂无评论