I have a WordPress Child Theme and I use a php file as the template for a particular page.
I am trying to implement an API for a plugin called GeoIP Detection. Please see the PHP file I am using on my site below. The API I am trying to apply is "Redirect depending on country" located here
When I load the script, I am supposed to be redirected to https://www.google.com.sg However, it does not do so.
Thank you.
My PHP
<?php /* Template Name: GeoIPDetectionv3 */
add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
if (is_admin())
return;
// This condition prevents a redirect loop:
// Redirect only if the home page is called. Change this condition to the specific page or URL you need.
if (!is_page(90))
return;
if (!function_exists('geoip_detect2_get_info_from_current_ip'))
return;
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$url = '/germany';
break;
case 'US':
$url = '/usa';
break;
case 'SG':
$url = 'https://www.google.com.sg';
break;
default:
$url = 'https://www.google.com.sg';
}
if ($url) {
wp_redirect(get_current_blog_id(null, $url));
exit;
}
}
Use a single PHP tag, and make sure the last portion of your code is in fact inside a PHP tag. Currently it is not, so it's parsed as plain text.
UPDATE: I've cleaned this up a bit for you and updated the code to reflect your revised question; i.e., following our comments below.
<?php /* Template Name: GeoIPDetectionv3 */
add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
if ( is_admin() ) {
return; // Not applicable.
}
if ( 123 !== get_current_blog_id() ) {
return; // Not on blog ID 123.
}
if ( ! is_page( 90 ) ) {
return; // Not a specific page ID on this blog.
}
if ( ! function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
return;
}
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$redirect_to = '/germany';
break;
case 'US':
$redirect_to = '/usa';
break;
case 'SG':
$redirect_to = 'https://www.google.com.sg';
break;
default:
$redirect_to = 'https://www.google.com.sg';
}
if ( ! empty( $redirect_to ) ) {
if ( stripos( $redirect_to, 'http' ) === 0 ) {
wp_redirect( $redirect_to ); // Full URL.
} else {
wp_redirect( home_url( $redirect_to ) ); // Local /path.
}
exit;
}
}
Related
I can see how the below code is problematic but not why it would result in an redirect loop?
// Redirect user based on Location_cookie
add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
$CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];
switch ($CFCountry){
case "SE":
wp_redirect( 'https://tidanapp.com/shop/sv/' );
exit;
}
}
You are probably looking for something like:
add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
$current_country = ''; // get current country from your multilanguage plugin
$CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];
if(strtolower($current_country) != strtolower($CFCountry)) {
switch ($CFCountry){
case "SE":
wp_redirect( 'https://tidanapp.com/shop/sv/' );
exit;
}
}
}
Or if you cannot get the current country/language from the plugin you are using you could check based on the URL (which is not the nicest option):
add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
$CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];
switch ($CFCountry){
case "SE":
if(strpos($_SERVER['REQUEST_URI'], '/sv/') === false) {
wp_redirect( 'https://tidanapp.com/shop/sv/' );
}
exit;
}
}
I'm learning and I need my index to run my pages instead of the links using php if statements and I'm missing something in my code. It just runs my header over and over again.
my if statements:
<?php
if (!isset($_GET['page']))
{
require('index.php');
}
else if ($_GET['page'] == 'aboutus') {
if (!isset($_GET['action'])) {
require('aboutus.php');
} else if ($_GET['action'] == 'store') {
require('store.php');
} else if ($_GET['action'] == 'contactus') {
require('contactus.php');
}
else
{
require('index.php');
}
}
else
{
require('index_.php');
}
?>
My nav bar:
<ul class="nav">
<li>Home</li>
<li>About</li>
<li>Current Items</li>
<li>Contact Us</li>
</ul>
I know I'm missing something, I'm just not sure what.
If I understood correctly that you wish to load a new file based upon the querystring parameter page ( it is unclear where the querystring parameter action comes from or how it is used ) then perhaps this might help.
<?php
/* set a variable for the querystring parameter "page" */
$page=isset( $_GET['page'] ) ? filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ) : false;
if( !$page ) $file='index';
else {
$pages=array(/* possible files to include */
'aboutus','store','contactus'
);
/* ensure user supplied GET variable is in desired format */
$page = strtolower( trim( pathinfo( $page, PATHINFO_FILENAME ) ) );
/* Find page in array of pages - or use default "index" */
if( in_array( $page, $pages ) )$file = $pages[ array_search( $page, $pages ) ];
else $file = 'index';
}
require sprintf( '%s.php', $file );
?>
The above could be simplified slightly like so:
<?php
/* set a variable for the querystring parameter "page" */
$page=isset( $_GET['page'] ) ? filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ) : false;
$pages=array(/* possible files to include */
'aboutus','store','contactus'
);
/* ensure user supplied GET variable is in desired format */
$page = strtolower( trim( pathinfo( $page, PATHINFO_FILENAME ) ) );
/* Find page in array of pages - or use default "index" */
if( in_array( $page, $pages ) )$file = $pages[ array_search( $page, $pages ) ];
else $file = 'index';
require sprintf( '%s.php', $file );
?>
why is that??? you can implement Single Page Application that url only change ater hashTag like :
link.url/index#about-us
link.url/index#store
and ...
about your problem , after require your code continueus , you must die; after each require to finish loop. just made it a little prettier.
<?php
if (!isset($_GET['page'])) {
require('index.php');die;
}
if ($_GET['page'] != 'aboutus') {
require('index_.php');die;
}
// where is your action variable in your code?????????????
// it's about us
if (!isset($_GET['action'])) {
require('aboutus.php');die;
}
switch ($_GET['action']) {
case 'store':
require('store.php');die;
break;
case 'contactus':
require('contactus.php');die;
break;
default:
die("route is not defined");
break;
}
?>
I was trying to make this with nginx and also with php header redirect in wordpress index.php but I always find myself in an infinite loop.
I just want to redirect iphone user to /category/iphone/ or android user to /category/android/ of my wordpress page.
Is this possible?
Thank you for your help!
Edit:
Maybe I was not totally clear. I only want to redirect a user if he comes to INDEX/MAIN page. He can still browse other categories without redirecting him to iphone/android category.
This if statment will allow you to to detect iOS and mobile device visitors using one of the following:
if( stristr($_SERVER['HTTP_USER_AGENT'],'ipad') ) {
$device = "ipad";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
$device = "iphone";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'blackberry') ) {
$device = "blackberry";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
$device = "android";
}
if( $device ) {
return $device;
} return false; {
return false;
}
}
Even you can use .htaccess file like:
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://www.yoururl.com [R=301]
For in your functions.php file:
add_action( 'template_redirect', 'device_redirect' );
function device_redirect(){
if ( is_front_page() && is_home() ) {
if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
wp_redirect( "http://www.example.com/iphone", 301 );
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
wp_redirect( "http://www.example.com/andriod", 301 );
}
}
}
In case you use a static homepage or blog page you have to change the if.
For example
if ( is_front_page() && is_home() ) {
// Default homepage
}
if ( is_front_page()){
//Static homepage
}
if ( is_home()){
//Blog page
}
We use WordPress and would like to link amp to amp if the linked page has an amp version. We have amp structured like that: test.de/test/amp
Unfortunately this code in my functions.php isnt applying to links hard-coded inside of the post content. What do I have to change, so its working for every internal link:
add_filter( 'post_link', function( $url, $post ) {
static $recursing = false;
if ( $recursing ) {
return $url;
}
$recursing = true;
if ( ! function_exists( 'post_supports_amp' ) || ! post_supports_amp( $post ) ) {
return $url;
}
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
$url = amp_get_permalink( $post->ID );
}
$recursing = false;
return $url;
}, 10, 2 );
At the moment its also applying to the canonical link, which is really bad for seo. How to prevent this?
Add these functions to your theme's 'functions.php'.
/* post link filter */
add_filter( 'post_link', 'change_amp_url', 10, 2 );
function change_amp_url( $url, $postobj ) {
static $recursing = false;
if ( $recursing ) {
return $url;
}
$recursing = true;
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
if ( function_exists( 'post_supports_amp' ) && post_supports_amp( $postobj ) ) {
$url = amp_get_permalink( $postobj->ID );
}
}
$recursing = false;
return $url;
}
/* content link filter */
add_filter( 'the_content', 'change_amp_url_content' );
function change_amp_url_content($content)
{
$dom = new DOMDocument();
$dom->loadHTML($content);
$tags = $dom->getElementsByTagName('a');
foreach ($tags as $tag) {
$link = $tag->getAttribute('href'); // original url
$extralink = '';
if(stristr($link,'#')) {
$pagelinktemp = explode("#",$link);
$pagelink = $pagelinktemp[0];
$extralink = '#'.$pagelinktemp[1];
} else {
$pagelink = $link;
}
if($pagelink!="") {
$postid = url_to_postid($pagelink);
$postobj = get_post($postid); // getting appropriate post object
if($postobj) {
$newlink = change_amp_url( $pagelink, $postobj ); //new url
}
else {
$newlink = $link;
}
}
else {
$newlink = $link;
}
if($link != $newlink) // change if only links are different
{
$content = str_replace($link, $newlink.$extralink, $content);
}
}
return $content;
}
/* override canonical link */
add_filter( 'wpseo_canonical', 'amp_override_canonical' );
function amp_override_canonical($url) {
if ( substr($url,-4)=="/amp" ) {
$url = substr($url,0,-4);
}
return $url;
}
The first function will provide the AMP URL if exists.
The second one will loop through each URL in the content and change to AMP URL if valid.
The last one will rewrite the canonical URL that displayed via Yoast SEO plugin.
If you want to replace hardcoded links inside of your post content I would suggest you use the "the_content" filter for wordpress.
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
add_filter( 'the_content', 'filter_function_name' )
From this you should be able to regular expression match the link and append /amp to it.
Pseudo code example:
function my_the_content_filter($content)
{
if (function_exists('is_amp_endpoint') && is_amp_endpoint()) {
$patterns = array(
//patterns
);
$replacements = array(
//replacements
);
$content = preg_replace($patterns, $replacements, $content);
}
return $content;
}
add_filter('the_content', 'my_the_content_filter');
I have tested the code submitted by Outsource WordPress, and in general it works fine but the 'amp_override_canonical function overwrites all the urls of the page removing the /amp.
I have made some changes to this piece of code but they do not work as I expect. It seems that the 'wpseo_canonical' function is being invoked in a different context.
add_filter( 'wpseo_canonical', 'amp_override_canonical' );
function amp_override_canonical($url) {
if ( substr($url,-4)=="/amp" ) {
$url = substr($url,0,-4);
}
return $url;
}
What I have done is I redirect the page after purchase but now I want to get the value of ordered quantity so that I can use it to my switch statement for some reason. How can I get the value of ordered quantity of the item?
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
//wp_redirect( 'http://grimmindustries.com.au/matchmaker/' );
$quantity = ; // ordered quantity that I need to use to switch statement to redirect
switch($quantity) {
case 1:
wp_redirect( 'http://www.facebook.com/' ); // exampe site
break;
case 2:
wp_redirect( 'http://www.youtube.com/' ); // exampe site
break;
default:
break;
}
exit();
}
}
add_action('template_redirect', 'wc_custom_redirect_after_purchase');
function wc_custom_redirect_after_purchase() {
global $wp;
if (is_checkout() && !empty($wp->query_vars['order-received'])) {
$order = new WC_Order($wp->query_vars['order-received']);
$quantity = 0;
if (count($order->get_items()) > 0) {
foreach ($order->get_items() as $item) {
if (!empty($item)) {
$quantity+= $item['qty'];
}
}
}
switch ($quantity) {
case 1:
wp_redirect('http://www.facebook.com/'); // exampe site
break;
case 2:
wp_redirect('http://www.youtube.com/'); // exampe site
break;
default:
break;
}
exit();
}
}
Try this code snippet.