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=.
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=.
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?
@yogu You can add 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;
}
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/
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