I'm writing a custom theme and have a custom post type which represents an external entity (e.g. a website). Hence, I have a field 'external_url' in my post type. Part of the custom post type post's template is a button that takes the user to the external website. However, instead of linking directly to an external ressource, I rather want to mask the original link to make it look like an internal link (e.g. http://example.com/go/unique-slug) and process it through a HTTP 301 redirection script. This link masking should happen automatically, when an editor adds a link to the very field.
I know, there are tons of plugins out there that cloak or mask links. I have tested Easy Affiliate Links, Thirsty Affiliates and Simple Urls. However, these plugins all seem to create their own custom post types. This results in a workflow, where an editor always has to separately add a new link and associate this link later in his new post.
My ideal work flow would allow to simply add the real external URL in the appropriate post's field. A custom functionality or plugin would automatically create and provide an internal URL, which would be called in the posts template and which would call the redirection script or simply perform the redirect straight away.
What I am looking for is some guiding on how to principally solve that task. Any kind of flow chart, meta code wireframe or verbal description would be appreciated, say
- create your own plugin - write some function create_internal_slug() which creates unique slug for internal link - write function create_internal_link() which creates the internal link - write function save_internal_link() which saves link ... but where? - create an action hook on 'matching_hook' and apply your save_internal_link() function there - etc. ...