Editing other PHP file carries the risk of breaking the rest of the code in the file and also opens you to the potential that the code will be lost when you update.
- Go to Plugins → Add New, search Code Snippets and install and activate the plugin. This will add a Snippets item to your wp-admin dashboard menu.
- Navigate to Snippets → Add New.
- Enter a title.
- Paste or type your code in the Code field.
- Click “Save Changes and Activate”.
Install via WP-CLI command.
If you have set a priority on the filter/hook. It is best that you set the priority at the same level. You also want to set/leave it on “Run snippet everywhere”.