Disable Access To Site Pages via Short Link URL – WordPress

Advertisement

This little snippet simply redirect your site into 404 or into your site home-page if your site visitors trying to navigate your site using short link url.

Why this snippet helpful?

– There are few reasons why we can tell this snippet is useful and the most common one is to avoid Google indexing the short link URL instead of the user friendly and that affect site ranking.

Here’s a sample


// friendly url
http://www.mydomain.com/hello-world/

// short link url
<blockquote data-secret="Z1CHMYS9E7" class="wp-embedded-content"><a href="https://www.ryansutana.name/sample-page/">Sample Page</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="https://www.ryansutana.name/sample-page/embed/#?secret=Z1CHMYS9E7" data-secret="Z1CHMYS9E7" width="600" height="338" title="&#8220;Sample Page&#8221; &#8212; Sutana Ryan Blog and Portfolio Site" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

So if some one trying to access the short url they’re automatically redirected into 404 page or your site home-page.


// add this code in your theme functions.php file
function short_link_template_redirect()
{
    if(isset($_GET['p']) && $_GET['p']!='') {
    {
        wp_redirect( home_url( '/404/' ) ); // remove '/404/' if you want it to redirect to site home-page
        exit();
    }
}
add_action( 'template_redirect', 'short_link_template_redirect', 8 );

That’s it happy coding ^_^

Advertisement