0
<?php
/*
Plugin Name: Héroes y Pasta
*/
    include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    function iniciatodo() {
        if(is_user_logged_in()) {
?>
            <p>Hola tio</p>
<?php
        } else {
            wp_redirect( "http://gexy.es.cx/login/" );
            exit();
        }
    }
    add_shortcode("inicia_todo", "iniciatodo");
?>

So I'm making a pluging that has to work different if the user is logged in or not, but when the user is not logged it should redirect to the login page. What is wrong with the code or what does it lacks?

Thank you in advance

  • maybe that when the shortcode is send, headers are already send and then "wp_redirect" is not able to work. Another solution is to test in the action wp_loaded if you are at this page, do the redirection. – mmm Nov 17 '17 at 19:26
  • @mmm stop leaving answers as comments – Tom J Nowell Nov 17 '17 at 19:28
  • why is it a shortcode? – inarilo Nov 17 '17 at 21:39
  • Also, why is wp-admin/includes/plugin.php being included? It should be unnecessary and can cause security issues loading it on the frontend – Tom J Nowell Nov 18 '17 at 0:11
1

You can also consider a Javascript solution by adding a portion of code that will redirect people to the page if not loggued in :

<?php
/*
Plugin Name: Héroes y Pasta
*/
    include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    function iniciatodo() {
        if(is_user_logged_in()) {
?>
            <p>Hola tio</p>
<?php
        } else { ?>
            <script>window.location.href = 'http://gexy.es.cx/login/';</script>
       <?php }
    }
    add_shortcode("inicia_todo", "iniciatodo");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.