I want to check if a link has been clicked and redirect it with Wordpress

Asked

Viewed 754 times

0

At the bottom (footer) has an image that is link to a page - external to my site - Google Docs. I want to restrict access to this page of Google Docs as follows: - If who clicked on the image/link is not logged, page login.php Wordpress (WP); - If you are logged in I open the Google Docs page.

I thought I’d use the file functions.php from WP, the idea is more or less this, just do not know how to check the click with PHP or with WP itself:

function logadoRedireciona() {
    if ( is_user_logged_in() ) { // se estiver logado 
        // vá para o link
        wp_redirect( 'https://docs.google.com/' );
        exit;
    } else { // se não 
        // faça o login
        wp_login_form();
    }
} 
add_action ( 'wp_footer' , 'logadoRedireciona' );

The WP has some template that checks links or Urls? Or using PHP, how can I do this?

2 answers

1

The image will be the same whether logged in or not:

<?php
if ( is_user_logged_in() ) {
    echo '<a href="https://docs.google.com/"><img src="imagem.png"></a>';
} else {
    echo '<a href="'.site_url().'/logar?jaCliquei=1"><img src="imagem.png"></a>';
}
?>

When he clicks on the link a condition created on functions.php will redirect the user after the login detecting the jaCliquei=1:

    function my_login_redirect( $redirect_to, $request, $user ) {

    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //if admin
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirecionamento padrão
            return $redirect_to;
        } else {
            if($_GET['jaCliquei'] == 1) {

             //AQUI O REDIRECIONAMENTO PARA O DOCS
              wp_redirect( 'https://docs.google.com/' );
            }else{

             //Login padrão de usuário comum
             return home_url();

            } 


        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

-

  • I didn’t test it, but see if it works.

  • Thank you. I’m pretty new to WP, so forgive me for the questions: the first snippet of code goes in footer.php? and the second on functions, right?

  • Exactly!.....

  • One more thing the image, the image/link in question was inserted in the footer through a widget. The theme I’m using is the crumina (widget, Jetpack image). Complicated, right?

  • The question is whether you can detect if you are logged in or not to put a link in the image depending on this.

0

If I understood your problem, you almost got there. In the footer of your site you will exbir the link normally and on the page of the Docs you will do the if

<?php  if (is_user_logged_in()): ?>
     //Conteudo do doc    
<?php else: ?>
// Fomulario de Login
<?php endif; ?>

I think this will help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.