Archived topic
Add PHP and JS code in elements
5 replies · Started by Alejandro on August 23, 2019
Hi !
I need to add PHP and JS code in my site, but i was reading when the theme is upgraded, the code added into functions.php archive will delete. So, If I add the codes in Elements hooks, They will work good when the theme upgrade, or the codes will be deleted too?
I dont really create a child theme and i need to know if i can use in a safe way elements hooks !
Thanks.
Hi there,
JS Code: no problem adding them to a Hook.
PHP Code: This will depend on what that code does. For example if it is calling a template part then all should be fine. If its a Filter Hook function then no, this needs to go in child theme functions file or you can use the Code snippets plugin
Great ! So, i have two options:
1 - If I add JS or for example google analytics or tag manager codes in the hooks on elements, those codes will not be deleted with de upgrade theme, and can i add a snippets plugin with out a child theme?
2 - Can i create the child theme, add the PHP code with out plugin and copy the JS in the element's hooks but working with the child theme?
Which of them are better?
I dont know if if you understood me ! Thanks for the support.
Both of those options will work. Whether you use a child theme or not is really your preference. A child theme becomes necessary if you need to overwrite core template files.
If you already have a child theme, using the functions.php file for functions is the way to go. Elements is a good place to add your analytics/Google manager code regardless of a child theme or not.
Thank you so much for the support ! All of you really helped me.
No problem! Glad we could help :)