0

My simple plugin gives error:

   // 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();

    ?>

full code of plugin:

<?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();

?>

I edited code like that:

} // 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();
}

?>

debug log:

[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

function.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 -->
  • 2
    Hello! Is this code snippet complete? My understanding of this is that the TopAuthor function gets immediatley executed on all page requests, REST API endpoints, AJAX requests, etc, in which case you can't do that, some API calls have to happen after certain events have fired, you're calling them before they're ready – Tom J Nowell Dec 30 '19 at 23:35
  • @TomJNowell I added all code.Can you look at again please? when I delete " TopAuthor(); " there is no error. – Faruk rıza Dec 30 '19 at 23:49
  • Is there a reason you've included TopAuthor rather than calling it in a template or adding it as a shortcode? Even without the fatal error, your code would break so many other things as is – Tom J Nowell Dec 31 '19 at 0:11
0

You have to call TopAuthor() function in plugins_loaded orinit action. You are getting Fatal error: Uncaught Error: Call to undefined function wp_get_current_user() because wp_get_current_user() is a pluggable function. You can not call when your plugin is included. You have to wait for the action plugins_loaded or init. Call your TopAuthor() function using init or plugins_loaded action.

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

I have tested it runs without any error.

Activated plugin : https://prnt.sc/qhu87l Registerd widget : https://prnt.sc/qhu8bj

| improve this answer | |
  • now, wp says crtica error.I editted code as edit in question – Faruk rıza Dec 31 '19 at 8:58
  • I have updated my answer with full code of file. check it out. and what kind of error are you getting ? – Chetan Vaghela Dec 31 '19 at 9:11
  • the error message is bottom of the picture : ibb.co/TqnxMww Also I have same snippet in fuction.php of my theme so is possible it to be source of the problem? function.php: – Faruk rıza Dec 31 '19 at 13:03
  • ' // Top Authors--> sfunction twentyfourteen_list_authors() { //here is same with plugin... <?php endforeach; } // /Top Authors -->' – Faruk rıza Dec 31 '19 at 13:07
  • 1
    it works, thank you – Faruk rıza Dec 31 '19 at 14:31

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.