Redirecting to a URL saved in a meta-field, to keep permalink structure intact and stable for SEO
Often times for projects, like Bartaco, in the top nav there will be a need to link to an external PDF or site.
For SEO reasons, it’s better to have a link like this:
http://bartaco.com/marketplace/
Rather than this:
http://bartaco-marketplace.myshopify.com/
If the client decides to change shop providers, then there is the potential to take an SEO hit. Same with things like like Food Menus that link to a PDF, that change seasonally.
http://bartaco.com/menu/
Is much better than:
http://bartaco.com/wp-content/uploads/spring-20160-menu.pdf
The solution is pretty simple. You create a meta field for your link. This is how I implemented this in bartaco with Advanced Custom Fields:
I have a template called “template-redirect”. When the page is set to this template, then the above meta field is the only thing that displays on this page.
The template itself, doesn’t have any presentational code at all, but simply checks that meta field, and redirects accordingly:
<?php /* Template Name: Redirect Simply forwards to the link set on the page. */ $redirect_type = get_field('internal-or-external'); if ($redirect_type == 'internal') { $redirect_link = addhttp(get_field('redirect-file')); } else $redirect_link = addhttp(get_field('redirect-link')); if ($redirect_link != '') wp_redirect($redirect_link); else wp_redirect(home_url()); exit; ?>
That’s pretty much it.
If you’re using ACF, you can import the following json to create that link field above:
[ { "key": "group_5695463fc80af", "title": "Link", "fields": [ { "key": "field_569546715033f", "label": "Internal or External Link?", "name": "internal-or-external", "type": "radio", "instructions": "", "required": 1, "conditional_logic": 0, "wrapper": { "width": "", "class": "", "id": "" }, "choices": { "internal": "Internal", "external": "External" }, "other_choice": 0, "save_other_choice": 0, "default_value": "", "layout": "vertical" }, { "key": "field_569547f450340", "label": "Select Internal Link", "name": "redirect-file", "type": "file", "instructions": "", "required": 0, "conditional_logic": [ [ { "field": "field_569546715033f", "operator": "==", "value": "internal" } ] ], "wrapper": { "width": "", "class": "", "id": "" }, "return_format": "url", "library": "all", "min_size": "", "max_size": "", "mime_types": "" }, { "key": "field_5695481c50342", "label": "Enter External Link", "name": "redirect-link", "type": "text", "instructions": "", "required": 0, "conditional_logic": [ [ { "field": "field_569546715033f", "operator": "==", "value": "external" } ] ], "wrapper": { "width": "", "class": "", "id": "" }, "default_value": "", "placeholder": "", "prepend": "", "append": "", "maxlength": "", "readonly": 0, "disabled": 0 } ], "location": [ [ { "param": "page_template", "operator": "==", "value": "template-redirect.php" } ] ], "menu_order": 0, "position": "normal", "style": "default", "label_placement": "top", "instruction_placement": "label", "hide_on_screen": [ "the_content", "excerpt", "custom_fields", "discussion", "comments", "revisions", "slug", "author", "format", "featured_image", "categories", "tags", "send-trackbacks" ], "active": 1, "description": "" } ]
About north street
We engineer the thoughtful transformation of great organizations. Our proven process helps us understand what your competitors are doing right — and wrong. Want to learn more? Let’s chat.