Where to add one custom end point and its function?

Where should I add the code in the sources files of WP?

Never, under any circumstances modify WordPress itself, everything will be lost when you update WordPress

So instead, create a plugin!

A plugin is just a PHP file in the plugins folder with a comment at the top like this:

<?php
/**
 * Plugin Name: Test Plugin
 */

Note that the above code block is actually a fully functioning plugin, if you drop it in your plugins folder and go to the plugins page, you’ll see Test Plugin listed, and can activate it, although it won’t do anything.

You can also create a folder and put the file in there, which you should do if you want to have more than 1 file. E.g. test-plugin/plugin.php. Note that it doesn’t have to be called plugin.php, I just use that for convention.

Anything that can go in a themes functions.php can go in here, and vice versa.


Note: Some people install lots of plugins which slows their sites down, so people say having lots of plugins slows your site down. This leads to the incorrect assumption that plugins themselves are what slows the site down, and you can move their contents into a theme to speed things back up again, which is nonsense. The cost of a plugin is what it does, you’re still doing that if you move it into a theme so it’ll be just as slow. Lots of plugins is slow because you’re doing a lot of stuff, not because the number of plugins is high