Creating a shortcode that inserts code
-
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!
The topic ‘Creating a shortcode that inserts code’ is closed to new replies.