WP: redirect users to a certain category using cookies - wordpress

Basically like cragslist. once you select city on craigslist, nexttime when you go to the site, it redirects you to the city you selected.
What I want to achieve: When a person comes to the site and selects a particular category, the next time they come to the site (returning user) - the page will open up on that category section.
I would think this would be fairly easy to do via setting a cookie when the visitor clicks on the category link (or when the category page loads). When they return the following time, the cookie is read and then the page redirects accordingly.
Unfortunately my knowledge of PHP and cookies is limited, (hence my search for answers) so I need to ask if anyone can help me out!
Anyone have any ideas?
Thanks!

markratledge has a good link, but WordPress has a built in function to redirect users and pass an http status code as well as it's own preferred method of settings cookies.
wp_redirect() Function
Reference
Setting Cookies in WordPress via MTSTS
Give this a shot. I'm not too sure if it works, because I can't test it out right now, but it should point you in the right direction.
function user_cat()
{
//Check to see if our cookie is set
if(isset($_COOKIE['visitorhome']))
{
//Redirect to the link defined in the cookie
wp_redirect($_COOKIE['visitorhome'], 302);
}
else
{
//If it's a category page than get the current URL set the cookie with it.
if(is_category())
{
$user_cat = get_permalink();
setcookie("visitorhome", $user_cat, time()+86400, "/", str_replace('http://www','',get_bloginfo('url')) );
}
}
}
add_action('init', 'user_cat');

Read this article on how to set and get cookies with PHP: http://www.w3schools.com/PHP/php_cookies.asp
Then read this page on PHP header redirecting: http://php.net/manual/en/function.header.php
Put the two together, and you can direct users anywhere!
Good luck.

Related

Redirect "dumb" URL to Author's custom post

I have created a website whereby users register and create their own templated profile pages. The profile pages are automatically created as Custom Posts upon registration, with the user being set as the Author of their specific post (profile).
(Users can only ever have one Custom Post)
I want to redirect a "dumb" URL like www.website.com/my-profile to a user's custom post when they are logged in.
For example, when John Smith visits www.website.com/my-profile he is directed to his profile page: www.website.com/users/john.smith
I have found many PHP solutions going the other way, but I can't seem to find a solution that does what I need. Any help would be greatly appreciated. Thanks!
This may not be the correct answer to the original query, but proved to be a solid workaround:
Instead of redirecting www.website.com/my-profile to www.website.com/users/john.smith every time it is entered in the URL bar, I created a shortcode that could be used when needed throughout the site.
add_shortcode('bt_redirect_user_link', 'bt_redirect_user_link');
function bt_redirect_user_link ($atts) {
// check if user is logged in
if (is_user_logged_in()) {
// get current user object
$current_user = wp_get_current_user();
// get user nickname
$user_nickname = $current_user->data->user_nicename;
// set the link href
$link_href = '/users/' . $user_nickname;
// output the link html
return $link_href;
}
}
Fortunately for me, the www.website.com/my-profile link (which needs to be redirected) is only available on buttons/icons visible to logged in users. This may not be a fully workable solution for websites that need to display the link to logged out users, and I assume IF/ELSE statements would needed to be added in those cases.

Wordpress Form Submit button GETS (or POSTS) form data to external page

We have a wordpress website that does marketing display, but now we want to allow a customer to submit an email and selection to a separate website with a landing page that will handle the backend DB work and finish them in the other website.
Something like,
www.ourmarket.com/getdata (on Submit button click GETS to...)
www.ouradminsite/landingpage.aspx (which processes the data that the use will not see then...)
www.ouradminsite/login.aspx (where the user can now login)
I am not familiar with WP at all, but I was able to create a page with a form that has the textbox/combobox I need.
I thought it would be something simple, but somehow it seems not. I read about AJAX and doing something in functions.php and creating a custom .js file, but when working on the marketing site I find no way to add this type of function in.
My fall back is to have the WP page just have a link to a generic landing page where they enter data, but it would be visually jarring to the customer unless I duplicate the WP site for one page.
Is there an easy way to just tell WP to redirect to an external page with a GET?
UPDATE--------------
I like to think I'm making progress. I found a link that may have given me a good start. I added a function to the functions.php file located in my WP theme. It starts like this:
add_action("gform_post_submission_4", "set_post_content", 10, 2);
function set_post_content($entry, $form){
//Gravity Forms has validated the data
//Our Custom Form Submitted via PHP will go here
// Lets get the IDs of the relevant fields and prepare an email message
$message = print_r($entry, true);
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
// Send
mail('myuser#mycompany.com', 'Getting the Gravity Form Field IDs', $message);
**wp_redirect("http://my.hearbuilder.com/hellomoto.aspx",301);**
}
From there I tried to edit the function to do that wp_redirect, just a simple one to start. This is added under the mail statement:
wp_redirect("http://my.hearbuilder.com/hellomoto.aspx",301);
From this link, when I fill out the form I can get the email, but the new page did not display. I added the exit; line and still got the same result, the page seems like it hangs.
The end result is that I need to have the new website landing page display (after it processes the data from the Wordpress form.
What am I still missing?
yes use wp_redirect()
if($_POST):
$textbox=$_POST['textboxname'];
$url= 'url'.'?custom=hello&textbox='.$textbox.'&anothervalue='.$anothervalue;
wp_redirect($url);
exit;
endif;
you can easily add the variables to the string as needed. The easiest way to control the url properly is to post the information to the same page , catch the post and redirect (place before get_header call or any output has started)
The other way is php Curl which is more difficult esp when dealing with .asp pages, but if you have access to the other server it makes figuring it out easier!

Wordpress: Incomplete URL redirects to actual page

Strange behavior for a Wordpress site. When I enter an incomplete URL, instead of getting a 404, I'm getting redirected to a page whose URL starts with the incomplete entry.
For example, when I enter this in my browser:
http://www.launchmoxie.com/jv/timeless
I'm redirected to:
http://www.launchmoxie.com/jv/timelessrhythm/timeless-rhythm-optin-confirmation/
There are several pages that begin with the initial URL, but I'd prefer that the user be given a 404, or I'd be okay with being able to set which of the pages gets served.
This behavior occurs for other pages with similar structures.
I'm pretty mystified. Any help/suggestions would be appreciated.
This is standard WordPress behaviour, and is part of the URL canonicalisation process—it's in redirect_canonical. There's a ticket to make just this auto-completion bit override-able, but it's not made it into a release yet.
In the meantime, there's a workaround suggested in that ticket:
function remove_redirect_guess_404_permalink( $redirect_url ) {
if ( is_404() )
return false;
return $redirect_url;
}
add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );
...which a helpful soul has also made into a plugin.

WordPress not recognizing user is logged in

I want ONLY users who are logged in to view my WordPress site and the code I'm using is:
add_action('template_redirect', 'admin_redirect');
function admin_redirect() {
if ( !is_user_logged_in()) {
auth_redirect();
}
}
PROMLEM: if I send a hyperlink every time the user clicks it they are asked to sign in again.
Can you not just put
auth_redirect();
into your functions.php file?
This should redirect users who aren't logged in to the log in page but remember which page they were trying to access. Then when they log in it should redirect them to their destination page.
Is this not working?
From my understanding of auth_redirect(), you shouldn't have to place that check around it. The function actually handles that check itself as shown in the Codex
Also, I'd recommend moving this function into your header. By simply calling auth_redirect() in your header (which should be called on each and every page anyways), you can check if the user is logged in. If not, they should be bounced to the wp-login page.
Edit:
add_action( 'template_redirect', function() {
is_user_logged_in() || auth_redirect();
});
The folks over at the WordPress Exchange are much better with this sort of stuff. Found this link here.

wordpress blog basepath

im working on a wordpress blog tryin to develop a multilanguage system.
Whenever the user clicks on his language button the lang query parameter is added to the url
Ex. localhost/my-blog?lang=es
Everything works.The point is i have the blog main menu that has links to different sections of the site that are using the wordpress bloginfo('url') :
Ex. contact us
And whenever the user choose it's language at the home-page and then clicks on "contact us"
he receive this wrong link:
localhost/my-blog?lang=es/contact-us
which wordpress function you use guys for this kind of things?
thanks
Luca
I think this type of url query will get you into trouble in the future, why not conform to using either a session value or base cookie to store the users choice,
then that way you can simply add some code to your functions.php file to read the session value or cookie, and return the translation type?
there is also this plugin xili-language
ie: functions.php
// START THE SESSION
function start_session(){
session_start();
}
add_action('init', 'start_session', 1);
function set_lang_pref($lang_pref){
if(isset($_GET['lang']) && ($_GET['lang']!=''){
$setlang = $_GET['lang'];
switch($setlang){
case "es" :
$lang = "es_ES";
$_SESSION['selectedlanguage']=$lang;
break;
}
}else{
return false;
}
}
the code is really rough, but you get the idea..?
store the users choice,
check for that choice,
if its been set then use that value as the language pref on the site..?
else just revert back to the default..

Resources