Wordpress - using php to redirect to a different page - wordpress

I am trying to create a form that when submitted, adds data to a mysql database and redirects to a success page.
I added this code to the bottom of my functions.php file:
add_action('init', 'form_submit');
function form_submit(){
global $wpdb;
if(isset($_POST['form_sub']))
{
$name= $_POST['name'];
$age= $_POST['age'];
$wpdb->insert(
'mytable',
array(
'name' => $name,
'age' => $age
),
array(
'%s',
'%s'
)
);
header("Location: http://www.google.com");
}
}
and added a form to my page that calls this function when submit is clicked.
When I try this, the data is added to the mysql database, but the redirect doesn't seem to work. Instead of going to google.com, it adds this to the top of the page:
Object Moved
This document may be found here
Where here is a link to google.com. Any idea on how to fix this? Thank you!

You are probably sending the redirect header too late (after the page is rendered).
Check this out: http://shibashake.com/wordpress-theme/wordpress-page-redirect
You can try to put out a javascript redirect if you can't get the header() or wp_redirect() out early enough.

Related

WooCommerce - Redirecting a User after They've Reset Their Password

I'm setting up a WooCommerce shop for a client and he has requested that the user be redirected back to the login form after the reset password form has been submitted.
Is this possible? Which function controls this?
Thanks in advance.
A much cleaner solution is to redirect using the woocommerce_customer_reset_password action:
function woocommerce_new_pass_redirect( $user ) {
wp_redirect( get_permalink(woocommerce_get_page_id('myaccount')));
exit;
}
add_action( 'woocommerce_customer_reset_password', 'woocommerce_new_pass_redirect' );
This code can be placed in functions.php.
Someone asked me about doing this and I think I'm going to talk them out of it, but here's what I came up with so that you could see the message and then be redirected in 5 seconds. Add a message/link to let them know you'll be redirecting. Plus, this will last through a WooCommerce upgrade if you add the template file to your theme
Add the form-lost-password.php template file to your theme and add the following code:
if ( $_GET[reset] = true && ( 'lost_password' == $args['form']) ) {
$my_account_url = get_site_url() . '/my-account';
echo "<script>window.setTimeout(function(){window.location.replace('$my_account_url')}, 5000)</script>";
}
If you dont want the time delay get rid of the window.setTimeout() and just use window.location.replace('$my_account_url').
I'm just facing the same question. I think the function that controls this is in the file woocommerce/includes/class-wc-form-handler.php. I changed the following line:
wp_redirect( add_query_arg( 'reset', 'true', remove_query_arg( array( 'key', 'login' ) ) ) );
with
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
With that change, you get redirected to the my-account page, where the customer can log in, but of course there are two problems doing that:
The customer gets no message the the password recovery was successful
Doing a WooCommerce update, the file gets overwritten with the original file
Would be great, if someone could come up with a "update-secure" solution.
Best regards
Christoph

Custom 'Edit Profile' Location in Wordpress

What I am trying to do is have the Edit Profile option in the Admin bar at the top of a wordpress page go to another url.
Basically instead of it going to the /wp-admin directory
I am trying to get it to go the the /editprofile.php page.
You can use the filter edit_profile_url:
add_filter( 'edit_profile_url', 'custom_profile_link_so_19216787', 10, 3 );
function custom_profile_link_so_19216787( $url, $user, $scheme )
{
return site_url( 'editprofile' );
}
I suppose that /editprofile.php is a page using this template and that its address is example.com/editprofile. If that's not the case, put a full URL instead of site_url($slug).
Related: Where to put my code: plugin or functions.php?

"login" in permalink redirects to wp-login.php wp3.6

I'm trying to create a custom login page for a custom user page, but I can't get wordpress to stop redirecting the word login to wp-login.php.
Setup is pretty simple, I'm adding a rule that looks like this. account/login/?$ that yields to index.php?account=login. I also setup my rewrite tag for that custom query var. I have a couple other rules setup and they all work properly.
When I use monkey man's rewrite analyzer everything points properly, but on the front end any time I enter login into the url it redirects me to wp-login.php. It doesn't matter what segment it is entered at. so /login, /account/login, /dogs/bulldogs/login.... all redirect to wp-login.php
As far as I can tell there aren't any other rewrite rules containing login. So i'm at a loss.
As a last piece of info, i'm working in wp 3.6
Any advise or ideas would be greatly appreciated.
I'm facing the exact same problem. Google wasn't being very helpful, so I had the need to dig in. :)
Right on top of the wp-includes/template-loader.php file, you find this little snippet of code:
if ( defined('WP_USE_THEMES') && WP_USE_THEMES )
do_action('template_redirect');
If you inspect what functions are hooked to that action at that time, you'll find a reference to the wp_redirect_admin_locations function (both the hook and the function are declared on the /wp-includes/canonical.php file - line 526).
Within the wp_redirect_admin_locations function, you'll find this:
$logins = array(
home_url( 'wp-login.php', 'relative' ),
home_url( 'login', 'relative' ),
site_url( 'login', 'relative' ),
);
if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
wp_redirect( site_url( 'wp-login.php', 'login' ) );
exit;
}
An easy solution for the problem, would be to hook a function of yours to the *wp_redirect* filter. Something like this (within your functions.php):
add_filter('wp_redirect', 'yourprefix_filter_login_redirect', 10, 2);
function yourprefix_filter_login_redirect($location, $status)
{
$uri = trim($_SERVER['REQUEST_URI'], '/');
if ($uri === 'login') {
return false;
}
return $location;
}
So... The *wp_redirect_admin_locations* terminates the script after trying to preform the redirect, so the solution above won't work.
The only other solution I'm seeing right now is to bypass the function all together which kind of sucks. All the other redirects defined within that function, will be lost all together...
Anyway, what I've ended up using:
add_action(
'init',
function() {
remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
}
);
Building on top of Diogos great answer, you can even disable the /login redirect without losing the other redirects. I just posted this solution on WPSE: Disable "/login" redirect

Wordpress page slug conflicts with media library item

I created a new page which is assigned a custom template. When I visit that page's url I see what appears to be the default page layout (not my template) and the admin toolbar shows options pertaining to media (ex: Edit media).
After some head scratching, I deduced that somehow that url must point to a media item. I edited the page slug and "bingo" the actual page appears just as expected. When I visit the original url (from the first slug) I see the same media item.
Bottom line: It appears that coincidentally the page and the media item share the same name, and this was somehow causing WP's wires to get crossed.
My question: Can someone help me understand how/why this happens? Does wordpress create magic permalinks to everything in the media library (other than their location in wp-content/uploads/...)?
Note: The media item was uploaded normally into the media library (not FTP into the root directory, etc)
Yes, in WordPress you cannot have duplicate slugs/categories/taxonomies/tags. So if your theme allows for media files and permalinks to have their own page and the slug is the same as another one, it will usually append a number to it because the database does not like it.
media slug "example"
page slug "example" will not work since that slug exists already , if done in the admin it will automatically change the slug to "example-1".
I just had this problem and fixed it like this:
$post_s=get_posts('posts_per_page=-1');
foreach($post_s as $p){
$atts = get_posts('post_type=attachment&name='.$p->post_name.'&posts_per_page=-1&post_status=inherit');
foreach($atts as $att){
echo 'found!! '.$p->post_name;
// Update post 37
$my_post = array(
'ID' => $atts->ID,
'post_name' => $att->post_name.'-image'
);
// Update the post into the database
wp_update_post( $my_post );
}
}
This is a late answer, but I wanted to give a cleaner version of the answer that alesub gave.
function wp21418_append_to_post_name() {
// Checks to see if the option images_updated has been set or has a value of true.
if ( get_option( 'images_updated' ) === 'true' ) :
return;
endif;
// get all attachment posts.
$attachments = get_posts([
'post_type' => 'attachment',
'post_status' => 'inherit',
'name' => $p->slug,
'posts_per_page' => -1,
]);
// For each attachment, loop and update the post_name
foreach($attachments as $p){
$attachment = array(
'ID' => $p->ID,
'post_name' => $p->post_name.'-image'
);
// Update the post into the database
wp_update_post( $attachment );
}
// Once everything is looped, add the option to the database.
add_option( 'images_updated', 'true' );
}
add_action( 'after_setup_theme', 'wp21418_append_to_post_name' );
This function runs on an action hook right after the theme has setup. The first line checks to see if there is an option in the database images_updated. If that option exists, we bail on the function and it doesn't do any processing. Otherwise, if the option does not exist, it runs the function and sets the option at the very end.
This makes it so it will only run once. You don't have to remove the function after refresh. If you want to run it again, you can simply remove the if statement at the top. As a caveat: doing this will add another -image at the end of post_names even if they have -image already (e.g. -image-image)
There could be more file name checking for that situation. Will update the answer with that if someone really needs it.
I tried one of the suggested solutions, and ended up having attachment pages like /bob-image-image-image-image/.
I'd suggest that instead of using alesub or disinfor's code blindly, use a better option in the form of "Disable Media Pages" plugin.
https://github.com/joppuyo/disable-media-pages
It automatically sets all attachment slugs to an unique id, and there is the option to mangle any existing attachment slugs so they won't cause any issues in the future.

Can I set a form_state redirect with a destination parameter in form submission?

I've built a "Quick Links" drop-down select box for my Drupal-based web app. The select box is implemented as a standard Drupal form, which appears on all pages in the site. When the user selects an option and submits the form, I want to redirect the user to the appropriate page, but have them return to the original page when they have finished.
e.g. user is on node/5, and selects a quick link which takes them to another form. When they have completed this form, I want them to be returned to node/5.
I have achieved this in other parts of the system using drupal_get_destination to append the ?destination=node/5 querystring parameter to the destination form, and this works nicely. However, in my quicklinks form submit handler, I'm setting form_state['redirect'] to set the target page, and if I add the ?destination= parameter it gets URL-encoded, and so isn't picked up when my target form is submitted.
function dh_seo_quick_links_form_submit($form, &$form_state) {
$form_state['redirect'] = $form['#link_targets'][$link].'?'.drupal_get_destination();
}
Have I gone about this completely the wrong way? Is this redirection even possible? Could I do something clever with the $_REQUEST variables directly?
Here's a nice solution for Drupal 7 which allows you to use relative paths:
$form_state['redirect'] = array(
'node/[node number]',
array(
'query' => array(
'variable_name' => 'value',
),
'fragment' => 'hash_fragment'
),
);
This code creates the following url:
..?node/[node number]&variable_name=value#hash_fragment
This allows you a lot of flexibility while taking advantage of the Drupal framework.
OK - I've worked it out. It seems that if you set a relative URL in the $form_state['redirect'] it gets URL-encoded, but an absolute URL will not:
$form_state['redirect'] = 'myform?'.drupal_get_destination();
redirects to myform%3Fdestination%3Dnode%252F5
global $base_url;
$form_state['redirect'] = $base_url.'/myform?'.drupal_get_destination();
redirects to myform?destination=node/5, which is what I wanted.
You can supply an array for $form_state['redirect'] and put the destination query as second parameter. For example:
$form_state['redirect'] = array(
'myform', array('query' => drupal_get_destination()),
);
You're answer worked for me Mark. Thanks!
You can do it this way also:
global $base_url;
$form['#redirect'] = $base_url .'/'. $form['#redirect'] .'?test=hello';
Finally I got this solution:
global $base_url;
$form_state['redirect'] = $base_url . '/myform?' . drupal_get_destination();

Resources