Hi I'm looking all over but I don't find the solution: I have an infobox with: Hi, welcome to our "some text"
Now I'd like to add posibility for logged in users "Hello Tony, welcome to our "some text"
I tried following code snippet:
<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>
but there's a fatal error in my XAMPP testing environment:
Fatal error: Uncaught Error: Call to undefined function wp_get_current_user() in /opt/lampp/htdocs/wordpress/wp-content/plugins/stoerer/stoerer.php:31 Stack trace: #0 /opt/lampp/htdocs/wordpress/wp-settings.php(377): include_once() #1 /opt/lampp/htdocs/wp-config.php(90): require_once('/opt/lampp/htdo...') #2 /opt/lampp/htdocs/wordpress/wp-load.php(42): require_once('/opt/lampp/htdo...') #3 /opt/lampp/htdocs/wordpress/wp-blog-header.php(13): require_once('/opt/lampp/htdo...') #4 /opt/lampp/htdocs/wordpress/index.php(17): require('/opt/lampp/htdo...') #5 {main} thrown in /opt/lampp/htdocs/wordpress/wp-content/plugins/stoerer/stoerer.php on line 31 There has been a critical error on your website.
Hi I'm looking all over but I don't find the solution: I have an infobox with: Hi, welcome to our "some text"
Now I'd like to add posibility for logged in users "Hello Tony, welcome to our "some text"
I tried following code snippet:
<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>
but there's a fatal error in my XAMPP testing environment:
Share Improve this question edited Apr 27, 2020 at 7:54 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Apr 25, 2020 at 22:34 tmzetmze 32 bronze badges 1Fatal error: Uncaught Error: Call to undefined function wp_get_current_user() in /opt/lampp/htdocs/wordpress/wp-content/plugins/stoerer/stoerer.php:31 Stack trace: #0 /opt/lampp/htdocs/wordpress/wp-settings.php(377): include_once() #1 /opt/lampp/htdocs/wp-config.php(90): require_once('/opt/lampp/htdo...') #2 /opt/lampp/htdocs/wordpress/wp-load.php(42): require_once('/opt/lampp/htdo...') #3 /opt/lampp/htdocs/wordpress/wp-blog-header.php(13): require_once('/opt/lampp/htdo...') #4 /opt/lampp/htdocs/wordpress/index.php(17): require('/opt/lampp/htdo...') #5 {main} thrown in /opt/lampp/htdocs/wordpress/wp-content/plugins/stoerer/stoerer.php on line 31 There has been a critical error on your website.
- It's trying to access a function that hasn't been defined yet. There are a few ways to get around it, but I'm looking into the one that conforms to best practices. Hold please. :) – Joel Hager Commented Apr 26, 2020 at 1:45
5 Answers
Reset to default 0this is the whole code in stoerer.php
<!doctype html>
<html lang="de">
<meta charset="utf-8">
<title> Störer</title>
<head>
<script src="https://ajax.googleapis/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>
this is the whole code in the child theme style.css:
/*
Theme Name: Twenty Twenty Child
Description: Mein Child Theme
Author: Webtimiser
Author URI: https://www.example
Template: twentytwenty
Version: 1.0
Text Domain: twenty-twenty-child
*/
and this is the functions.php in the child theme:
<?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 ""
}
}?>
and it's still the same error
this is now the actual version
<?php
/**
* @package stoerer
* Plugin Name: Störer
* Plugin URI: http://www.mywebsite/stoerer
* Description: Ein Störer für ddcom AG
* Author: TM
* Author URI: http://www.mywebsite
* 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>
maybe you know too why my backend changes too. it seems like it's not only used in the front its in the backend too there is my lorem ipsum text.
If you're developing a plugin that's used for multi themes, you can declare $current_user
object global and invoke it.
At the top of your plugin.php that you're editing, add $current_user = wp_get_current_user();
Then call it like $current_user->user_firstname;
Let me know if that does it. :)
I have now this:
<?php
$current_user = wp_get_current_user();
/**
* @package stoerer
* Plugin Name: Störer
* Plugin URI: http://www.mywebsite/stoerer
* Description: Ein Störer für ddcom AG
* Author: TM
* Author URI: http://www.mywebsite
* 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>
I tried it also with
Global $current_user = wp_get_current_user();
<?php
global $current_user;
$current_user = wp_get_current_user();
/**
* @package stoerer
* Plugin Name: Störer
* Plugin URI: http://www.mywebsite/stoerer
* Description: Ein Störer für ddcom AG
* Author: TM
* Author URI: http://www.mywebsite
* 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>