WP Rewrite structure page with id parameter ?id=[id] to /id/[id] - wordpress

Now I have a page Detail Field (slug: detail-field, template name: Detail field template)
Currently, I use parameter ?id=[id] for send data to this page: http://example.com/detail-field?id=69. Base on $_GET['id'] It will get data of field and show to front-end.
But this slug not good for SEO and have some problems when share to facebook social. So I want to change it to http://example.com/detail-field/id/69
I'm not good at rewrite and I don't know where to start to do this. Can you guys help me?

WordPress Rewrite Structure:
Add Below function in function.php
function myplugin_rewrite_tag_rule() {
add_rewrite_tag( '%detail-field%', '([^&]+)' );
add_rewrite_rule( '^detail-field/([^/]*)/?', 'index.php?detail-field=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);
Then reset permalink.
Hope this works for you.

Related

Wordpress Rewriting Rules

I have a Wordpress Multisite installation and I add custom post type named course in blog 1 and then I select the other blogs where I want see it.
In other blogs add a page that I use like single course template.
The url of this page is
http://example.com/blogName/course/?course-name=lorem-ipsum&course-id=xx
I would like to have a url like this
http://example.com/blogName/course/lorem-ipsum
and i would like to get the course-id parameter in template page.
I tried to use add_rewrite_rule but i'm not able to do what i want.
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'course-name';
return $vars;
}
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^course/([^/]*)/?','index.php?course-name=$matches[1]','top');
}
How can I do this?
I need to add something to .htaccess?
Once you've executed the above hooks, find a way to call the flush rewrite function which will update the rewrite cache so it should start working.
flush_rewrite_rules( true );
Documentation for this function can be found on the developer docs site.
You can indirectly call that function too by just saving the Permalinks settings in the dashboard by going to Settings > Permalinks.

Wordpress custom post type and custom taxonomy url rewrite

Hello i have a wordpress website that has this current link structure
http://www.timorcarsales.com/buy-car-timor-leste/?make=honda
I would like to have something like this
http://www.timorcarsales.com/buy-car-timor-leste/honda
The cars are added through a custom pot type, and the make comes form a custom taxonomy
Any help would be greatly appreciated.
Thank you and have a nice day!
Copy & Check below code in your function.php file.
function add_custom_rewrite_tag(){
add_rewrite_tag('%make%', '([^&]+)');
}
add_action('init', 'add_custom_rewrite_tag', 10, 0);
function add_custom_rewrite_rule() {
add_rewrite_rule('^buy-car-timor-leste/([^/]*)/?','index.php?page_id='.templateID.'&make=$matches[1]','top');
}
add_action('init', 'add_custom_rewrite_rule', 10, 0);

Add specific words to WordPress custom type slug

I have a WordPress installation with a custom type (places) wich produces this permalink structure:
http://example.com/places/the-first-site/
And I would like to show all my sites like this:
http://example.com/places/visit-the-first-site-and-enjoy/
where 'visit' and 'and-enjoy' would be always those specific words (constants).
Even better I would like to put a custom taxonomy I have (year) as a metadata
places/visit-the-first-site-and-enjoy-1985/
I can access the DB and modify the post name of all post, but I would have to do for the new post also, and I'm looking for some automated rule, but can't find how to do.
Maybe some rewrite rule, but I don't know how to do it.
Any ideas??
You do not have to edit anything in your database nor any codes in your WordPress installation; you can achieve that directly from your WordPress administration panel.
From your Dashboard, click on the Permalink sub-menu under Settings; there, you will have to select the Custom Structure option and set it as follow in order to achieve your desired effect:
/places/visit-%postname%-and-enjoy-%year%/
Please note: here, we made use of both %postname% and %year% Structure Tags so as to get names of posts with their corresponding year of publication respectively.
Don't forget to click on the Save Changes button on the page in order to effect your changes.
... Read more on Permaklinks (for general knowledge).
For a custom post type and taxonomies as expressed further in comments, you will need a custom solution which will require a little bit of coding and or tweaking, depending on your abilities; you may use this handy plugin (Custom Post Type Permalinks) from the WordPress.org Plugins repository.
This posts should equally be of great help to you, to getting started and understanding further, should you chose to code.
You have différent hook like save_post or pending_to_publish, where you can set or change the "post_name" (that's corresponds to the permalink slug).
To Add specific words to WordPress custom type slug You have to Register custom rewrite rules.
suppose this is Your URL http://example.com/places/the-first-site !
and you have a post type places.
function add_rewrite_rules()
{
$wp_rewrite->add_rewrite_tag('%places%', '([^/]+)', 'places=');
$wp_rewrite->add_rewrite_tag('%state%', '([^/]+)', 'state=');
$wp_rewrite->add_permastruct('places', 'places/visit-%state%-and-enjoy/', false);
}
function permalinks($permalink, $post, $leavename)
{
$no_data = 'no-data';
$post_id = $post->ID;
if($post->post_type != 'story' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$state = get_post_meta($post_id, 'location', true);
if(!$state)
$state = $no_data;
$permalink = str_replace('%state%', $state, $permalink);
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);
put this code in your custom post type plugin. and if get page not found error please change your permalink setting. or make a custom template file to show this post.

Wordpress add_rewrite_rule gives 404

I want to change the permalink structure for specific custom post type.i.e
http://test.com/brand/calvin-klien?mtype=tab2
^this is dynamic
To
http://test.com/brand/calvin-klien/mtype/tab2
^this is dynamic
Here is a piece of code I tried.
Registering add_rewrite_tag
function custom_rewrite_tag() {
add_rewrite_tag('%mtype%', '([a-z0-9\-]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
add_action('init', 'wpse50530_journal_archive_rewrite', 10, 0);
Code1
function wpse50530_journal_archive_rewrite(){
add_rewrite_rule('brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$','index.php?name=$matches[1]/?mtype=$matches[2]','top');
}
Code2
add_action('generate_rewrite_rules', 'work_list');
function work_list($wp_rewrite) {
$newrules = array();
$newrules['brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$'] = 'index.php?name=$matches[1]&mtype=$matches[2]';
$wp_rewrite->rules = $newrules + $wp_rewrite->rules;
I have tried both above codes, flushed permalinks but still a 404. I dont know why it is creating $matches in htaccess as htacces doesnt know WHAT IS $matches
Also I have tried monkeyman-rewrite-analyzer plugin which is showing the correct matched result for my permalink but still word press showing 404. See attached screenshots for Code1 & Code2
The following code should help
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule(
'^brand/([^/]*)/mtype/([^/]*)/?',
'index.php?name=$matches[1]&mtype=$matches[2]',
'top');
}
Flush your permalinks and it should work.
I will continue Anand's code for some further changes. It was redirecting on name=$matches[1] and I need to stay at the same URL I hit, for this it must include the custom post type name.
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule('^brand/([^/]*)/([^/]*)/?','index.php?brand=$matches[1]&mtype=$matches[2]','top');
^--this
}
I got the Pretty URL... Yaaay!!!
BUT URL doesn't contain query string(i.e: mtype=tab1) and the rest of my code is useless, so we can achieve this by doing get_query_var('mtype') and I got the same value which was working in $_REQUEST['mtype'] and my code worked like a charm.
Also I deactivated the monkeyman pluggin
Just in case anyone has the same problem and checks out this page.
In the permalink settings page, check how the permalink structure is configured. The most common is to enter /%postname%/ in the custom structure field to let the rewrite_rules work properly.
before or after the call the function add_rewrite.. insert this code status_header(200);

wordpress - create seo friendly lnks from my plugin

I'm creating a Wordpress plugin with this address
//worpress/my-plugin/
and I'd like to pass (by GET) some variables in seo friendly form like
//worpress/my-plugin/first-var/second-var/
Is there any way to "create" and "get" these variables from my plugin without changing the .htaccess file (the permalink setting is already set in "Post name" mode) but just inserting some code in my plugin page?
Thank you!
You can do this by Rewrite api of wordpress:
below is a small example:
add_action('init', array($this, 'add_rules'));
add_rewrite_rule('nameofurl/?([^/]*)', 'index.php?page_id=' . $post_ID . '&jsubscribe_id=$matches[1]', 'top');//if you need this for any specefic post.
/* OR */
add_rewrite_tag('%action%', '([^/]+)');
add_permastruct('action', 'url_prefix_like_plugin_name' . '/%action%');
/*After adding permastruct do below work*/
add_filter('template_redirect', 'function_name');
/*Get you query value and do what you want*/
function function_name(){
$action=get_query_var('jaction');
//put your login here
}
and don't forget to flush_rewrite_rules();
for more information visit to below links:
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
http://codex.wordpress.org/Rewrite_API/add_rewrite_tag
http://codex.wordpress.org/Function_Reference/add_permastruct

Resources