how to set a header.php redirect in wordpress? - wordpress

i just want to ask how to set a redirect url in header.php? it seems I am having a redirect loop.
<?php
header("Location: http://www.sitename.com/category/videos/");
?>

You should use wp_redirect instead of the php header() function. Hook the call to the wp_loaded to prevent the "headers already sent"-error.
Example: (add to functions.php)
add_action ('wp_loaded', 'my_redirect_function');
function my_redirect_function() {
// define when the redirect should be made
// example: only redirect for the page with slug "about-me"
if(!is_page( 'about-me' )){
return;
}
// define your url here
$url = 'http://google.com';
wp_redirect($url);
exit;
}

You Can put you code directly in header.php
or
if($_SERVER['REQUEST_URI']=="your URL When you want redirect")
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.sitename.com/category/videos/");
exit;
}

Place the following HTML redirect code in a WordPress Page or Post:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourURL.com/index.htm">
This code tells your browser to auto-refresh the current Web page. When it does, it loads the URL in the "url" string. The "0" attribute associated to "content" determines how many seconds before the redirect takes place. The attributes associated with "content" and "url" are the only ones you should alter.

Related

Pretty URLs for named anchors on WordPress page

I'm working on a WordPress site in which the services page is one long page with anchors for each section.
www.mysite.com/services/#human-resources jumps to the Human Resources section as expected.
Is there any way in the .htaccess file to make a url such as www.mysite.com/services/human-resources/ jump directly to the anchor?
I've tried several different rewrite rules, the closest I've gotten is:
RewriteRule services/human-resources services/#human-resources [NE,L,R=301]
But this shows the # in the url which defeats the purpose. Removing the R=301 just causes it to do nothing.
Thanks.
I have been working on a similar site and this is how I solved that issue.
First add this action to functions.php, witch redirects the user to the proper anchor on the page.
function url_handler(){
$path = substr($_SERVER['REQUEST_URI'], 1); //get the path minus the '/'
if( is_page() || is_404() ){ // do you own conditional tags
wp_redirect( home_url("#" . $path) ); //redirect to the #anchor
exit();
}
}
add_action( 'template_redirect', 'url_handler' );
Next you need to make a javascirpt handler witch gets the url of the page (the url with the anchor) and make the proper event to scroll to that section. Something like this:
var hash = window.location.hash;
hash = hash.replace('#','');
hash = hash.replace('/','');
/* do stuff here */
Hope this helps

How to have a PHP redirect on WordPress?

I want to have a URL like this www.example.com/redirect.php?www.google.com in my WordPress website:
Every time I change www.google.com to another URL it will redirect to that page. I also want to have some urls to show as example.com/redirect.php?www.google.com but redirects to affiliate.google.com.
If there is any plugin please mention that.
create redirect.php and upload it on your wordpress root folder
the and this file
<?php
$query = $_SERVER['QUERY_STRING'];
if (!preg_match("~^(?:f|ht)tps?://~i", $query)) {
$url = "http://" . $query;
header('Location: '.$url);
}
<?php
$links=array
(
'www.google.com'=>'http://google.com',
'google.co'=>'http://google.co.uk',
'google.af'=>'http://google.com.af'
// etc... last line without comma
);
if (array_key_exists($_GET[id],$links))
{
header("HTTP/1.1 301 Moved Permanently");
header("Location:" . $links[$_GET[id]]);
}
else {echo "bad url";}
exit(); ?>
here is the code I was able to find, create a file called redirect.php or other type paste the code and then when you write www.yoursite.com/redirect.php?id=google.co it will redirect to google.co.uk

How to redirect based on URL

I am using Wp Store Locator plugin.And I have Modified it according my need.But I am stuck in redirection.
Basically this plugin works with short code.So at the time of listing my URL is like that : localhost/wordpress/?page_id=391
Now there is one link which is redirects me to http://localhost/wordpress/?wpsl_id=3
Here is code for that :
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
$dpath = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ;
$templatefilename = 'single-store.php';
if (strpos($dpath,'wpsl_id') !== false){
$return_template = get_template_directory() .'/' .$templatefilename ;
//$nurl = get_permalink($return_template);
//echo $return_template; exit;
wp_redirect($return_template);
exit;
}
}
This code is not redirecting me to any-where.It stays on the same page localhost/wordpress/?page_id=391 What might be the issue?
Anyone can suggest me how can I direct to URL when it detects wpsl_id in the URL ? I want to redirect them to plugin's template folder where I have added a file for that.
EDITED :
I have added above code in the plugin file just above the short code. Hope I has nothing to do with this issue :)
The template name looks fine. You just need to register the template before you call it.
Add this filter so this will execute before your action:
add_filter( 'template_include', 'single-store' );

Redirect outside WP after Login

I have http://mysite.com/admin.php
There I check wether the user is admin or not.
In second case I send the user to the wp-login page like this:
blog.mysite.com/wp-login.php?redirect_to=http%3A%2F%2Fmysite.com/admin.php
I expect redirect back for admin.php but wordpress always send me to wp-admin control panel.
I have researched.
When the dest. host is not in filter
allowed_redirect_hosts
WP just redirect the user to wp-admin.
How can I add more hosts to the filter?
If I put this example from the WP Codex on functions.php it stops working.
(http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts)
add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$content[] = 'blog.example.com';
$content[] = 'codex.example.com';
// wrong: $content[] = 'http://codex.example.com';
return $content;
}
Add the following in your functions.php:
function my_allowed_redirect_hosts($allowed_host) {
$allowed_host[] = 'anothersite.com';
$allowed_host[] = 'www.someotherwebsite.com';
return $allowed_host;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
Replace anothersite.com and add new values accordingly.
If you're trying to redirect users in a normal page, you can make use of Wordpress's wp_redirect() function:
<?php
wp_redirect( $location, $status );
exit;
?>
Documentation: wp_redirect()
Hope this helps!
Finally it works!
What I was doing wrong is putting the code in the WP functions.php file, and not in my custom theme functions.php file.
Thanks all!

WordPress 404 header for custom 404 page

I built a simple plugin that I can use to quickly create a "pretty" custom 404 page that resides in the WordPress pages area where it can be easily edited. I recently discovered that my plugin is not sending the proper 404 not found header when Google webmaster tools informed me that I've had an increase in soft 404s. As you can see in the code below, I first send the 404 Not found header and then call a 301 permanent redirect.
Conceptually I would think this should work (and I've seen this used in other 404 plugins), but it doesn't. The only way I could get the 404 header to show up is if I sent the header after the location header, but that kind of defeats the purpose as it shows a blank page. I'm not really sure how to best make this work in a WordPress plugin, and I want to make sure I can some how make this work within WordPress's page structure.
Here's the function:
if ($wp_query->is_404) {
$page_title = $this->options['404_page_title'];
$redirect_404_url = esc_url(get_permalink(get_page_by_title($page_title)));
header("HTTP/1.0 404 Not Found");
header("HTTP/1.1 301 Moved Permanently");
header("Location:" . $redirect_404_url);
die;
}
Instead of redirecting to your 404 page, send the 404 header and simply print out the 404 page content.
This is untested, but the idea is something like this...
<?php
if ($wp_query->is_404) {
header("HTTP/1.0 404 Not Found");
$page_title = $this->options['404_page_title'];
// Override the current post to the 404 page
$GLOBALS['post'] = get_page_by_title($page_title);
}
I finally got it! Thanks Brady for helping point me in the right direction!
First I set up a function to redirect to the page created by the plugin on a 404 error. Then, I created another function that adds a 404 header status to the page if the page title matches the page title of the 404 page created by the plugin. Finally, I hooked the functions into my constructor. Piece of cake. :)
404 Redirect Function:
public function redirect_404() {
global $options, $wp_query;
if ($wp_query->is_404) {
$page_title = $this->options['404_page_title'];
$redirect_404_url = esc_url(get_permalink(get_page_by_title($page_title)));
wp_redirect($redirect_404_url);
add_action('wp', array(&$this, 'redirection'));
exit();
}
}
Apply 404 Header to Page Function:
public function is_page_function() {
global $options;
$page_title = $this->options['404_page_title'];
if (is_page($page_title)) {
header("Status: 404 Not Found");
}
}
Actions:
add_action('template_redirect', array( &$this, 'redirect_404' ), 0);
add_action('template_redirect', array( &$this, 'is_page_function'), 0);

Resources