Skip to main content
added 2 characters in body
Source Link
Ken
  • 369
  • 4
  • 15

Also on index.php I have products there pulled from mysql database in which accessible by links like http://website.com/store/?slug=product_info.php&product_id=38php&products_id=38 and so on.

    like http://website.com/store/?slug=product_info.php&product_id=38php&products_id=38 
    to become http://website.com/store/product-info/38

Also on index.php I have products there pulled from mysql database in which accessible by links like http://website.com/store/?slug=product_info.php&product_id=38 and so on.

    like http://website.com/store/?slug=product_info.php&product_id=38 
    to become http://website.com/store/product-info/38

Also on index.php I have products there pulled from mysql database in which accessible by links like http://website.com/store/?slug=product_info.php&products_id=38 and so on.

    like http://website.com/store/?slug=product_info.php&products_id=38 
    to become http://website.com/store/product-info/38
Source Link
Ken
  • 369
  • 4
  • 15

Using WP Rewrite to rewrite custom urls in this scenario

I have build a plugin where I can output special php pages on a page using shortcode.

The plugin directory consists of the following php pages.

product_info.php
index.php
account.php
login.php
checkout.php
core.php

The shortcode function which is on core.php is as follows,

function output_store(){
if(!isset($_REQUEST['slug']))
    include(WP_PLUGIN_DIR.'/'.basename(dirname(__FILE__)).'/index.php');       
if($_REQUEST['slug'])
    include(WP_PLUGIN_DIR.'/'.basename(dirname(__FILE__)).'/'.$_REQUEST['slug']);   
}
function filter($content = '') {
    if ( '' == $content || !strstr($content, '[WP_online_store]') ) { return $content; }
    return preg_replace('|(<p>)?(\n)*[(\[)WP_online_store(\])](\n)*(</p>)?|', do_shortcode( '[WP_online_store]' ), $content);
}
add_shortcode('output_store', 'output_store');
add_filter('the_content', 'filter');

[output_store] will be placed on any page for example on store page so when I visit the store page the default content of that page will display the index.php file from the plugin directory.

In the index.php file I have links for accessing login.php, product_info.php, account.php and checkout.php each has url of:

login.php => http://website.com/store/?slug=login.php
product_info.php => http://website.com/store/?slug=product.info.php
account.php => http://website.com/store/?slug=account.php
checkout.php => http://website.com/store/?slug=checkout.php

Also on index.php I have products there pulled from mysql database in which accessible by links like http://website.com/store/?slug=product_info.php&product_id=38 and so on.

Now I wanted to rewrite these urls to be search engine friendly. I wanted:

http://website.com/store/?slug=login.php to become http://website.com/store/login
http://website.com/store/?slug=account.php to become http://website.com/store/account
http://website.com/store/?slug=checkout.php to become http://website.com/store/checkout

and all product urls:

    like http://website.com/store/?slug=product_info.php&product_id=38 
    to become http://website.com/store/product-info/38

How can I accomplish that using wordpress's WP Rewrite?

I tried this for the product info pages:

// Define permalinks
function add_query_vars($aVars) {
$aVars[] = "products_id";
return $aVars;
}


function add_rewrite_rules($aRules) {
$aNewRules = array('product-info/([^/]+)/?$' => 'index.php?slug=product_info.php&products_id=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter('query_vars', 'add_query_vars');
add_filter('rewrite_rules_array', 'add_rewrite_rules');

function isset_productinfo() {
  if(isset($wp_query->query_vars['products_id'])) {
   $sMsdsCat = urldecode($wp_query->query_vars['products_id']);
  }
}
add_action('init','isset_productinfo');

// End Special Permalinks

Flushed my permalinks and its not working.. When I access http://website.com/store/product-info/38 and goes on http://website.com/store/ instead of displaying the product page. How can I fix this? or do you have any other solutions?

Please help..