仅当以<p>否则不直接使用名称登录时才显示用户名

时间:2020-04-25 作者:tmze

嗨,我到处找,但我没有找到解决方案:我有一个信息框,上面写着:嗨,欢迎来到我们的“一些文字”

现在我想添加登录用户的可能性“Hello”Tony, 欢迎来到我们的“一些文字”

我尝试了以下代码段:

<div id="infoBox">
<button class="cross" type="button">X</button>
  <p> Hello </p> <?php global $current_user; wp_get_current_user(); ?>
<?php if ( is_user_logged_in() ) { 
 echo \'Username: \' . $current_user->user_login . "\\n"; echo \'User display name: \' . $current_user->display_name . "\\n"; } 
else { wp_loginout(); } ?>



  <p>  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gub

  </p>
</div>
但我的XAMPP测试环境中有一个致命错误:

致命错误:未捕获错误:在/opt/lampp/htdocs/wordpress/wp-content/plugins/stoerer/stoerer中调用未定义的函数wp\\u get\\u current\\u user()。php:31堆栈跟踪:#0/opt/lampp/htdocs/wordpress/wp设置。php(377):include\\u once()#1/opt/lampp/htdocs/wp config。php(90):require\\u once(\'/opt/lampp/htdo…\')\\2/opt/lampp/htdocs/wordpress/wp load。php(42):require\\u once(\'/opt/lampp/htdo…\')\\3/opt/lampp/htdocs/wordpress/wp blog header。php(13):require\\u once(\'/opt/lampp/htdo…\')\\4/opt/lampp/htdocs/wordpress/index。php(17):require(\'/opt/lampp/htdo…\')#5{main}抛出/opt/lampp/htdocs/wordpress/wp-content/plugins/stoerer/stoerer。php第31行您的网站出现严重错误。

5 个回复
SO网友:Joel Hager

如果您正在开发一个用于多主题的插件,您可以声明$current_user 对象全局并调用它。

在插件的顶部。正在编辑的php,添加$current_user = wp_get_current_user();

那就这样说吧$current_user->user_firstname;

如果这样做了,请告诉我。:)

SO网友:tmze

这是stoerer中的全部代码。php

<!doctype html>
<html lang="de">
<meta charset="utf-8">
<title> Störer</title>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" type="text/css">

</head> 

<body>
    <header> 
<button>Ich bin ein Störer</button>



<!-- Und dann die Info-Box --> 
<div id="infoBox">
<button class="cross" type="button">X</button>
  <p> Hello </p> <?php wp_get_current_user(); ?>




  <p>  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gub

  </p>
</div>
</header> 

<script>
$("button").click(function(){
    $("#infoBox").toggle();
  });

</script>
</body>
</html>
这是子主题样式中的全部代码。css:

/*
 Theme Name:   Twenty Twenty Child
 Description:  Mein Child Theme
 Author:       Webtimiser
 Author URI:   https://www.example.com
 Template:     twentytwenty
 Version:      1.0
 Text Domain:  twenty-twenty-child
*/
这就是功能。子主题中的php:

<?php
/**
* Child theme stylesheet einbinden in Abhängigkeit vom Original-Stylesheet
*/

function child_theme_styles() {
wp_enqueue_style( \'parent-style\', get_template_directory_uri() . \'/style.css\' );
wp_enqueue_style( \'child-theme-css\', get_stylesheet_directory_uri() .\'/style.css\' , array(\'parent-style\'));

}
add_action( \'wp_enqueue_scripts\', \'child_theme_styles\' );



add_shortcode( \'get_current_user\' , \'wp_get_current_user_func\' );

function wp_get_current_user_func( $atts ) {

    $current_user = wp_get_current_user();

    if ($current_user != 0)
    {
        return $current_user->user_firstname;
    } else
    {
        return ""
    }
}?>
还是一样的错误

SO网友:tmze

现在这是实际版本

    <?php



/**
 * @package stoerer 
 * Plugin Name: Störer 
 * Plugin URI: http://www.mywebsite.com/stoerer
 * Description: Ein Störer für ddcom AG
 * Author: TM
 * Author URI: http://www.mywebsite.com
 * Version 1.0.0
 */



if ( ! defined( \'ABSPATH\' ) ){
    die;

}

class stoererPlugin
{
    function __construct() {
        add_action( \'init\', array( $this, \'custom_post_type\'));

    }

    function register(){
add_action( \'wp_enqueue_scripts\', array( $this, \'enqueue\' ) );

    }

   function activate(){
       //generated a cPT
      // $this->custom_post_type();
       // flush rewrite rules
       flush_rewrite_rules();
   }

   function deactivate() {
    // flush rewrite rules
    flush_rewrite_rules();
   }


        function enqueue() {
            //enqueue all our scripts
            wp_enqueue_style( \'mypluginstyle\', plugins_url( \'/assets/mystyle.css\', __FILE__ ));
            wp_enqueue_script( \'mypluginscript\', plugins_url( \'/assets/myscripts.js\', __FILE__));
        }


  function custom_post_type() {
       register_post_type( \'book\', [\'public\' => true, \'label\' => \'Books\']);
    }

    // methods 
}
if ( class_exists( \'stoererPlugin\')) {

$stoererPlugin = new stoererPlugin();
$stoererPlugin->register();

}

// activation 
register_activation_hook( __FILE__, array( $stoererPlugin, \'activate\'));

// deactivation
register_deactivation_hook( __FILE__, array( $stoererPlugin, \'deactivate\'));

//uninstall

?>

<!doctype html>
<html lang="de">
<head>
    <meta charset="utf-8">
<title> Störer</title>


</head> 

<body>

<button class="stoerer" id="stoerer"onclick="buttonShow()">Ich bin ein Störer</button>



<!-- Und dann die Info-Box --> 
<div id="infoBox">
<button class="cross" type="button" onclick="buttonHide()">X</button>
  <p> Hello </p> <?php add_shortcode( \'get_current_user\' , \'wp_get_current_user_func\' );

function wp_get_current_user_func( $atts ) {

    $current_user = wp_get_current_user();

    if ($current_user != 0)
    {
        return $current_user->user_firstname;
    } else
    {
        return "";
    }
}?>




  <p>  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gub

  </p>
</div>



</body>
</html>
也许你也知道为什么我的后端也会改变。它似乎不仅在前端使用,在后端也有我的lorem ipsum文本。

SO网友:tmze

我现在有这个:

<?php
$current_user = wp_get_current_user();


/**
 * @package stoerer 
 * Plugin Name: Störer 
 * Plugin URI: http://www.mywebsite.com/stoerer
 * Description: Ein Störer für ddcom AG
 * Author: TM
 * Author URI: http://www.mywebsite.com
 * Version 1.0.0
 */



if ( ! defined( \'ABSPATH\' ) ){
    die;

}



class stoererPlugin
{


    function __construct() {
        add_action( \'init\', array( $this, \'custom_post_type\'));

    }

    function register(){
add_action( \'wp_enqueue_scripts\', array( $this, \'enqueue\' ) );

    }

   function activate(){
       //generated a cPT
      // $this->custom_post_type();
       // flush rewrite rules
       flush_rewrite_rules();
   }

   function deactivate() {
    // flush rewrite rules
    flush_rewrite_rules();
   }


        function enqueue() {
            //enqueue all our scripts
            wp_enqueue_style( \'mypluginstyle\', plugins_url( \'/assets/mystyle.css\', __FILE__ ));
            wp_enqueue_script( \'mypluginscript\', plugins_url( \'/assets/myscripts.js\', __FILE__));
        }


  function custom_post_type() {
       register_post_type( \'book\', [\'public\' => true, \'label\' => \'Books\']);
    }

    // methods 
}
if ( class_exists( \'stoererPlugin\')) {

$stoererPlugin = new stoererPlugin();
$stoererPlugin->register();

}

// activation 
register_activation_hook( __FILE__, array( $stoererPlugin, \'activate\'));

// deactivation
register_deactivation_hook( __FILE__, array( $stoererPlugin, \'deactivate\'));

//uninstall

?>

<!doctype html>
<html lang="de">
<head>
    <meta charset="utf-8">
<title> Störer</title>


</head> 

<body>

<button class="stoerer" id="stoerer"onclick="buttonShow()">Ich bin ein Störer</button>



<!-- Und dann die Info-Box --> 
<div id="infoBox">
<button class="cross" type="button" onclick="buttonHide()">X</button>
  <p> Hello </p> <?php $current_user->user_firstname;
?>




  <p>  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gub

  </p>
</div>



</body>
</html>
我也试过了

Global $current_user = wp_get_current_user();

SO网友:tmze
<?php

global $current_user;
$current_user = wp_get_current_user();


/**
 * @package stoerer 
 * Plugin Name: Störer 
 * Plugin URI: http://www.mywebsite.com/stoerer
 * Description: Ein Störer für ddcom AG
 * Author: TM
 * Author URI: http://www.mywebsite.com
 * Version 1.0.0
 */


if ( ! defined( \'ABSPATH\' ) ){
    die;

}



class stoererPlugin
{



    function __construct() {
        add_action( \'init\', array( $this, \'custom_post_type\'));

    }

    function register(){
add_action( \'wp_enqueue_scripts\', array( $this, \'enqueue\' ) );

    }

   function activate(){
       //generated a cPT
      // $this->custom_post_type();
       // flush rewrite rules
       flush_rewrite_rules();
   }

   function deactivate() {
    // flush rewrite rules
    flush_rewrite_rules();
   }


        function enqueue() {
            //enqueue all our scripts
            wp_enqueue_style( \'mypluginstyle\', plugins_url( \'/assets/mystyle.css\', __FILE__ ));
            wp_enqueue_script( \'mypluginscript\', plugins_url( \'/assets/myscripts.js\', __FILE__));
        }


  function custom_post_type() {
       register_post_type( \'book\', [\'public\' => true, \'label\' => \'Books\']);
    }

    // methods 
}
if ( class_exists( \'stoererPlugin\')) {

$stoererPlugin = new stoererPlugin();
$stoererPlugin->register();

}

// activation 
register_activation_hook( __FILE__, array( $stoererPlugin, \'activate\'));

// deactivation
register_deactivation_hook( __FILE__, array( $stoererPlugin, \'deactivate\'));

//uninstall

?>

<!doctype html>
<html lang="de">

</head> 

<body>

<button class="stoerer" id="stoerer"onclick="buttonShow()">Ich bin ein Störer</button>



<!-- Und dann die Info-Box --> 
<div id="infoBox">
<button class="cross" type="button" onclick="buttonHide()">X</button>
  <?php echo "<h2> Hallo, </h2>" ; echo $current_user->user_firstname;
echo "Ich freue mich";
?>





</div>



</body>
</html>

相关推荐

How to change a username?

如何更改用户名?我的用户名以作者身份出现在搜索引擎中,但它是一个基于产品和服务的网站(仅使用页面),供客户使用。如果我无法删除作者页面,我需要更改用户名。如果打开,您似乎可以更改用户名wordpress.com 所以这意味着它可能是可能的,但这是一个个人WP安装。