繁体   English   中英

WordPress:如何在Wordpress中按用户名搜索用户

[英]Wordpress : how to search a user by username in wordpress

我正在尝试在WordPress的在线游戏帖子下进行一个{Ach for a challange}按钮,因此用户将通过搜索框选择另一个用户,并通过游戏链接发送请求,但我不知道该如何做。去做这个,

因此,一开始我尝试了这段代码,以便可以向我显示搜索方法是否有效,但没有成功,这也是我第一次创建搜索框,因此..这里是:

<?php 
 global $wpdb ;
if($_POST){

    $search_input = $_POST['chs'];
    if(username_exists($search_input)){
        print_r($search_input)  ;

    }else {
        print_r("there is no username by this");
    }
}

?>

<html>
    <head>

    </head>
    <body>
        <form method="post">
            <p> would you challenge your friends ? </p>
            <input type="text" name="chs" id="chs" >
            <button type="submit"> Challenge</button>
        </form>

    </body>

</html>

在此代码中,我希望它返回搜索到的用户名(如果存在的话),但随后我需要返回可能与条目匹配的用户名建议。 例如:如果用户键入“ mf”,它将根据用户键入的内容返回用户名的最佳建议,然后用户从中进行选择。

您可以使用get_user_by()通过用户名获取用户信息

<?php
$user = get_user_by( 'login', $username);

if ( $user !== false){
   // User with the username is found. Do your work
} else {
   // User with the username is not found.
}
?>

参考: https : //developer.wordpress.org/reference/functions/get_user_by/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM