-1

maybe u can help to solve this problem. for example i have this link

https://direktoriku.com/shopping/?user=

and i want to add username after login, like https://direktoriku.com/shopping/?user=admin

I want to make it on button.

<a href="https://direktoriku.com/shopping/?user=username">
    <button>Click me</button>
</a>

I want to put this on post / page.

0

You can do

<?php
      if( get_current_user_id() ): // check if user is loggedin
         $current_user = wp_get_current_user();  //get user       
?>
<a href="https://direktoriku.com/shopping/?user=<?php echo $current_user->user_login;?>
   <button>Click me</button>
</a>
<?php endif;?>
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Apr 17 '18 at 6:55
0

This is a small plugin I made for you. Put this code in a php file and upload it as a plugin, and activate it. Then put the shortcode [my-awesome-button] in a post or page, and there it will appear the button. If the visitor is not logged-in, nothing will appear.

<?php

/*
Plugin Name: My Awesome Button
Description: The shortcode [my-awesome-button] will be replaced with the HTML code for a button for logged-in users and for guests it will be replaced with an empty string (so it will be removed). This will work for posts and pages.
Author: Nikolay Nikolov
Version: 1.0.0
*/

add_filter( 'the_content', 'my_awesome_button_function', 99999999999 );

function my_awesome_button_function( $content ) {
    if ( strpos( $content, '[my-awesome-button]' ) !== false ) {
        if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            $button_html = '<a href="https://direktoriku.com/shopping/?user=' . esc_attr( $current_user->user_login ) . '"><button>Click me</button></a>';
            $content = str_replace( '[my-awesome-button]', $button_html, $content );
        } else {
            $content = str_replace( '[my-awesome-button]', '', $content );
        }
    }
    return $content;
}

By the way, we can easily modify this to only show the username, not the whole button. This way you can put the username anywhere you want in the post or page. Let me know if you need help with that.

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.