I think things like that are typically added inside your Child Theme's folder and then called from your Child Theme's functions.php file. However, it is also possible to make your own plugin and do all of that right there.
https://www.google.com/search?q=how+to+make+custom+wordpress+plugin