I am trying to build a filtering functionality for a Wordpress website using add_rewrite_rules. I have the following link example
example.com/offers-type-all-inclusive-from-germany-transportation-airplane.html
Using the following rewrite rule:
add_rewrite_rule('^offers-type-?([a-z0-9-]+)?-from-?([a-z-]+)?-transportation-?([a-z0-9-]+)?.html','index.php?filters=offers&type=$matches[1]&location=$matches[2]&transportation=$matches[3]','top');
I managed to redirect the user to a custom page template with the arguments stored as an array
array(
'type' => 'all-inclusive',
'from' => 'germany',
'transportation' => 'airplane'
);
The problem is that some of the arguments could be optional so that links like
example.com/offers-type-all-inclusive-from-germany.html
example.com/offers-type-all-inclusive-transportation-airplane.html
also need to build the correct filtering array.
Is it even possible to achieve this?
What a tricky regex!
Try this expression
offers-type-([a-z0-9-]*?)(?:-from-([a-z-]*?))?(?:-transportation-([a-z0-9-]*?))?\.html
Here is an online demonstration
http://regex101.com/r/fS4xB7
Related
I have a MediaWiki 1.33.0 website with only one extension → ContactPage, with which I can have a simple contact form.
Using HTMLForms template engine (in which the default form-template for ContactPage is written), I have expanded the default form to include a selection menu.
My problem
Selection list array keys and values of this selection menu are written in English inside LocalSettings.php but my site isn't primarily in the LTR English, rather, it is in the RTL Hebrew and I would like them to appear in my site's native language for end users.
My own code pattern
wfLoadExtension( 'ContactPage' );
$wgContactConfig['default'] = array(
'RecipientUser' => 'Admin', // Must be the name of a valid account which also has a verified e-mail-address added to it.
'SenderName' => 'Contact Form on ' . $wgSitename, // "Contact Form on" needs to be translated
'SenderEmail' => null, // Defaults to $wgPasswordSender, may be changed as required
'RequireDetails' => true, // Either "true" or "false" as required
'IncludeIP' => false, // Either "true" or "false" as required
'MustBeLoggedIn' => false, // Check if the user is logged in before rendering the form
'AdditionalFields' => array(
'omgaselectbox' => [
'class' => 'HTMLSelectField',
'label' => 'Select an option',
'options' => [
'X' => 'X',
'Y' => 'Y',
'Z' => 'Z',
],
],
),
// Added in MW 1.26
'DisplayFormat' => 'table', // See HTMLForm documentation for available values.
'RLModules' => array(), // Resource loader modules to add to the form display page.
'RLStyleModules' => array(), // Resource loader CSS modules to add to the form display page.
);
possible solutions
1) Writing selection list array keys and values in Hebrew (which might be a bit messy due to LTR-RTL clashings):
'options' => [
'ס' => 'ס',
'ט' => 'ט',
'ז' => 'ז',
],
2) Translating English selection list array keys and values in client side JavaScript by some similar code:
document.getElementById('select').selectedIndex = 0;
document.getElementById('select').value = 'Default';
My desire
I desire an ordinal backend way to do so, and if there is one, than without an extension
In this discussion, a MediaWiki community member recommended using system message transclution but the chapter dealing with it was very unclear to me; I didn't understand what this is about and how can this help in my situation.
My question
What are the possible ways to translate in MediaWiki from "backend", without an extension?
The localisation system is working perfectly fine in the backend (php), as well in the frontend (JavaScript) parts of MediaWiki → staying with it backend is best as it is more minimal.
Assuming you take a backend only approach:
Translation with a predefined string
If your desired translations already exist in MediaWiki (e.g. on another page of form), you can "simply" re-use the key. So, let's assume, your current additional select field definition looks like this:
'Select' => [
'type' => 'select',
'options' => [
'The english message' => 'value'
]
],
Then, you would change it to something like this:
'Select' => [
'type' => 'select',
'options-messages' => [
'the-message-key' => 'test'
]
],
Please consider the changing of options into the options-messages key.
Also: Change the key the-message-key to the message key you want to reuse.
If you know a page where the message/string is used, you can just open that page with the GET option uselang and the value qqx, in order to see the message key. Example: If the string is used on the login page, simply open the login page with https://example.com/wiki/Special:Userlogin?uselang=qqx to show all the message keys used on the page.
However, one warning when doing that: It is mostly discouraged to re-use existing message keys, especially when they're used on other pages. The keys are translated to hundreds of languages with that specific context in mind. That could also mean, that a translation in a specific language does not fit when the string/message is used on the contact page. So I would suggest to use the second option below.
Translation without a predefined string
Usually it will be done by extension which can provide a specific directory where the JSON files with the message key translations are saved. However, as you're "just" customizing an extension, you need a way to put in the translations for your keys.
So, first of all, let's take over the changes from above. Change your select field definition to be something like:
'Select' => [
'type' => 'select',
'options-messages' => [
'my-fancy-key' => 'test'
]
],
Now, two ways to get the key translated:
On-Wiki
By saving the message on-wiki, the messages can also easily being changed simply by editing the respective page in the wiki. In our example, let's translate the key to english and hebrew:
English: Edit the page MediaWiki:My-fancy-key in your wiki and add the desired text.
Hebrew: Edit the page MediaWiki:My-fancy-key/he in your wiki and add the desired text.
As part of the deployed code
We need to register a directory with JSON files for the translations of these messages. We're using the same configuration variable as extensions would use as well, $wgMessagesDirs, even given that we don't create an extension. Add the following line to your LocalSettings.php:
$wgMessagesDirs['ContactPageCustomization'] = __DIR__ . '/customContactPage';
Now, create a directory customContactPage in the root folder of your MediaWiki installation and put in the following file with the following contents:
en.json
{
"my-fancy-key": "Default"
}
If you want to translate to another language, create a new file with the language code you want to translate to. In hebrew it should be he, so let's create a new language file:
he.json
{
"my-fancy-key": "ברירת מחדל"
}
If you then open the contact page, the message key my-fancy-key should be translated to the english Default and the same (at least based on Google Translate) for hebrew. This is a more stable way of adding custom translations, however, you now also need to take care of translating the keys into the languages you want to support on your own as well. If a key is not translated into the selected language of the user, the default language, english, is used.
Is there any way to find page by id in layout file. I'm currently using
$this->fuel->pages->find(5)
But its not working. I'm getting following error message
Plugin module can not be found, maybe you forgot to bind it if it's a
custom plugin ?
You have to use an array to pass parameters in the method and you can use the find_one.
$this->fuel->pages->find_one(array('where' => array('id' => 5)))
I have url http://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms
Where "real-estate-news" is category and "phuket-luxury-property-in-high-demand-ms" is the post name .
when I print $wp_query->query_vars['name']; it gives the post slug and $wp_query->query_vars['category_name'] gives the category slug.
I want to generate a new url like http://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms/xyz
and want to get xyz in query var like
echo $wp_query->query_vars['name']; // print "phuket-luxury-property-in-high-demand-ms"
echo $wp_query->query_vars['category_name']; // print "real-estate-news"
echo $wp_query->query_vars['section']; //print "xyz"
How to add section query var in wordpress please help me
Is this a Custom Post Type? If it is, you have more options than not. Within the register_post_type()'s array you can enter an argument for 'rewrite' to change the slug name and hack in specific rewrite rules for that particular CPT. I have put this project away because of its complexity and if you find an answer I'd love to hear it. Here is my notes on the matter.
register_post_type(array(
//options
'rewrite' => array(
'slug' => 'beach', //a slug used to identify the post type in URLs.
'with_front' => false,
'feed' => true,
'pages' => true
),
dreamdare.org
shibashake.com1
shibashake.com2
Beware of wp.tutsplus very often misinformation is offered there by authors themselves.
wp.tutsplus.com
The way I managed to do this is adding a rewrite rule.
This will look very similar to the one that custom post type creates but also receives a third parameter.
so in your case the rewrite rule would look like this
add_rewrite_rule(
// post-type | post-slug | section
'^real-state-news/([^/]+)(?:/([0-9]+))?/([^/]+)/?$',
// | Here the section is added
'index.php?post_type=real-state-news&name=$matches[1]§ion=$matches[3]',
'top'
);
//You then need to add a tag to it
add_rewrite_tag('%section%','([^&]+)');
// At this point, you should be able to grab that param
get_query_var('section')
+269: [critical] Potential problem: drupal_set_message
http://api.drupal.org/api/function/drupal_set_message/() only accepts
filtered text, be sure all !placeholders for $variables in t
http://api.drupal.org/api/function/t/() are fully sanitized using
check_plain http://api.drupal.org/api/function/check_plain/(),
filter_xss http://api.drupal.org/api/function/filter_xss/() or
similar.
Which pertains to this code:
drupal_set_message(t('Batch complete! View/Download !results', array(
'!results' => filter_xss(l(t('simple results'), file_create_url($filename))),
)), 'info');
What's going wrong?
The method you're using is under the 'DO NOT DO THESE THINGS' portion of Dynamic or static links in translatable strings. You need to change it to one of the approved methods. For reference:
<?php
// DO NOT DO THESE THINGS
$BAD_EXTERNAL_LINK = t('Look at Drupal documentation at !handbook.', array('!handbook' => ''. t('the Drupal Handbooks') .''));
$ANOTHER_BAD_EXTERNAL_LINK = t('Look at Drupal documentation at the Drupal Handbooks.');
$BAD_INTERNAL_LINK = t('To get an overview of your administration options, go to !administer in the main menu.', array('!administer' => l(t('the Administer screen'), 'admin'));
// Do this instead.
$external_link = t('Look at Drupal documentation at the Drupal Handbooks.', array('#drupal-handbook' => 'http://drupal.org/handbooks'));
$internal_link = t('To get an overview of your administration options, go to the Administer screen in the main menu.', array('#administer-page' => url('admin')));
?>
Isn't it much slower concerning development time ?
What are the advantages of of HTML->link ?
Thanks !
It's just a question of whether you want to generate your own URLs and hard-code them, or if you want Cake to do the work for you. For simple urls leading to the homepage of your site using cake may seem slower, but it's actually useful for dynamic urls, for example:
Say you're printing a table of items and you have a link for each item that deletes that item. You can easily create this using:
<?php
echo $this->Html->link(
'Delete',
array('controller' => 'recipes', 'action' => 'delete', $id),
array(),
"Are you sure you wish to delete this recipe?"
);
Notice how using an array specifying the controller and action as a URL allows you to be agnostic of any custom routes. This can have its advantages.
The corresponding way to do it without the HTML helper would be:
Delete
It can also be really useful for constructing URL query strings automatically. For example, you can do this in array format:
<?php
echo $this->Html->link('View image', array(
'controller' => 'images',
'action' => 'view',
1,
'?' => array('height' => 400, 'width' => 500))
);
That then outputs this line of HTML:
View image
It could be a pain to generate that URL manually.
In summary, while it may seem awkward for simple links, the HTML helper definitely has its uses. For further uses, consult the cakePHP book on the HTML helper's link function.