Redirect after profile update, language specific - php

The profile-update URLs for User Profiles are language specific: .../nl/profile.php and .../en/profile.php. When users click "Update Profile" I can redirect them to any URL. But now I want to test the current URL to see if there's /nl/ in there, so I can give them a redirect to their own language. I use the code below but the result of the 'if statement' is always false. I can see that: when I enter another url there, it picks up that one. So the code seems to work, just the test fails. Any ideas what I'm doing wrong?
add_action( 'profile_update', 'custom_profile_redirect', 12 );
function custom_profile_redirect() {
if(strpos("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}", '/nl/') === false ) {
wp_redirect( trailingslashit( home_url() . '/en' ) );
} else {
wp_redirect( trailingslashit( home_url() . '/nl' ) );
}
exit;
}
[edit:] Variables I managed to retrieve are all set to 'en' after the profile update, even the plugin's global variable. I now worked around it by adding a LanguagePreference dropdown in the sign up form. In the After-Update-Redirect, I read the usermeta and redirect to their own preference.
There must be better ways to achieve this...

Related

WordPress: Redirecting a drafted post

I'm trying to set up a redirect for drafted posts of the post type "Personnel", as we'd rather show a page with a custom message than a 404 page in this instance. I have added the following to functions.php (adopted from this older thread) which for some reason only works for logged-in users- otherwise, the general 404 page shows. I've also tried adding the redirect to the single-personnel.php template, but it has no effect. I'm wondering how to get this redirect working for all users (logged in or not) and/or if there's a better way to implement the redirect? Thanks for any insight here.
add_action( 'template_redirect', 'inactive_personnel_redirect', 0 );
function inactive_personnel_redirect() {
global $post;
if( ( $post->post_status == 'draft' ) && ( is_singular('personnel') ) ) {
wp_redirect( home_url() . '/about-us/inactive', 301 );
exit;
}
}

How to redirect to another page if referrer url is not a specific url in wordpress?

I'm using WordPress with Elementor, I want a certain page to be accessible only if it comes from a certain url. I saw from other answers in similar questions that I can use this:
add_action( 'template_redirect', 'wpse15677455_redirect' );
function wpse15677455_redirect() {
$value = ('https://mywebsite.com/quotaton/') ;
if (!is_page(555) & wp_get_referer() !== $value ) {
wp_safe_redirect( get_home_url() );
}
};
I tried using this in the function.php of the theme but it returns the error "Unable to communicate with server to check for fatal errors". I tried with all plugins deactivated except elementor but same result.
I tried without the add_action call but, despite not giving errors, it also does nothing. I can't seem to find the right place/way to use this function.
Trying out the code, I believe the problem is that you're missing a single ampersand(&) for the And operator. Also, if is_page is used to check for the "certain page", maybe the not(!) operator isn't necessary...
if (is_page(555) && wp_get_referer() !== $value ) {
function custom_redirects() {
if ( is_front_page() ) {
wp_redirect( home_url( '/dashboard/' ) );
die;
}
if ( is_page('contact') ) {
wp_redirect( home_url( '/new-contact/' ) );
die;
}
}
add_action( 'template_redirect', 'custom_redirects' );
is_page('contact') => 'contact' is page slug.
is_page(150) => '150' is page ID.
If the link that has to be redirected comes from a page, you could use a redirection plugin like this one: https://it.wordpress.org/plugins/page-links-to/
Once activated you just edit the page you want to be redirected inserting the correct link in the page links to field.

Avoiding redirection to the Dashboard in WordPress

I understand that I can use the following code in WordPress to ensure that users who are already logged into my site are not re-directed to the Dashboard when they click the login button on my home page:
<?php
if(is_user_logged_in()){
// redirect to desired page
}
?>
I would be grateful for advice on where I should place this code and which part of it exactly should be overwritten with the URL for the page to which I wish to send logged in users. I should add that the desired page is the same page as I direct users to on logging in.
Many thanks in advance for your kind assistance.
Just take a look here, for more perspective: https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
If you want to land your Users, for example to this page example.com/some-page/ after login, you can use this code:
function mysite_login_redirect($redirect_to, $request, $user) {
if (!isset($_GET['loggedout'])) {
return (isset($user->roles) && is_array($user->roles) && in_array('administrator', $user->roles)) ? admin_url() : site_url('/some-page/');
}
}
add_filter('login_redirect', 'mysite_login_redirect', 10, 3);
The algorithm for doing this is simple:
If the user is an administrator, continue to admin_url()
Otherwise, redirect to, say the site_url('/some-page/')
The login_redirect filter will pass three arguments to the function:
$redirect_to
$request
$user
We’re primarily concerned with the third as we can take a look at its roles attribute to determine if it contains the administrator value.
And with that, we are now redirecting non-admin users to the specific page rather then the dashboard.
site_url('/some-page/') will transform to example.com/some-page/
Hope it will help you :)
As I undestood about your question, you are trying to prevent users to visualize the dashboard of WordPress. You can prevent non-admin users to go to the Dashboard on the admin init hook.
add_action( "admin_init", "prevent_dashboard_non_admin" );
function prevent_dashboard_non_admin() {
if ( ! current_user_can('manage_options') && ! defined( 'DOING_AJAX' ) ) {
wp_redirect( home_url() ); // You can change that home_url() to any page URL you are tring to redirect
}
}
You can put this code inside functions.php
Note: This code has not been tested, please leave a comment and I will create the update if you have any issues with it.
UPDATE
function my_is_loggin_page() {
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
if( is_user_logged_in() ) {
wp_redirect( "yourpage" );
}
}
}
add_action( "init", "my_is_loggin_page", 12 );

Wordpress - create route to action in plugin

I want to create a route in my wordpress plugin that isn't linked to a page but to an action that sends an email. So i would send a get request like this
example.com/send/email?email=test#test.co.uk
and this would link to an action with the email as a parameter. I'm very new to Wordpress so forgive me if this is a stupid question but I'm really struggling to achieve this or evebn find a good starting point, can anyone help?
A good option in your case would be to use a rewrite endpoint. A rewrite endpoint allows you to add extra query parameters to certain URLs. For instance you can add a gallery endpoint to all posts, that could render a different template showing all images for the given post. More information about add_rewrite_endpoint() can be seen in the Codex.
Below is some code that adds a send endpoint to EP_ROOT(the home page of the site). Note that you'll have to go to Settings > Permalinks after adding this code in order for the rewrite endpoint to start working.
Once we have the rewrite endpoint in place, we hook to the template_redirect action in order to check for the presence of the send query var. If send is not present, then we do nothing.
If send is present, but empty(like for instance if you load the page http://example.com/send/), then we redirect to the home page.
Otherwise we split send into multiple parts at every / and assign that to the $send_parts variable. We then use a switch statement to see what the $send_action(the first part after /send/) and act accordingly.
Right now we're only checking for the email action(if it's not email, we redirect to the home page again). We check if there's an actual email($send_parts[1]) and whether it's a valid email(I have to note that is_email() is not RFC compliant and it might reject valid email addresses, so use with caution). If it's a valid email, we use wp_mail() to send the email, otherwise we redirect to the home page.
Now, since I don't know how you're planning to implement this my code doesn't cover things like authentication(who can send emails - if it's everyone, I can abuse your site and spam users and get your mail server blacklisted - bad :( ), generation of the email Subject and Message(is it going to be dynamic via $_POST variables, is it going to be pre-defined, etc.). Those are specifics that you will have to implement on your own.
Once the code below is placed in an appropriate file(a .php file that gets loaded in the current theme, or an active plugin's file) and you regenerate your Rewrite Rules(by going to Settings > Permalinks), you can go to http://example.com/send/email/your.email#example.com/ and you should receive an email with a subject "Hello" and a message "This is a message".
function so_34002145_add_email_endpoint() {
add_rewrite_endpoint( 'send', EP_ROOT );
}
add_action( 'init', 'so_34002145_add_email_endpoint', 10 );
function so_34002145_handle_send_email() {
$send = get_query_var( 'send', null );
// This is not a /send/ URL
if ( is_null( $send ) ) {
return;
}
// We're missing an action, the URL is /send/
// Take the user to the home page, since this is an incomplete request.
if ( empty( $send ) ) {
wp_redirect( home_url( '/' ) );
exit;
}
$send_parts = explode( '/', $send );
$send_action = $send_parts[0];
switch ( $send_action ) {
case 'email':
$email = ! empty( $send_parts[1] ) ? $send_parts[1] : false;
if ( ! $email || ! is_email( $email ) ) {
// A missing or invalid email address, abort
wp_redirect( home_url( '/' ) );
exit;
}
wp_mail( $email, 'Hello', 'This is a message' );
break;
default:
// This is an unknown action, send to the home page
wp_redirect( home_url( '/' ) );
exit;
break;
}
}
add_action( 'template_redirect', 'so_34002145_handle_send_email', 10 );

mod_rewriting with name from database

I have a page with the the URL format:
www.site.com/events/event.php?id=1&cat_id=4
which I'd like to rewrite to something more SEO friendly, namely
www.site.com/events/name-of-event
(I don't need the category name in my URL, just for the page to create dynamic breadcrumb links).
I have the event name in my MySQL database, and I'd like to use that in the URL. I've tried to adapt the code in this answer but without success. Can anyone help?
EDIT:
RewriteRule ^events/([^/]+)$ event.php?event_name=$1 [L,QSA] this is what I initially tried (the cat_id isn't essential to the URL as the page checks and retrieves a default if none is passed).
For backward compatibility, you probably want to redirect events/event.php?id=123 to the new events/event_name url. The problem is that, as the person said in your linked answer, there is no real way to connect to the database via Apache, so you'll have to handle the redirection/translation in the file itself.
The rule itself must look something like this:
RewriteRule ^events/([^/]+)/?$ /events/event.php?event_name=$1 [L,QSA]
Then, in your event.php you'll do something like this:
#This is the top line of your file.
if( isset( $_GET['id'] ) ) {
$id = intval( $_GET['id'], 10 );
$event_name = do_database_query_NOAW( $id );
header( 'HTTP/1.1 301 Moved Permanently' );
header( 'Location: /events/' . $event_name );
exit();
}
#code that now uses (the sanitized) event name instead of the sanitized id
if( !isset( $_GET['event_name'] ) || !preg_match( '/[A-Za-z0-9-]+/', $_GET['event_name'] ) ) {
header( 'HTTP/1.1 400 Bad Request' );
exit();
}

Categories