Converting code to WordPress functions and hooks. Still requres testing