Ask your WordPress questions! Pay money and get answers fast! (more info)

Email parameter in URL when logged in for 1 page only? WordPress

  • SOLVED

Continuing last question:
http://www.wpquestions.com/question/showLoggedIn/id/9624

Would there be a way to have this working only for 1 page of my WP website?

Many thanks

The answer of @timDesain worked, but I would love this work for only one page.
dd_action('wp_footer', 'timdesain_var_footer');

function timdesain_var_footer(){
global $user_ID;

if(is_user_logged_in()){
$user_info = get_userdata($user_ID);
$user_email = $user_info->user_email;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
var mail = 'email=<?php echo sanitize_email($user_email); ?>';
jQuery('a').attr('href', function() {
if(!/email/i.test(this)){
return this.href + '?' + mail;
}
});
});
</script>
<?php
}
}

add_action('init', 'timdesain_var_init');
function timdesain_var_init(){
//get the email here
if(is_user_logged_in()){
$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';
//echo $email;
}
}

Answers (4)

2014-06-05

Remy answers:

You can check for the page you're currently on with is_page(). Example :

dd_action('wp_footer', 'timdesain_var_footer');



function timdesain_var_footer(){

global $user_ID;



if(is_user_logged_in() && is_page( 42 ) ){

$user_info = get_userdata($user_ID);

$user_email = $user_info->user_email;

?>

<script type="text/javascript">

jQuery(document).ready(function(){

var mail = 'email=<?php echo sanitize_email($user_email); ?>';

jQuery('a').attr('href', function() {

if(!/email/i.test(this)){

return this.href + '?' + mail;

}

});

});

</script>

<?php

}

}



add_action('init', 'timdesain_var_init');

function timdesain_var_init(){

//get the email here

if(is_user_logged_in() && is_page( 42 ) ){

$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';

//echo $email;

}

}


42 is the ID of the page, you can also pass the slug if you prefer


Jonathan Surinx comments:

Yes that worked! :)
Same story for a few pages?
if(is_user_logged_in() && is_page( 42,43,44 ) ){
Many thanks,
Jonathan


Remy comments:

For more than one page, you need to put the IDs/slugs in an array like this :

is_page( array( 42,43,44 ) )

You can mix IDs and slugs too

is_page( array( 42, 'contact' , 44, 'blog' ) )


Jonathan Surinx comments:

Ack I just tried a bit further (with one page only) and it doens't always work...

When I refresh the home page and click on the page ( 42 ) it works.

Then I click on another page ( 43 ) and come back to ( 42 ) and it doesn't work anymore --> the email in URL disappeared.

Any ideas?
Many thanks,
Jonathan


Remy comments:

Remove is_page from the second function and tell me if it fixes it

function timdesain_var_init(){



//get the email here



if(is_user_logged_in() ){



$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';



//echo $email;



}



}


Jonathan Surinx comments:

Hmm in fact it didn't really work.

All pages have the email parameter the first time I go on it. And then they all dissapear (up to the point I refresh the website again).


Ideas?


Remy comments:

Do you have a test link ?


Jonathan Surinx comments:

Yes but I will send it to you in PM, just a sec.


Jonathan Surinx comments:

Hi Remy, just got your code:
function append_query_string( $url, $post ) {
if ( $post == 1098 && is_user_logged_in() ) {
global $user_ID;

$user_info = get_userdata($user_ID);

$user_email = $user_info->user_email;

return add_query_arg( 'email', $user_email );
}
return $url;
}
add_filter( 'page_link', 'append_query_string', 10, 2 );

It looks like working perfectly, only issue it also affects the url name,
now it shows:
<strong>mywebsite.com/[email protected] (which already works so great!)</strong>
instead of
<strong>mywebsite.com/page1/[email protected]</strong>


Remy comments:

Function is missing one parameter, that's why it's not working correctly. This should be ok

function append_query_string( $url, $post ) {

if ( $post == 1098 && is_user_logged_in() ) {

global $user_ID;



$user_info = get_userdata($user_ID);



$user_email = $user_info->user_email;



return add_query_arg( 'email', $user_email, $url );

}

return $url;

}

add_filter( 'page_link', 'append_query_string', 10, 2 );


Jonathan Surinx comments:

Amazing! All perfect :)
Many thanks (merci beaucoup !! ;)
Jonathan

2014-06-05

smc answers:

for which page you want this?


smc comments:

i have modified the code a little and hope that will work , you just have to change the variable with correct page id you want ,
$page_id = 111111; so change this 111111 to the id of the page ..
let me know if that not works for you.

<?php
dd_action('wp_footer', 'timdesain_var_footer');



function timdesain_var_footer(){

$page_id = 111111;

if( !is_page( $page_id ) )
return ;

global $user_ID;



if(is_user_logged_in()){

$user_info = get_userdata($user_ID);

$user_email = $user_info->user_email;

?>

<script type="text/javascript">

jQuery(document).ready(function(){

var mail = 'email=<?php echo sanitize_email($user_email); ?>';

jQuery('a').attr('href', function() {

if(!/email/i.test(this)){

return this.href + '?' + mail;

}

});

});

</script>

<?php

}

}



add_action('init', 'timdesain_var_init');

function timdesain_var_init(){

//get the email here

if(is_user_logged_in()){

$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';

//echo $email;

}

}


Jonathan Surinx comments:

No that didn't help. Thanks.


smc comments:

try this for multiple pages
<?php
dd_action('wp_footer', 'timdesain_var_footer');



function timdesain_var_footer(){



global $user_ID;



if(is_user_logged_in() && ( is_page( 42 ) || is_page( 43 ) || is_page( 44 ) ) ){

$user_info = get_userdata($user_ID);

$user_email = $user_info->user_email;

?>

<script type="text/javascript">

jQuery(document).ready(function(){

var mail = 'email=<?php echo sanitize_email($user_email); ?>';

jQuery('a').attr('href', function() {

if(!/email/i.test(this)){

return this.href + '?' + mail;

}

});

});

</script>

<?php

}

}



add_action('init', 'timdesain_var_init');

function timdesain_var_init(){

//get the email here

if(is_user_logged_in() && ( is_page( 42 ) || is_page( 43 ) || is_page( 44 ) ) ){

$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';

//echo $email;

}

}

2014-06-05

Bob answers:

can you please try this code
add your desired id,slug or page title in <strong>is_page()</strong>

add_action('wp_footer', 'add_email_footer');

function add_email_footer(){
global $user_ID;
//here in you have to pass page_id or page title or page_slug
if(is_user_logged_in() && is_page( 1 )){
$user_info = get_userdata($user_ID);
$user_email = $user_info->user_email;

?>

<script type="text/javascript">

jQuery(document).ready(function(){

jQuery('a').each(function(){
var mail = 'email=<?php echo sanitize_email($user_email); ?>';
var href = jQuery(this).attr('href');

if(href) {
href += (href.match(/\?/) ? '&' : '?') + mail;
jQuery(this).attr('href', href);
}
});
});

</script>

<?php

}

}



add_action('init', 'timdesain_var_init');

function timdesain_var_init(){

//get the email here

if(is_user_logged_in()){

$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';

//echo $email;

}

}
?>

2014-06-05

timDesain Nanang answers:

how about this:


<strong>before: </strong>
global $user_ID;

<strong>after</strong>
global $user_ID, $post;

and

<strong>before: </strong>
if(is_user_logged_in()){

<strong>after</strong>

use <strong>is_single</strong> for any <strong>single Post</strong> or attachment or <strong>custom Post Type</strong>
if(is_user_logged_in() AND is_single( 1234 ) ){

use <strong>is_page</strong> for post_type <strong>page only</strong>
if(is_user_logged_in() AND is_page( 1234 ) ){


Jonathan Surinx comments:

Hello Tim,
I just tried your solution and it looks like almost right. Here is the code:

add_action('wp_footer', 'timdesain_var_footer');
function timdesain_var_footer(){
global $user_ID, $post;

if(is_user_logged_in() AND is_page('1098') ){
$user_info = get_userdata($user_ID);
$user_email = $user_info->user_email;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
var mail = 'email=<?php echo sanitize_email($user_email); ?>';
jQuery('a').attr('href', function() {
if(!/email/i.test(this)){
return this.href + '?' + mail;
}
});
});
</script>
<?php
}
}

add_action('init', 'timdesain_var_init');
function timdesain_var_init(){
//get the email here
if(is_user_logged_in() AND is_single('1098') ){
$email = isset($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '';
//echo $email;
}
}


But as a result I have only the email parameter when I'm in this page ('1098') for all <strong>other pages</strong>.
--> So if I click in this page I see that the parameter is on all other pages (but not the one I'm on).
Almost there ;)


Jonathan Surinx comments:

Sorry I forgot to change the second :

if(is_user_logged_in() AND is_single('1098') ){
to
if(is_user_logged_in() AND is_page('1098') ){
But same result.
Many thanks