0

I have a weird problem. I have a social sharing plugin that is fetching social shares via the social network's APIs. However, if a user changes their URL structure, most social networks will no longer show the shares for the new URL, because the shares occurred on the old URL.

Is it possible to fetch a permalink from WordPress in a different format than what it is set at in the user permalinks settings? This would allow me to check against the alternate format (old shares) and the current format (new shares).

WordPress has the following permalink structures available: Default, Day and name, Month and name, Numeric, and Post name.

I would love to be able to use get_permalink() and check that against the social APIs (which is what I currently do), and also be able to use some sort of function like get_alternate_permalink('Numeric') so that I can check both against the API and add them up (for networks that don't already honor 301's in their total).

Any ideas?

3
  • 1
    Take a look at shortlinks: codex.wordpress.org/Function_Reference/wp_get_shortlink Regardless of your permalink structure these won't change. Commented Jan 24, 2015 at 0:34
  • @NathanDawson That solves getting the "default" version of the permalink. But I have a friend who just lost all their social shares because they switched from "Day and name" to "Post name" a few months ago. So in her case, I need to be able to fetch that version of the "Day and name" version of the permalink. Commented Jan 24, 2015 at 0:36
  • 1
    Hi Nicholas, that's a bit more complicated although certainly possible. If you run into an issue when building that functionality I'm happy to help you solve it. As far as I'm aware there's no out of the box solution. Commented Jan 24, 2015 at 7:39

1 Answer 1

1

Here's the solution that I came up with. This provides the permalink using all the the standard WordPress permalink formats.

function get_alternate_permalink($format) {

    // Setup the Default Permalink Structure
    if($format == 'Default'):
        $domain = get_site_url();
        $id = get_the_ID();
        $url = $domain.'/?p='.$id;

    // Setup the "Day and name" Permalink Structure
    elseif($format == 'Day and name'):
        $domain = get_site_url();
        $date = get_the_date('Y/m/d');
        $slug = basename(get_permalink());
        $url = $domain.'/'.$date.'/'.$slug;

    // Setup the "Month and name" Permalink Structure
    elseif($format == 'Month and name'):
        $domain = get_site_url();
        $date = get_the_date('Y/m');
        $slug = basename(get_permalink());
        $url = $domain.'/'.$date.'/'.$slug;

    // Setup the "Numeric" Permalink Structure
    elseif($format == 'Numeric'):
        $domain = get_site_url();
        $id = get_the_ID();
        $url = $domain.'/archives/'.$id.'/';

    // Setup the "Post name" Permalink Structure
    elseif($format == 'Post Name'):
        $domain = get_site_url();
        $slug = basename(get_permalink());
        $url = $domain.'/'.$slug;

    endif;

    return $url;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.