Custom post type url plain ID in address bar - permalinks

I want to show plain urls for all custom post types in browser address bar, Changed permalinks to plain and to %post_id%. and tried many different functions but no luck, the server runs on nginx, so htaccess not effecting.
The url below takes me to the right post but prints its slug in the address bar.
I Want the link to look ugly like that in the address bar
https://tumtime.com/?p=2146
any suggestion is apricated
```your text``
The code below prints the desire url in browser bar but redirects all posts to 404
function recipe_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=recipe&?p=';
$wp_rewrite->add_rewrite_tag( '%cpt_id%', '([^/]+)', $queryarg );
$wp_rewrite->add_permastruct( 'recipe', '/?p=%cpt_id%/', false );
}
add_action( 'init', 'recipe_rewrite' );
```'`your text`

Related

Wordpress custom post type change permalink query

My permalink rules are set to post name. I created a custom post type named: house.
The default url would be: {{site_url}}/house/{{house_slug}}
Now I want to change the way the house url is used, I want it to accept the following:
/house/param1:value1/param2:value2/param3:value3
I would like it that the query object used in the pre_get_posts method contains the post type (house) and the sent parameters.
I tried the following rewrite rules 1 by 1 but couldn't get a single one working:
add_rewrite_rule('^house/([a-z0-9-]+)[/]?$', 'house/$matches[1]', 'top');
add_rewrite_rule('^house/(.*)[/]?$', 'house?test=$matches[1]', 'top');
add_rewrite_rule('^house/([^/]+)/?$', 'index.php?post_type=house&name=$matches[1]', 'top');
If you will accept anything after /house you can use: /([^/]+)/. So your last one should be it: add_rewrite_rule( 'house/([^/]+)/?$', 'index.php?post_type=house&name=$matches[1]', 'top' );
It's important to flush the rewrite rules by re-saving the permalinks in WordPress: settings -> permalinks -> save.
That should do the trick.
You can check this by exporting your rules (in functions.php):
add_action( 'parse_request', 'debug_404_rewrite_dump' );
function debug_404_rewrite_dump( &$wp ) {
global $wp_rewrite;
echo '<h2>rewrite rules</h2>';
echo var_export( $wp_rewrite->wp_rewrite_rules(), true );
}

Wordpress rewrites not working and shows 404 page

I want to load a page with this requests:
/foo/bar
/foo/bar/bar
But this isn't working and shows 404 page:
function custom_rewrite_rule() {
add_rewrite_rule('^foo\/','index.php?page_id=205533','top');
}
add_action('init', 'custom_rewrite_rule');
The same piece of code worked.
But the settings for the Permalinks needed to be saved again.
From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.
function wpd_foo_rewrite_rule() {
add_rewrite_rule(
'^foo/([^/]*)/?',
'index.php?page_id=$matches[1]&param=foo',
'top'
);
}
add_action( 'init', 'wpd_foo_rewrite_rule' );
This takes whatever comes after foo/ and sets that as pagename for the query, and then param gets the static value foo. If you need different URL patterns, you'll need extra rules for each unique pattern

How to change Wordpress dashboard login url to website URL?

I have a Wordpress website
www.example.com
Once a user goes to URL he sees Wordpress login page and below URL in the browser address bar.
www.example.com/wp-admin/....
I want to change that so when people go www.example.com they see the login page and the url in the browser address bar remains same www.example.com or in other words I dont want that slug and redirect text seen in the address bar. Is that possible to do?
I don't know that you can rewrite wp-admin, but you could create your own login code to display on the Home page. To start with, wp_login_form() displays a login form:
<?php wp_login_form(); ?>
You could add this code to a page template, or create a shortcode so it can be added via the Dashboard editor like this:
add_action( 'init', 'stackoverflow_login_shortcode' );
function stackoverflow_login_shortcode() {
add_shortcode( 'stackoverflow-login-form', 'stackoverflow_login_form_shortcode' );
}
function stackoverflow_login_form_shortcode() {
if ( is_user_logged_in() ) {
return '<p>You are already logged in!</p>';
}
return wp_login_form( array( 'echo' => false ) );
}
Now you can add [stackoverflow-login-form] into a page.

Wordpress remove permalink from custom posts but retain archive

I have a Wordpress site where I keep track of publications with an archive of custom post types called "publications". Each publication post shouldn't have its own page, it just needs to appear in the archive page. Right now when I create a publication called "test publication" there is a page created at www.mysite.com/publications/test-publication. I've seen the suggestion of changing the post option public to false, but then I can't access the archive itself, it just redirects to the home page. If I add publicly_queryable => true in addition to public => false then I can get to the archive page, but the dedicated page for each publication shows up again. I need it to give me a 404 if I try to visit www.mysite.com/publications/test-publication but still allow me to access the archive. Help, am I missing something obvious?
You can have template redirect added so that singular link if accessed redirects to Archive page:
add_action( 'template_redirect', 'disable_singular_publications' );
function disable_singular_publications()
{
if ( ! is_singular( 'publications' ) )
return;
wp_redirect( get_post_type_archive_link( 'publications' ), 301 );
exit;
}
You may add above function in functions.php , code is not tested so you may need to check on any typos or syntax errors.

Converting Wordpress URL path into query string

I have a Wordpress page that accepts a query string argument:
http://x.com/page-name/?parameter=value
This works fine. The page gets the value of $_GET['parameter'] correctly.
What I want to do is make it possible to type this as a normal URL:
http://x.com/page-name/value
I need the ability to rewrite the URL so the user enters URL 2, and Wordpress gets URL 1. I am using Apache and would prefer to do this with mod_rewrite and .htaccess. Any advice?
You can do this within WP using your theme's functions.php:
add_action( 'init', 'ss_permalinks' );
function ss_permalinks() {
add_rewrite_rule(
'page/remove/([^/]+)/?',
'index.php?pagename=page&service=$matches[1]',
'top'
);
}
add_filter( 'query_vars', 'ss_query_vars' );
function ss_query_vars( $query_vars ) {
$query_vars[] = 'removeid';
return $query_vars;
}
Re-save your permalink settings once after implementing. page is the slug of the page to point to when the user access this URL (domain.com/page/remove/432), and $matches[1] should be the number after remove/ in the URL. This number is accessible by the variable specified later, $query_vars[] = 'removeid';/ $removeid on the target page's template will be the number in the URL, if specified.

Resources