I'm trying to alter the experience of the user by changing the page templates based on URL UTM variables.
Example, if the utm_source=google I want to show the person the page-google.php.
Here's the code I have so far.
<?php if ( isset( $_GET['utm_source'] ) && $_GET['utm_source'] == 'test' )
{
get_page_template(test);
}
else {
get_page_template(gallery);
}
?>
I'm not sure 1) if this is the right way 2) where it should be placed in wordpress.
Thanks for your help!
in page.php
<?php if ( isset( $_GET['utm_source'] ) && $_GET['utm_source'] == 'test' ) {
get_template_part('test');
}
else {
get_template_part('gallery');
}
?>
this is if test.php is in your sites theme directory
Related
We have added a live product search shortcode on the top of our website for mobile devices only.
On desktops we have left the live product search in a side bar.
We have a function that only calls and places the shortcode for mobile devices.
Previously when you done a live product search on desktop devices and hit enter, 20 products would be returned and made visible in a grid view on the screen.
Since adding the shortcode for mobile devices, this has broken this feature for desktop devices.
The products still appear in a drop down menu, but previously you could hit enter and they would be moved into a nice grid view display in the centre of the screen.
To achieve this before, to enable the products to appear in the centre of the screen, the following line of code was added:
if (get_option( 'wpsc_default_category' ) == 'list' && isset($wp_query-
>query_vars['search_terms']))
$output = false;
}
Section of code with the above code having been added at the bottom:
function wpsc_display_categories() {
global $wp_query;
$output = false;
if ( !is_numeric( get_option( 'wpsc_default_category' ) ) && ! get_query_var( 'product_tag' ) ) {
if ( isset( $wp_query->query_vars['products'] ) )
$category_id = $wp_query->query_vars['products'];
else if ( isset( $_GET['products'] ) )
$category_id = $_GET['products'];
// if we have no categories, and no search, show the group list
if ( is_numeric( get_option( 'wpsc_default_category' ) ) || (isset( $product_id ) && is_numeric( $product_id )) )
$output = true;
if ( (get_option( 'wpsc_default_category' ) == 'all+list'))
$output = true;
if (get_option( 'wpsc_default_category' ) == 'list' && (!isset($wp_query->query_vars['wpsc_product_category']) || !isset($wp_query->query_vars['product_tag']) && get_option('wpsc_display_categories')))
$output = true;
if (get_option( 'wpsc_default_category' ) == 'list' && isset($wp_query->query_vars['search_terms']))
$output = false;
}
if ( isset( $category_id ) && $category_id > 0 )
$output = false;
if ( get_option( 'wpsc_display_categories' ))
$output = true;
return $output;
}
Not sure why adding the shortcode has broken the products returning in the centre of the screen, from the first line of code shown at the top here?
It's strange as the function we have that adds the shortcode for mobiles, checks that it's a mobile device and only applies the shortcode for mobile devices.
Any thoughts, or ideas on how we can solve this one?
Current filter in functions.php which checks for the mobile devices, and applies the shortcode if a mobile device is detected:
add_action( 'generate_inside_navigation','tu_add_to_mobile_bar' );
function tu_add_to_mobile_bar() {
if (wp_is_mobile()) {
?>
<div class="search">
<?php echo do_shortcode( '[our shortcode in here] ' ); ?>
</div>
<?php
}}
Here's the gist of what I was talking about.
Step #1 - Register a variable with the global query
This isn't technically 100% necessary, but it is generally considered a best practice. You could alternatively just inspect the raw $_GET or $_POST globals.
add_filter(
'query_vars',
function( $vars ) {
$vars[] = 'is_mobile_search';
return $vars;
}
);
Step #2 - Pass the variable
I'm assuming your shortcode actually renders a form, and that you can conditionally change it somehow, maybe [our_shortcode mobile="true"].
<form>
<input type="hidden" name="is_mobile_search" value="true" />
</form>
Step #3 - Use the variable
The contents of this function are your normal stuff, you are just checking is_mobile_search for the literal (but arbitrary) string value of true.
function wpsc_display_categories() {
$is_mobile_search = get_query_var('is_mobile_search') === "true";
}
I'm trying to get if its home page or not. (I mean all kind of home page in wordpress: Default, static, blog). I wrote this code in function.php file but in all pages I get "not home"!!
if ( is_front_page() && is_home() ) {
die("home");
} elseif ( is_front_page() ) {
die("home");
} elseif ( is_home() ) {
die("home");
} else {
die("not home");
}
Set home page screenshot https://prnt.sc/rbfgy8
Add this code in theme functions.php
//Put the condition in wp_footer hook to see the result
add_action('wp_footer', 'custom_wp_footer_fun');
function custom_wp_footer_fun(){
if ( is_front_page() && is_home() ) {
echo 'home';
} elseif ( is_front_page() ) {
echo 'home';
} elseif ( is_home() ) {
echo 'home';
} else {
echo 'not home';
}
}
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
}
I am working on uninstall.php script and want to know how to properly delete option in multi-sites environment. So far I have the following:
if ( ! is_multisite() ) {
delete_option('my-option-name');
} else {
# Get all sites
$sites = # Not sure how to get this
foreach ( $sites as $site ) {
switch_to_blog( $site);
delete_option('my-options-name')
}
restore_current_blog();
}
Or does the following code achieve the same thing?
if ( ! is_multisite() ) {
delete_option('my-option-name');
} else {
delete_site_option('my-option-name');
}
I have this bit of code in my header:
<?php if (has_nav_menu('sub-header-menu', 'responsive')) { ?>
<?php wp_nav_menu(array(
'container' => '',
'menu_class' => 'sub-header-menu',
'theme_location' => 'sub-header-menu')
);
?>
<?php } ?>
And I need something that will make it only show on the blog page and the children for that page (i.e the categories).. I'm not great with PHP but I guess this would be something simple
Just add a page Id of your blog page in you condition.
$parentPageId = is_subpage();
if (has_nav_menu('sub-header-menu', 'responsive') &&
(is_page( $blogPageId ) || $parentPageId == $blogPageId))
You can alos check your page using slug.
is_page( 'blog' )
Function to get Parent Page Id if exists.
function is_subpage() {
global $post;
if ( is_page() && $post->post_parent ) {
return $post->post_parent;
} else {
return false;
}
}
Find out the ID of the blog page $blogid = 123 (for example) and then check with if ($page->ID == $blogid) { /*show menu*/ }