I'm using WordPress with woocommerce and I am trying to rewrite a url, i.e
WEBSITE/product/celebrity-aston-recliner-fabric/?attribute_pa_size=standard-recliner&attribute_pa_action=dual-motor-lift-tilt
to
WEBSITE/product/celebrity-aston-recliner-fabric/standard-recliner/dual-motor-lift-tilt/
Can anyone help?
Managed to get wordpress to accept the url:WEBSITE/product/celebrity-aston-recliner-fabric/standard-recliner/dual-motor-lift-tilt/ with this:
function custom_rewrite( $wp_rewrite ) {
$feed_rules = array(
'product/(.+)/(.+)/(.+)' => 'index.php?product='. $wp_rewrite->preg_index(1).'&attribute_pa_size='. $wp_rewrite->preg_index(2).'&attribute_pa_action='. $wp_rewrite->preg_index(3)
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );
However the first URL still exists and the new URL doesn't set the variataion
Related
I was working on creating a WP plugin that will load other plugins only on specific URL. These plugins are deactivate in WP admin plugins, and are only loaded when a specific page is accessed.
Within my plugin's Class construct function:
$uri = $_SERVER['REQUEST_URI'];
$exp = explode('/', $uri);
$uri = $exp[2];
$options = get_option( $this->plugin_name );
$key = array_search( '/'.$uri.'/', array_column($options, 'url') );
$plugin_dir = $options[$key]['plugin']; // this prints plugin file directory ex. /MyPlugin/myplugin.php
include( WP_PLUGIN_DIR . $plugin_dir);
Above code loads the plugin on specific URL/page. Meaning the variable $plugin_dir grabbed the correct directory. BUT, problem occur when there's an AJAX request from that plugin. Ex. when i try to delete an item using ajax request, it returns Error 404 Bad request.
Weird part is, almost same code above, but this time, i manually assign the plugin directory to a variable: ex.
$uri = $_SERVER['REQUEST_URI'];
$exp = explode('/', $uri);
$uri = $exp[2];
$options = get_option( $this->plugin_name );
$key = array_search( '/'.$uri.'/', array_column($options, 'url') );
$plugin_dir = '/MyPlugin/myplugin.php'; // manually place the plugin file dir
//same output as $plugin_dir = $options[$key]['plugin'];
include( WP_PLUGIN_DIR . $plugin_dir);
But this time, plugin works really well. No Ajax bad request error.
What could be the possible explanation for this? Is there any solution about this issue, so that i can dynamically get the plugin file directory from wp options based on the Request URI.
Also, another issue. Instead of REQUEST URI, i wanted to get the POST/PAGE ID instead, but everything returns NULL/empty. Still inside the construct function, i tried different approach to get the page ID:
global $post;
var_dump($post->ID); //returns NULL
global $wp_query;
var_dump($wp_query->post->ID); //returns NULL
echo get_the_ID(); //returns empty/NULL
Is there a way how to properly get the POST/PAGE details, or even just the ID?
Thank you.
I'm trying to implement a multilanguage laravel 4 website, with language code in the url ( mywebsite.com/en/home and mywebsite.com/de/home )
I've seen a couple of options like filtering all requests and checking if the first param is one of the language code.
I've also check on packagist but haven't find something that already do tee job.
Is there a better way to implement it?
Thank you
Finally, I've create a config variable in config/app.php
'available_language' => array('en', 'fr', 'es'),
In filters.php I detect the browser language:
Route::filter('detectLang', function($lang = "auto")
{
if($lang != "auto" && in_array($lang , Config::get('app.available_language')))
{
Config::set('app.locale', $lang);
}else{
$browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
$browser_lang = substr($browser_lang, 0,2);
$userLang = (in_array($browser_lang, Config::get('app.available_language'))) ? $browser_lang : Config::get('app.locale');
Config::set('app.locale', $userLang);
}
});
and then in routes.php I can either detect the language or force it:
Route::get('/', array(
'before' => 'detectLang()', // auto-detect language
function(){
...
})
);
or
Route::get('/', array(
'before' => 'detectLang("fr")', // force language to "fe"
function(){
...
})
);
You could set a language variable in the user session.
Then use a 'before' filter, and view that variable, and log the correct language file.
If there is no variable set - then use a default (perhaps based upon their IP location).
If I have module mymodule in which I have index Controller. In which I have subaction as 'subaction'
Normally I access page as
http://www.mywebsite/index.php/mymodule/index/subaction
How can I set url from code such as
http://www.mywebsite/index.php/subaction
or
http://www.mywebsite/index.php/mymodule/subaction
Note :: I do not want to create new controller I want this in the same index controller.
Magento URL-to-controller matching works through the Standard router which expects URLs to have a specific form. If you want to change that you have a few options at your disposal:
Deprecated config-based URL rewrites
Create URL rewrite entries in core_url_rewrite table
Create a custom router class to match the URL patterns which you would like to use
When considering how URL matching should work, you need to consider how Magento will expect to build the URL using its native URL calculation tools as well as how to get requests to match.
You can do this by using routes
in your bootstrap do the following
protected function _initMyRoutes() {
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
$router->addDefaultRoutes();
$router->addConfig($config, 'routes');
return $router;
}
and in the configs directory create a file called routes.ini and in it place the following
routes.myRoute.type = "Zend_Controller_Router_Route_Static"
routes.myRoute.route = "/subaction/"
routes.myRoute.defaults.module = "mymodule"
routes.myRoute.defaults.controller = "index"
routes.myRoute.defaults.action = "subaction"
OR
you can add the route directly in your bootstrap with
protected function _initMyRoutes() {
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$router->addDefaultRoutes();
$route = new Zend_Controller_Router_Route_Static(
'subaction',
array('module' => 'mymodule', 'controller' => 'index', 'action' => 'subaction')
);
$router->addRoute('subaction', $route);
return $router;
}
That should do the trick but be adviced using routes can really be a pain.
More about routes in the ZF manual
I am using codeigniter and have a controller in which I assign some search criteria's to be stored in session like this:
$srchCriteria = array(
'stockCode'=>$this->input->post('srchScode'),
'qty'=>$this->input->post('srchQty'),
'class' => $this->router->fetch_class(),
'method' => $this->router->fetch_method(),
);
$this->session->set_userdata('srchCriteria',$srchCriteria);
And on basis of this criteria output is generated. Now I need to clear this criteria if the user navigates to another page other than this page. i.e every time the user visits the search page the search criteria should be cleared except for pagination. For this purpose I checked the class and method variables in core controller like this:
$srchCriteria = $this->session->userdata('srchCriteria');
$className = $this->router->fetch_class();
$methodName = $this->router->fetch_method();
if(!empty( $srchCriteria['class'] ) && !empty( $srchCriteria['method'] )){
if( ($srchCriteria['method'] != $methodName){
$this->session->set_userdata('srchCriteria',array());
}
}
But it is not working please guide me in right way. What is my mistake here?
Try $this->session->unset_userdata('srchCriteria'); instead of $this->session->set_userdata('srchCriteria',array());
Here's my situation: I have a bunch of HTML pages in sites/default/files/pdf. I want to serve them as is so I have links to them in my Drupal site. However, one of the requirements is that all the URLs of these HTML pages must not contain any extensions. In addition, I want it to act in such a way that when users go to example.com/sites/default/files/pdf/somehtmlfile, the URL will show as example.com/pdf/somehtmlfile and also when users visit example.com/pdf/somehtmlfile, example.com/sites/default/files/pdf/somehtmlfile will be served instead.
From my independent research, it seems that I should be using hook_url_inbound_alter() and hook_url_outbound_alter(). However, I seem to be doing something wrong because the URL does not change at all.
Below is my code. I created a module called html_extension_remover (not very imaginative name, I know). I have successfully activated the module and some debugging statements are run successfully, so I know that the module is running.
function html_extension_remover_url_outbound_alter(&$path, &$options, $original_path){
$pdf_regex = '|^sites/default/files/pdf(/.*)?|';
$pdf_new_path = 'pdf';
if (preg_match($pdf_regex,$path, $matches)) //rewrite all request to sites/default/files/pdf to pdf, looks nicer
if (count($matches)==1)
$path = $pdf_new_path;
else
$path = $pdf_new_path . $matches[1]; //append the rest of the URL, after the Regex match
if (strpos($path, $pdf_new_path)!=FALSE) //URL contains pdf, means viewing converted PDFs in pdf dir
if (strpos($path, '.htm')!=FALSE){ //if viewing .htm/.html file
$path = substr(0, strpos); //strip extension from URL
}
$pdf_new_path = 'sites/default/files/pdf';
$pdf_regex = '|^pdf(/.*)?|';
if (preg_match($pdf_regex, $path, $matches)){
if (count($matches)==1){
$path = $pdf_new_path;
}
else{
$path = $pdf_new_path.$matches[1].'.htm';
}
}
}
function html_extension_remover_url_inbound_alter(&$path, &$options, $original_path){
$pdf_new_path = 'sites/default/files/pdf';
$pdf_regex = '|^pdf(/.*)?|';
if (preg_match($pdf_regex, $path, $matches)){
if (count($matches)==1){
$path = $pdf_new_path;
}
else{
$path = $pdf_new_path.$matches[1].'.htm';
}
}
}
If I understand you correctly URL rewriting is not what you need. Why? Because mapping an external URL to some internal URL / alias is not going to help you serve the file.
What you need is a way to have an external URL process a request and return the file in question. Luckily Drupal 7 makes this pretty easy to do.
1.) Define a menu mapping in hook_menu()
function MODULE_menu() {
$items = array();
$items['pdf'] = array(
'title' => 'Map PDF',
'page callback' => 'MODULE_show_pdf',
'access callback' => TRUE,
'description' => 'TBD',
'type' => MENU_CALLBACK,
);
return ($items);
}
2.) Define your callback function
function MODULE_show_pdf($somehtmlfile = '') {
$stream_wrapper_uri = 'public://pdf/' . $somehtmlfile . '.pdf';
$stream_wrapper = file_create_url($stream_wrapper_uri);
$stream_headers = array(
'Content-Type' => file_get_mimetype($stream_wrapper_uri),
'Content-Length' => filesize($stream_wrapper_uri),
'Pragma' => 'no-cache',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
'Accept-Ranges' => 'bytes'
);
file_transfer($stream_wrapper_uri, $stream_headers);
}
Some things to note:
There is no need to explicitly define somehtmlfile param in the menu. This allows you more flexibility in that you can simply define whatever params you would like this external URL to support simply by adjusting the params in the callback function.
When public stream wrapper dirs/files are sub-dir of: sites/default/files
It is assumed that although you have somehtmlfile in the URL that you really want to stream somehtmlfile.pdf (if you want to stream somehtmlfile.html then simply adjust the hard coded '.pdf' suffix)
file_transfer calls drupal_exit() as its last step which essentially ends request processing.
Make sure to flush your cache otherwise the above will not work as menu entries are cached and the external URL will fail to be found