0

How to remove ?p= from WordPress short links?

I removed ?p= from wp_get_shortlink and it's get the links without ?p= successfully but the problem that this links not working until added ?p= so how can make links working without added ?p=.

4
  • The question as it is right now do not make much sense. Those are the wordpress short links, if you are not happy with them, make your own.... Commented Apr 29, 2017 at 4:43
  • do you want to display url like e.g. : site-url.com/post instead of site-url.com/?p=22 ?? Commented Apr 29, 2017 at 5:44
  • @PunitPatel To appear like this: site-url.com/22 (without ?p=)
    – Yogu
    Commented Apr 29, 2017 at 5:45
  • 1
    @Yogu It's very simple just add %post_id% in setting > permalink > in post name. [![Please check image for solution][1]][1] [1]: i.sstatic.net/p3ovY.png Commented Apr 29, 2017 at 6:54

4 Answers 4

1

I am not sure how you removed ?p= from the link but you can use get_shortlink filter to override the shortlink. You can refer following article for more details.

http://www.wpbeginner.com/wp-themes/how-to-display-wordpress-shortlinks-in-your-theme/

Reference to get_shortlink filter: https://developer.wordpress.org/reference/hooks/get_shortlink/

Can you share some more details on how you removed the ?p= from the link?

1
  • I replace $shortlink = home_url( '?p=' . $post_id ); to $shortlink = home_url( '' . $post_id ); from wp_get_shortlink and i used it like this wp_get_shortlink(post_id), and it's wokring the issue when go to the link it's give me 404 not found.
    – Yogu
    Commented Apr 29, 2017 at 4:36
1

@yogu You can add %post_id% in permalink to get required structure. site-url.com/22 (without ?p=)

For specific post you can add following function and add specific condition for it :

add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // For shortlink condition
    if (  'Shortlink Condition here' ) {
        $permalink = trailingslashit( home_url('/'. $post->ID .'/' ) );
    }
    return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
    // This rule will will match the post id in %post_id% struture
    $new_rules['^([^/]*)-([0-9]+)/?'] = 'index.php?p=$matches[2]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}
2
  • Sorry for this but sure I know that but what I need to do to remove ?p= for the short links only not the all posts links.
    – Yogu
    Commented Apr 29, 2017 at 16:52
  • I have updated answer as per your need. Try it. Commented May 1, 2017 at 9:15
0

Wordpress redirects without the parameter: ?p. Regardless of whether it's pages or posts ...

For example: site.com/922 (post or page ID...), this should redirect.

If you want to change link one at the head of your site:

add_filter('pre_get_shortlink', function () {
    return home_url('/') . get_the_ID();
}, 10, 3);

In wordpress docs: https://developer.wordpress.org/reference/hooks/pre_get_shortlink/

0

I handled this with wp_redirect in a very easy way:

I have already responded to ur needs here: https://wordpress.stackexchange.com/a/417549/143910

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.