• I read the API tutorial for creating wordrpess shortcodes and I’m still not quite sure what to do.

    I’m basically trying to create a dynamic login link into a shortcode

    I’m trying to put this into shortcode form

    <?php if ( is_user_logged_in() ) { ?>
    <div class="logoutformat">< a href="<?php echo wp_logout_url( get_permalink(716) ); ?>" title="Logout">Logout</a> </div>
    
    <?php } else { ?>
    
    <div class="loginformat"> <a href="<?php echo wp_login_url(); ?>" title="Login">Login</a></div>
    
    <?php } ?>

    According to how I understand the codex, shouldn’t the function I input into my functions.php file look something like this?

    function foobar_func( $atts ){
    	return "if ( is_user_logged_in() ) { ?>
    <div class='logoutformat'>< a href='<?php echo wp_logout_url( get_permalink(716) ); ?>' title='Logout'>Logout</a> </div>
    
    <?php } else { ?>
    
    <div class='loginformat'> <a href='<?php echo wp_login_url(); ?>' title='Login'>Login</a></div>
    
    <?php }
    ";
    }
    add_shortcode( 'foobar', 'foobar_func' );
Viewing 1 replies (of 1 total)
  • Your calls to wp_logout_url() and wp_login_url() will not be executed because they are being returned as part of a string. The function should look like this (untested):

    function foobar_func( $atts ){
       if ( is_user_logged_in() ) {
          $op = "<div class='logoutformat'><a href='" . wp_logout_url( get_permalink(716) ) .
                "' title='Logout'>Logout</a></div>";
       } else {
          $op = "<div class='logoutformat'><a href='" . wp_login_url( get_permalink(716) ) .
                "' title='Login'>Login</a></div>";
       }
       return $op;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to create a shortcode’ is closed to new replies.