Yes, it is legal to add custom code to your WordPress site. WordPress is an open-source platform that allows users to modify and customize their websites according to their needs.