Moved filtering logic into a separate function
to make it easier to call it in other wordpress filters