• Resolved anton1234

    (@anton1234)


    Hii guys!

    I need some help with the following. On my forum there was no link to login. So right below the page title I created a “login bar” with some basic HTML, CSS, PHP and the help of “insert PHP” plugin. It also displays a welcome message. Works fine for me.

    Piece of code:

    <div class="message">
    <div class="welcome">[insert_php]
    if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
        echo 'Welcome, ' , esc_html( $current_user->display_name );
    } else {
        echo 'Welcome, visitor!';
    }
    
    [/insert_php]</div>
    <div class="action">[insert_php]
    if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
        echo '<a href="link">Logout »</a> <a href="/messageboard/profile/">Change Profile »</a>' ;
    } else {
        echo '<a href="link">Login »</a>';
    }
    
    [/insert_php]</div></div>

    But now I’m worried because all the editors can see this code and maybe screw with it. So what I’d like to do is to make a shortcode (for example: [loginbar]) that calls for that piece of code. So I’ll only have to put the “[loginbar]” in the page, instead of the code itself.

    This is a snippet from my functions.php I’ve tested and works:

    function login_bar( ) {
    	echo 'test';
       
    }
    add_shortcode ( 'loginbar', 'login_bar' ) ;

    So now when I insert [loginbar] on my page, it shows the word “test”

    But there is no way I can echo my piece of code because there is a lot of closing and opening tags in it, that close the echo to early. And I’m also confused if my “insert PHP” plugin would still work this way.

    Can someone help me how to display the code, I’m not familiar with PHP at all. If I need something done with PHP I just watch tutorial videos. But there isn’t any on this topic :(.

    Thanks in advance!

    • This topic was modified 8 years, 9 months ago by anton1234.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter anton1234

    (@anton1234)

    I’m sorry for the bad aligning on the head post

    • This reply was modified 8 years, 9 months ago by anton1234.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Do not echo within a shortcode — A shortcode returns a string containing the contents to be displayed.

    Here’s a link to a bunch of shortcodes showing how to return strings and execute PHP:

    https://github.com/sterndata/sdsplugin/blob/master/sterndata-plugin.php

    Thread Starter anton1234

    (@anton1234)

    Thanks for your reply, but I’m having trouble understanding what it is I need to do. I’m not familiar with PHP, that link doesn’t make a lot of sense to me.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Put your PHP inside a function (with all its conditionals) and use that function to return a string which you hook into a shortcode.

    something like

    
    function my_stuff( $atts, $contents) {
       ob_start();
       if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
        echo 'Welcome, ' , esc_html( $current_user->display_name );
    } else {
        echo 'Welcome, visitor!';
    }
    
    echo '<div class="action">';
    if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            echo '<a href="link">Logout »</a> <a href="/messageboard/profile/">Change Profile »</a>' ;
         } else {
           echo '<a href="link">Login »</a>';
         }
    return ob_get_clean();
    }
    add_shortcode( 'logged_in', 'my_stuff' );

    That code has not been tested and may contain errors.

    Thread Starter anton1234

    (@anton1234)

    I’ll try that right away

    • This reply was modified 8 years, 9 months ago by anton1234.
    • This reply was modified 8 years, 9 months ago by anton1234.
    • This reply was modified 8 years, 9 months ago by anton1234.
    Thread Starter anton1234

    (@anton1234)

    My end result:

    function login_bar ( $atts, $contents) {
       ob_start();
     echo '<div class="message"><div class="welcome">';
       if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
        echo 'Welcome, ' , esc_html( $current_user->display_name );
    } else {
        echo 'Welcome, visitor!';
    }
    echo '</div><div class="action">';
    if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            echo '<a href="link">Logout »</a> <a href="/messageboard/profile/">Change Profile »</a>' ;
         } else {
           echo '<a href="link">Login »</a>';
         }
    echo '</div></div>';
    return ob_get_clean();
    }
    add_shortcode( 'loginbar', 'login_bar' );

    Thanks for the help! Works great!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Creating a shortcode that inserts code’ is closed to new replies.