Search code examples
phpwordpresswordpress-rest-api

WordPress /wp-inlcludes/functions.php: Custom functions/code disappears


I have added two custom written functions to my wordpress code. One of them uses the add_action() and the other uses the add_filter() function of wordpress.

After deploying the code, both functions work as expected, but after some time (I am not sure how long it takes and what causes wordpress to do so) the code just magically disappears from the file.

I am now wondering

  • Why is this happening?
  • What can I do so that this won't happen again?

Thanks for your help


Solution

  • Hey you just mentioned that you've added code into /wp-includes/functions.php file. You've used action and filters that's good but your code should be go into active theme's (child theme is the best practice) functions.php file. Files under /wp-admin/ and /wp-includes/ get updated automatically from WP when new update is available (based on settings you have done on your WP setup you can ignore updates as well).

    So, your code must be removed by update. Files were overridden by updates. If you have backup copy those code and put it into active theme's functions.php file /wp-content/themes/yourtheme/functions.php is the correct file where you can add the code.

    If you have added code into theme's functions.php file make sure you didn't get theme updated otherwise you may lost your work as well that's the reason you can use the Child theme. You can learn how to create child theme here: https://developer.wordpress.org/themes/advanced-topics/child-themes/