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

0

Footer ) has an image that is link for a page - external to my site - from Google Docs. I want to restrict access to this Google Docs page as follows: - If the person who clicked on the image / link is not logged in , I call the login.php Wordpress (WP); - If it's logged in I'll open the Google Docs page.

I thought of using the file functions.php of WP, the idea is more or less this, just do not know how to check the click with PHP or with the 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' );
Does WP have some template tag that checks links or URLs ? Or using PHP, how do I do that?

    
asked by anonymous 02.09.2015 / 00:58

2 answers

1

The image will be the same either for login 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 it clicks the link a condition created in functions.php will redirect the user after login by detecting 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 );

-

    
02.09.2015 / 01:16
0

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

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

I think this will help you.

    
05.09.2015 / 15:54