wordpress - create seo friendly lnks from my plugin - wordpress

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

Related

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

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.

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.

How to Add .html to the End of a WordPress URL and hide /product-category/

I use the Remove Taxonomy Base Slug plugin to hide in the url /product-category/
For example site.ru/product-category/cars/sport/bmw make site.ru/cars/sport/bmw
the plugin does a good job with it, but it's impossible to add everywhere to the end of the url .html to have site.ru/cars/sport/bmw.html instead of site.ru/product-category/cars/sport/bmw
site.ru/cars.html instead of site.ru/product-category/cars
In the settings of permanent links installed /%postname%.html
but it still does not work, it seems because of the rules of the plugin
Yes, We can hide and show category into URL with .html extension into a page. I was developing one plugin for .html URL for WordPress.
check plugin and add your comment. also, we provide support.
Plugin linn Wordpress add .htmli into custom post and category list page
Sample Code :
function jn_htmlInUrl_page_permalink() {
global $wp_rewrite;
if ( in_array( 'page', $this->selected_post_type ) ) {
if ( ! strpos( $wp_rewrite->get_page_permastruct(), '.html' ) ) {
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
$wp_rewrite->flush_rules();
}
if you fetching issues and comment here or plugin details page.
i will try to help resolve issues

Wordpress Multisite : Default page for newly created site

I am new to wordpress multisite.
whenever I create a new sub-site and visit that sub-site it show 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!'
My problem is I want to set a sample(Default)page for all the newly created sub-sit with custom text menu and theme instead of their default post.
is there any plugin that can help.
So my question is how to set sample(Default) page for each sub-site? instead of setting those page manually.
Any way to get rid of this issue?
Thanks!
By default, wordpress shows the latest posts page as front page when you create a new site
You can change that by going to Settings > Reading.
or you can use Refrence action:
function wporg_wpmu_new_blog_example( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
global $switched;
switch_to_blog($blog_id);
update_option('page_on_front', 'sample page id');
restore_current_blog();
}
add_action( 'wpmu_new_blog', 'wporg_wpmu_new_blog_example', 10, 6 );

How to create Custom Pagination Permalinks in wordpress

I have an article with several pages in my wordpress blog. if for example i have the following link in my blog :
http://example.com/heartbreaking-photos
any idea how can i change the link of the second page from
http://example.com/heartbreaking-photos/2
to http://example.com/heartbreaking-photos/CUSTOM-STRING
CUSTOM-STRING aimed to be a custom title inside the page
To achieve this, you will need to do 2 things:
Disable the default WordPress canonical redirect - this is necessary, because WordPress will always redirect to the /2/ page when it encounters the page parameter in the URL or query args.
Add a custom rewrite rule to direct your custom title to the second page of your page - this is essentially necessary to allow the link format that you want.
In terms of code, this is what you need (this is a working solution, I've just tested it locally):
// Removes the canonical redirection
remove_filter( 'template_redirect', 'redirect_canonical' );
// Add custom rewrite rules
add_action( 'init', 'my_add_custom_rewrite_rules' );
function my_add_custom_rewrite_rules() {
// Slug of the target page
$page_slug = 'heartbreaking-photos';
// Page number to replace
$page_num = 2;
// Title you wish to replace the page number with
$title = 'custom-string';
// Add the custom rewrite rule
add_rewrite_rule(
'^' . $page_slug . '/' . $title . '/?$',
'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'
);
}
There are three things you might want to configure or change here:
$page_slug - this is the slug of your page, in your case this is heartbreaking-photos
$page_num - the number of your pagination page, in your case this is 2
$title - the title you wish to use instead of your page number 2.
Feel free to alter the code as you wish, or copy it to cover more additional cases, similar to this one.
EDIT
Important: Once you use the code, go to Settings > Permalinks and click the "Save Changes" button. This will rebuild your rewrite rules, and is necessary for the solution to work.
Hope that helps. Let me know if you have any questions.
You can try this codex. Pass the arg and you will get page id, page title and use those
https://codex.wordpress.org/Function_Reference/get_pages
Or you can call page title by page id
$pagetitle= get_post_field( 'post_title', $page_id );
Ok, so basically you don't want to display the navigation link under the page (use css or modify the post template in the child theme) and add your custom link. If I understand it well:
Remove navigation links (depends on your theme, but basically):
.nav-links { display: none; }
You can add the custom link through function + custom fileds:
create a custom field, for example "my-url" in your post, see codex: https://codex.wordpress.org/Custom_Fields
add to your functions.php (in the child theme or in a custom site plugin):
function my_page_add_to_content( $content ) {
if ( ! empty(get_post_meta( get_the_ID(), 'my-url', true ) ) {
$content .= 'URL TEXT HERE'
}
return $content;
}
add_filter( 'the_content', 'my_page_add_to_content' );

Resources