admin管理员组

文章数量:1333442

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

/?user=

and i want to add username after login, like /?user=admin

I want to make it on button.

<a href="/?user=username">
    <button>Click me</button>
</a>

I want to put this on post / page.

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

https://direktoriku/shopping/?user=

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

I want to make it on button.

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

I want to put this on post / page.

Share Improve this question edited Apr 17, 2018 at 6:53 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Apr 17, 2018 at 3:20 Angga PradiptaAngga Pradipta 31 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/shopping/?user=<?php echo $current_user->user_login;?>
   <button>Click me</button>
</a>
<?php endif;?>

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/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.

本文标签: usersHow to add wordpress username after url