0

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.

1
  • 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 Apr 26 '20 at 1:45
0

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

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

6
  • Actually the whole idea was to create a plugin. My first one but I don't find any help who to get that. – tmze Apr 26 '20 at 12:26
  • Instead of the old code, just write the shortcode [get_current_user] in storer.php – Joel Hager Apr 26 '20 at 18:21
  • I made it :) do you know how to test that on a local environment? there's no error anymore – tmze Apr 26 '20 at 20:52
  • It shouldn't matter where Wp is installed. It's querying the db for the current user. So just log in and access wherever that plugins page is called. – Joel Hager Apr 26 '20 at 21:06
  • oke so then it doesn't work.. – tmze Apr 26 '20 at 22:45
0

this is now the actual version

    <?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>

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.

6
  • Your <p> Lorem ipsum isn't wrapped in a conditional, so it's going to be shown no matter what. If you want to see if you have access to the $current_user object, do a var_dump($current_user);. I'm almost positive this plugin is trying to access an object that hasn't fired yet. Which is why we use the shortcode to grab a reference to that object globally. Not to be rude, but the implementation of this is nothing at all like I described. – Joel Hager Apr 27 '20 at 8:07
  • I'm really a beginner could you not copy paste the code and put it there where you described? I'm trying and trying and it doesn't work – tmze Apr 27 '20 at 8:22
  • My answer has the exact code you need to add to your functions.php file under your theme in wp_content>themes>your-theme-name>functions.php keep in mind, if you're using a child theme (which I highly encourage, as if you download an update all of your edits will go away). then you would find your active theme -child theme. Paste the large block of code as is on the bottom line of your functions.php file. Then in your plugin file, remove all of the logic, and just where you want it to say the name, write do_shortcode('[get_current_user]'); instead. None of the other stuff. – Joel Hager Apr 27 '20 at 8:29
  • Yes I understand but what I don't understand why do I have to put the code in a child-theme when I'm doing a Plugin. I only want to do the plugin that it works for every theme, not that I have to put manually the code in every theme. It's like when you download a plugin, you don't have to put manually a code in a theme – tmze Apr 27 '20 at 8:39
  • Ok, now I understand what you're meaning. I've edited my answer. Take a look and let me know how that goes. :) – Joel Hager Apr 27 '20 at 8:49
0

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. :)

6
  • I have now this solution: in my stoerer.php: – tmze Apr 26 '20 at 12:17
  • I doesn't work. Why put the PHP Code in a child-theme when I want to do a Plugin? – tmze Apr 27 '20 at 8:00
  • You're generating the shortcode in your theme folder. It hooks into the action to be able to access that $current_user object. Then, your plugin is accessing that by utilizing that function via shortcode. So you should have the code I provided in your functions.php in either your main theme or your child theme (if you're using a child theme it should go there). Then, you should use do_shortcode('[get_current_user]'); in your plugin. – Joel Hager Apr 27 '20 at 8:04
  • I'm so sorry thank you for your patience – tmze Apr 27 '20 at 9:21
  • Of course. Did you try to new answer? – Joel Hager Apr 27 '20 at 9:27
0

I have now this:

<?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>

I tried it also with

Global $current_user = wp_get_current_user();
11
  • this is the new code – tmze Apr 27 '20 at 9:50
  • You want to echo $current_user->user_firstname otherwise it'll only return it and not print it. – Joel Hager Apr 27 '20 at 9:58
  • It also seems the property may have changed to 'user_first_name' but it's either way in various places so I'd try both. – Joel Hager Apr 27 '20 at 10:00
  • It has a error: Uncaught Error: Call to undefined function wp_get_current_user() in on line 3. – tmze Apr 27 '20 at 11:09
  • Even as a global? – Joel Hager Apr 27 '20 at 11:17
0
<?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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.