users - How to add wordpress username after url?

maybe u can help to solve this problem.for example i have this link?user=and i want to add username after login, like

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742354573a4428178.html

相关推荐

  • users - How to add wordpress username after url?

    maybe u can help to solve this problem.for example i have this link?user=and i want to add username after login, like

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信