未捕获错误:调用中的未定义函数wp_get_Current_User()

时间:2019-12-30 作者:Faruk rıza

我的简单插件出现错误:

   // Class wpb_widget ends here
    add_action(\'init\',\'do_stuff\');
    function TopAuthor() {
        $contributor_ids = get_users( array(
            \'fields\'  => \'ID\',
            \'orderby\' => \'post_count\',
            \'order\'   => \'DESC\',
            \'who\'     => \'authors\',
        ) );

        foreach ( $contributor_ids as $contributor_id ) :
            $post_count = count_user_posts( $contributor_id );

            // Move on if user has not published a post (yet).
            if ( ! $post_count ) {
                continue;
            }
            ?>

            <div class="contributor">
                <div class="contributor-info">
                    <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
                    <div class="contributor-summary">
                        <h2 class="contributor-name"><?php echo get_the_author_meta( \'display_name\', $contributor_id ); ?></h2>
                        <p class="contributor-bio">
                            <?php echo get_the_author_meta( \'description\', $contributor_id ); ?>
                        </p>
                        <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
                            <?php printf( _n( \'%d Article\', \'%d Articles\', $post_count, \'twentyfourteen\' ), $post_count ); ?>
                        </a>
                    </div><!-- .contributor-summary -->
                </div><!-- .contributor-info -->
            </div><!-- .contributor -->

        <?php
        endforeach;
    }
    TopAuthor();

    ?>
插件完整代码:

<?php

/*
Plugin Name: Site Plugin for example.com
Description: Site specific code changes for example.com
*/
/* Start Adding Functions Below this Line */


/* Stop Adding Functions Below this Line */


// Register and load the widget
function wpb_load_widget() {
    register_widget( \'wpb_widget\' );
}
add_action( \'widgets_init\', \'wpb_load_widget\' );

// Creating the widget
class wpb_widget extends WP_Widget {

    function __construct() {
        parent::__construct(

// Base ID of your widget
            \'wpb_widget\',

// Widget name will appear in UI
            __(\'WPBeginner Widget\', \'wpb_widget_domain\'),

// Widget description
            array( \'description\' => __( \'Sample widget based on WPBeginner Tutorial\', \'wpb_widget_domain\' ), )
        );
    }

// Creating widget front-end

    public function widget( $args, $instance ) {
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );

// before and after widget arguments are defined by themes
        echo $args[\'before_widget\'];
        if ( ! empty( $title ) )
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];

// This is where you run the code and display the output
        echo __( \'Hello, World!\', \'wpb_widget_domain\' );
        echo $args[\'after_widget\'];
    }

// Widget Backend
    public function form( $instance ) {
        if ( isset( $instance[ \'title\' ] ) ) {
            $title = $instance[ \'title\' ];
        }
        else {
            $title = __( \'New title\', \'wpb_widget_domain\' );
        }
// Widget admin form
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <?php
    }

// Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
        return $instance;
    }
} // Class wpb_widget ends here
add_action(\'init\',\'do_stuff\');
function TopAuthor() {
    $contributor_ids = get_users( array(
        \'fields\'  => \'ID\',
        \'orderby\' => \'post_count\',
        \'order\'   => \'DESC\',
        \'who\'     => \'authors\',
    ) );

    foreach ( $contributor_ids as $contributor_id ) :
        $post_count = count_user_posts( $contributor_id );

        // Move on if user has not published a post (yet).
        if ( ! $post_count ) {
            continue;
        }
        ?>

        <div class="contributor">
            <div class="contributor-info">
                <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
                <div class="contributor-summary">
                    <h2 class="contributor-name"><?php echo get_the_author_meta( \'display_name\', $contributor_id ); ?></h2>
                    <p class="contributor-bio">
                        <?php echo get_the_author_meta( \'description\', $contributor_id ); ?>
                    </p>
                    <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
                        <?php printf( _n( \'%d Article\', \'%d Articles\', $post_count, \'twentyfourteen\' ), $post_count ); ?>
                    </a>
                </div><!-- .contributor-summary -->
            </div><!-- .contributor-info -->
        </div><!-- .contributor -->

    <?php
    endforeach;
}
TopAuthor();

?>
我编辑了这样的代码:

} // Class wpb_widget ends here
add_action(\'plugins_loaded\',\'do_stuff\');
function TopAuthor() {
    $contributor_ids = get_users( array(
        \'fields\'  => \'ID\',
        \'orderby\' => \'post_count\',
        \'order\'   => \'DESC\',
        \'who\'     => \'authors\',
    ) );

    foreach ( $contributor_ids as $contributor_id ) :
        $post_count = count_user_posts( $contributor_id );

        // Move on if user has not published a post (yet).
        if ( ! $post_count ) {
            continue;
        }
        ?>

        <div class="contributor">
            <div class="contributor-info">
                <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
                <div class="contributor-summary">
                    <h2 class="contributor-name"><?php echo get_the_author_meta( \'display_name\', $contributor_id ); ?></h2>
                    <p class="contributor-bio">
                        <?php echo get_the_author_meta( \'description\', $contributor_id ); ?>
                    </p>
                    <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
                        <?php printf( _n( \'%d Article\', \'%d Articles\', $post_count, \'twentyfourteen\' ), $post_count ); ?>
                    </a>
                </div><!-- .contributor-summary -->
            </div><!-- .contributor-info -->
        </div><!-- .contributor -->

    <?php
    endforeach;
}
function do_stuff()
{
    TopAuthor();
}

?>
调试日志:

[31-Dec-2019 13:50:26 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_author_permastruct() on null in /home/deniztas/public_html/themeforest-deneme/wp-includes/author-template.php:346
Stack trace:
#0 /home/deniztas/public_html/themeforest-deneme/wp-content/plugins/Top-Author/Top-Author.php(103): get_author_posts_url(\'1\')
#1 /home/deniztas/public_html/themeforest-deneme/wp-content/plugins/Top-Author/Top-Author.php(77): TopAuthor()
#2 /home/deniztas/public_html/themeforest-deneme/wp-includes/class-wp-hook.php(288): do_stuff(\'\')
#3 /home/deniztas/public_html/themeforest-deneme/wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters(NULL, Array)
#4 /home/deniztas/public_html/themeforest-deneme/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#5 /home/deniztas/public_html/themeforest-deneme/wp-settings.php(392): do_action(\'plugins_loaded\')
#6 /home/deniztas/public_html/themeforest-deneme/wp-config.php(101): require_once(\'/home/deniztas/...\')
#7 /home/deniztas/public_html/themeforest-deneme/wp-load.php(37): require_once(\'/home/de in /home/deniztas/public_html/themeforest-deneme/wp-includes/author-template.php on line 346
功能。php:

// Write or Paste your Functions

function your_prefix_get_meta_box( $meta_boxes ) {
    $prefix = \'prefix-\';

    $meta_boxes[] = array(
        \'id\' => \'untitled\',
        \'title\' => esc_html__( \'Untitled Metabox\', \'metabox-online-generator\' ),
        \'post_types\' => array(\'post\', \'page\' ),
        \'context\' => \'advanced\',
        \'priority\' => \'default\',
        \'autosave\' => \'false\',
        \'fields\' => array(
            array(
                \'id\' => $prefix . \'image_advanced_1\',
                \'type\' => \'image_advanced\',
                \'name\' => esc_html__( \'Image Advanced\', \'metabox-online-generator\' ),
            ),
        ),
    );

    return $meta_boxes;
}
add_filter( \'rwmb_meta_boxes\', \'your_prefix_get_meta_box\' );


  // Top Authors-->

function twentyfourteen_list_authors() {
    $contributor_ids = get_users( array(
        \'fields\'  => \'ID\',
        \'orderby\' => \'post_count\',
        \'order\'   => \'DESC\',
        \'who\'     => \'authors\',
    ) );

    foreach ( $contributor_ids as $contributor_id ) :
        $post_count = count_user_posts( $contributor_id );

        // Move on if user has not published a post (yet).
        if ( ! $post_count ) {
            continue;
        }
        ?>

        <div class="contributor">
            <div class="contributor-info">
                <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
                <div class="contributor-summary">
                    <h2 class="contributor-name"><?php echo get_the_author_meta( \'display_name\', $contributor_id ); ?></h2>
                    <p class="contributor-bio">
                        <?php echo get_the_author_meta( \'description\', $contributor_id ); ?>
                    </p>
                    <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
                        <?php printf( _n( \'%d Article\', \'%d Articles\', $post_count, \'twentyfourteen\' ), $post_count ); ?>
                    </a>
                </div><!-- .contributor-summary -->
            </div><!-- .contributor-info -->
        </div><!-- .contributor -->

    <?php
    endforeach;
}

            // /Top Authors -->

1 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

你必须打电话TopAuthor() 中的函数plugins_loadedinit 行动您正在Fatal error: Uncaught Error: Call to undefined function wp_get_current_user() 因为wp_get_current_user() 是一个可插入的函数。包含插件时不能调用。你必须等待行动plugins_loadedinit. 致电您的TopAuthor() 函数使用initplugins_loaded 行动

init action:

add_action(\'init\',\'do_stuff\');

plugins_loaded action:

add_action(\'plugins_loaded\',\'do_stuff\');

Your function :

function do_stuff()
{
    TopAuthor();
}

So full code of Your Plugin file look like:

<?php
/*
Plugin Name: Site Plugin for example.com
Description: Site specific code changes for example.com
*/
/* Start Adding Functions Below this Line */

/* Stop Adding Functions Below this Line */

// Register and load the widget
function wpb_load_widget() {
    register_widget( \'wpb_widget\' );
}
add_action( \'widgets_init\', \'wpb_load_widget\' );

// Creating the widget
class wpb_widget extends WP_Widget {

    function __construct() {
        parent::__construct(

            // Base ID of your widget
            \'wpb_widget\',

            // Widget name will appear in UI
            __(\'WPBeginner Widget\', \'wpb_widget_domain\'),

            // Widget description
            array( \'description\' => __( \'Sample widget based on WPBeginner Tutorial\', \'wpb_widget_domain\' ), )
        );
    }

    // Creating widget front-end
    public function widget( $args, $instance ) {
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );

        // before and after widget arguments are defined by themes
        echo $args[\'before_widget\'];
        if ( ! empty( $title ) )
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];

        // This is where you run the code and display the output
        echo __( \'Hello, World!\', \'wpb_widget_domain\' );
        echo $args[\'after_widget\'];
    }

    // Widget Backend
    public function form( $instance ) {
        if ( isset( $instance[ \'title\' ] ) ) {
            $title = $instance[ \'title\' ];
        }
        else {
            $title = __( \'New title\', \'wpb_widget_domain\' );
        }
        // Widget admin form
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <?php
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
        return $instance;
    }
} // Class wpb_widget ends here


//add_action(\'init\',\'do_stuff\');
add_action(\'plugins_loaded\',\'do_stuff\');
function do_stuff()
{
    TopAuthor();
}

function TopAuthor() {
    $contributor_ids = get_users( array(
        \'fields\'  => \'ID\',
        \'orderby\' => \'post_count\',
        \'order\'   => \'DESC\',
        \'who\'     => \'authors\',
    ) );

    foreach ( $contributor_ids as $contributor_id ) :
        $post_count = count_user_posts( $contributor_id );
        // Move on if user has not published a post (yet).
        if ( ! $post_count ) {
            continue;
        }
        ?>
        <div class="contributor">
            <div class="contributor-info">
                <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
                <div class="contributor-summary">
                    <h2 class="contributor-name"><?php echo get_the_author_meta( \'display_name\', $contributor_id ); ?></h2>
                    <p class="contributor-bio">
                        <?php echo get_the_author_meta( \'description\', $contributor_id ); ?>
                    </p>
                    <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
                        <?php printf( _n( \'%d Article\', \'%d Articles\', $post_count, \'twentyfourteen\' ), $post_count ); ?>
                    </a>
                </div><!-- .contributor-summary -->
            </div><!-- .contributor-info -->
        </div><!-- .contributor -->

    <?php
    endforeach;
}
?>
我已经测试了它的运行,没有任何错误。

激活的插件:https://prnt.sc/qhu87lRegisterd小部件:https://prnt.sc/qhu8bj

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'