Asked  7 Months ago    Answers:  5   Viewed   40 times

I'm trying to get the current user info in my plugin using the func wp_get_current_user(). But am getting Call to undefined function wp_get_current_user()

Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.

Anybody any ideas on how to get the user details in my plugin?

 Answers

38

Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.

Indeed it is. So wrap whichever thing you're doing in a function, and hook it onto the plugins_loaded or init hook. (see the wp-settings.php file)

Example:

add_action('init','do_stuff');
function do_stuff(){
  $current_user = wp_get_current_user();
  // ...
}
Wednesday, March 31, 2021
 
apokryfos
answered 7 Months ago
30

I don't know how your code looks but this is how I just tested and it worked:

add_action('admin_menu', 'my_menu');

function my_menu() {
    add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-page-slug', 'my_function');
}

function my_function() {
    echo 'Hello world!';
}

Take a look here http://codex.wordpress.org/Administration_Menus

Wednesday, March 31, 2021
 
anjan
answered 7 Months ago
59

finfo_open is only available for php >= 5.3.0 http://www.php.net/manual/en/function.finfo-open.php

You may want to double check your server is up to date with php_info()

Wednesday, March 31, 2021
 
EnTee
answered 7 Months ago
63

You are directly calling theme's index file which is not correct way, as your theme must be using some default functions of WordPress, like get_header() in this case.

So you need to make sure wp-load.php is loaded to make all WP functions available to use. You have two way for that:

1) Call root index.php so everything will be loaded by default.

2) Call theme's index.php but add Below code in that:

if(!function_exists('get_header')) {
    require_once( '/wp-load.php' );
}

However this is not good way :)

Friday, May 28, 2021
 
aWebDeveloper
answered 5 Months ago
71

That happened because that's not how templates work in WordPress. You don't create a specific file for each page in your website. You create pages, and then you assign templates to them, and let WordPress figure out how to access and create accesses to those pages. Trying to direct access one of those files will yield a 404 because WordPress due to the fact that a page (in wp land) with that name does not exist.

The fact that it did work when you tried going directly into index.php is because , in the template hierarchy, index.php is the last file WP looks for when searching for a template from which to display your page. As this file is a must-have in every theme, it was found, and thus no 404s.

There's something called permalinks which allows you to create friendly URLs to your site without changing any names in your template files. That would be impossible if your URLs were directly attached to the file names.

WordPress Theme Handbook has a pretty neat article on page templates, and the codex can give you some hints on how to get started with them. Smashing Magazine has an amazing article, written by Nick Schäferhoff, which gives detailed instructions on how to create a page template.

In a nutshell, and taken from WordPress theme Twentyfourteen, a page template works somewhat like this

<?php
/**
 * Template Name: Full Width Page
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

get_header(); ?>

<div id="main-content" class="main-content">

<?php
    if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
        // Include the featured content template.
        get_template_part( 'featured-content' );
    }
?>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();

                    // Include the page content template.
                    get_template_part( 'content', 'page' );

                    // If comments are open or we have at least one comment, load up the comment template.
                    if ( comments_open() || get_comments_number() ) {
                        comments_template();
                    }
                endwhile;
            ?>
        </div><!-- #content -->
    </div><!-- #primary -->
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

Interestingly enough, the comment part Template Name: Full Width Page makes this template global, which means it can be accessed anywhere within your site (take a look at the docs for more detail on hierarchy). Once you have something like that on your template, create a page, and then assign you template to it. You should be golden!

EDIT:

Still in time, check this awesome infographic that shows how templating works in WP land, and how every page eventually renders to index.php, if no other template file is found.

Saturday, May 29, 2021
 
muaaz
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :