For SEO purposes I need to be sure all URLs are saved as lowercase.
How can I force the SilverStripe admin to save the URL in lowercase even if the user types the permalink in uppercase?
You can do this in your Page's onBeforeWrite method:
protected function onBeforeWrite() {
parent::onBeforeWrite(); //this is important!
$this->URLSegment = strtolower($this->URLSegment);
}
See API docs
Related
I need to store current URL in transient, so I can access it for later use (when I need to link back to that page from search page).
Code:
if(!is_page_template('search.php')) {
set_transient( 'last_url', $current_url, 60*60 );
}
So this code should save current url of current page, until we are on search page.
However, once I click on the search page, 'last_url' will become domain.tld/search. I have no idea why is that happening when I explicitly have the rule if(!is_page_template('search.php'))
However, my temporary solution is to check if there is also word search in URL, and if there isn't then create transient, like:
if(!is_page_template('search.php')) {
if(stripos($current_url, 'search') === false) {
set_transient( 'shop_last_url', $current_url, 60*60 );
}
}
While this solution works, its the bad one since search page have different slug - for example if there are several languages...
I have also tried to use cookies and sessions without any luck.
If your theme doesn't uses default WordPress queries ($wp_query) then the functions such as is_page_template,get_page_template_slug would not work properly.
You can see it in corresponding Core code here.
So, for your current case you can use global template variable instead.
if (basename($GLOBALS["template"])=='search.php'){
set_transient( 'last_url', $current_url, 60*60 );
}
or
if (basename(get_page_template())=='search.php'){
set_transient( 'last_url', $current_url, 60*60 );
}
I am building a Silverstripe site that allow users to sign up to something. I have a few pages that in the CMS I set the page visibility to "Logged-in users" this is great but the default action is to redirect to /Security/Login. Is there a simple way of change the redirect for normal pages to goto etc /Account/Login and leave the default /Security/Login for CMS users?
Thanks
With the help of another plugin, I used this bit of code
public function onBeforeSecurityLogin()
{
$backUrl = $this->owner->getRequest()->getVar('BackURL');
if (!strstr($backUrl, '/admin/')) {
if (Controller::curr()->class != 'Account') {
$link = 'account/login' . '?BackURL=' . urlencode($backUrl);
return $this->owner->redirect($link);
}
}
}
And I also extended the Security Class to create my own handler and form for logins
You can set ?BackURL=/Account/Login in your links, or in the Session.
Alternatively set the config variable, E.g:
Security:
login_url: Account/Login
How to redirect user to a particular page after the user is successfully registered into the site?
Here drupal_goto() does not work. We need to work with the form redirection. How can this be achieved?
// include the Url namespace in your custom module
use Drupal\Core\Url;
// Add CUSTOM SUBMIT HANDLER FOR REGISTRATION
$form['actions']['submit']['#submit'][] = '__tmp_registration_submit';
function __tmp_registration_submit(&$form, FormStateInterface $form_state) {
// set relative internal path
$redirect_path = "\user\uid\payment-history";
$url = url::fromUserInput($redirect_path);
// set redirect
$form_state->setRedirectUrl($url);
}
Maybe a bit heavy, but you could also use the Rules module.
I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.
This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.
I have a site www.example.com and a wordpress blog at www.example.com/blog.
I want my user to login at www.example.com and $_SESSION['USERNAME'] is passed to www.example.com/blog.
I just want to know, how can i automatically login the end user to wordpress blog, once they login to my main site.
For now i am passing $_SESSION['USERNAME'] and using a plugin external database login, which I have linked to my main site database.
Any function that is used to login to wordpress using session username will be helpful.
Wordpress don't use $_SESSIONyou need to code a plugin or to add some code to your functions.php to do this - cf http://www.frank-verhoeven.com/using-session-in-wordpress/
You should add something like this :
function init_sessions() {
if (!session_id()) {
session_start();
}
}
add_action('init', 'init_sessions');
NB: there is also a plugin to manage Sessions with Wordpress, maybe you can use/hack this http://wordpress.org/plugins/wp-session-manager/
I have been reading on this. Here is what I have found as I am in the same issue. In your wp-config.php file at the top add:
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}
HOWEVER, " The data stored in the session doesn’t go away when the user logs out or logs into a different account. For that you need to destroy the session. ..." (exerpt from this nice write up). It explains you also need to:
function myEndSession() {
session_destroy ();
}
if you can get wp user_id you could do something like this:
function init_sessions() {
if (user_logged_on_site) {
#session_start();
$user_id = $_SESSION['wp_user_id']; //this has to be set when the user logs on your website
wp_set_auth_cookie( $user_id); //log in the user on wordpress
}
}
add_action('init', 'init_sessions');
Documentation on wp_set_auth_cookie http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie