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. ...
  • 1
    How are you attaching the custom fields in the admin? You can hook into the save of your external_url field, turn it into whatever you need and save it as another field which the admin user never sees. In the template, output/process that field. – Will Feb 8 '16 at 18:48
  • I use this plugin called Pods which takes care of all the custom post types and custom fields. If I understand you right, I could add another custom field to my post type and hide this from admin (or have it appear read-only). Then in my plugin, I add an action hook, which fills the extra field of my post type with the generated internal URL? – Bunjip Feb 8 '16 at 19:31
  • Yep. That's how I'd do it. Check out the pre_save_pod_item filters. When external_url is saved, you can process it there and stuff it into another field. – Will Feb 8 '16 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.