0

I want to hide my plugin on admin pages because its conflict with that.however when İ add the control, wp says there is critical error.

İ want to add this control:

if ( ! is_admin() ) {
    // Runs only if this PHP code is in a file that displays outside the admin panels, like the theme template.
    echo '<div style="text-align: center">Welcome to our website.</div>';
} else {
    // Runs only if this code is in a file that displays inside the admin panels, like a plugin file.
    Return;
}

The text code which stats welcome to.. must change with topauthor function.İ did but it cause critical error.how can i do it?

Here is the full code before editing:

<?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
//eklediğim kod

add_action('wp_default_scripts', function ($scripts) {
    if (!empty($scripts->registered['jquery'])) {
        $scripts->registered['jquery']->deps = array_diff($scripts->registered['jquery']->deps, ['jquery-migrate']);
    }
});

//add_action('init','do_stuff');
add_action('wp_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;
}
?>

İ am writing from mobile so please edit my english syntax.thanks

4
  • 1
    Where in the second block of code are you trying to add this condition? And what is the specific "critical error"? Commented Jan 13, 2020 at 2:36
  • @JacobPeattie all of "funtion TopAuthor".there is no error code on the page.İ will look at the logs file if i find them Commented Jan 13, 2020 at 2:45
  • Why is TopAuthor() run during wp_loaded? That makes no sense. Commented Jan 13, 2020 at 2:55
  • So must i delete it? can you edit the code for me? Commented Jan 13, 2020 at 2:58

1 Answer 1

0

Well i think you just do everything inside of TopAuthor and

add_action( 'admin_menu', 'TopAuthor' );
4
  • no, I want to hide the plugin output on admin page Commented Jan 13, 2020 at 12:22
  • so in this case you use send_headers instead admin_menu , therefore if (is_add() ) ?...
    – demopix
    Commented Jan 13, 2020 at 12:27
  • function do_stuff() { if( ! is_admin() ) TopAuthor(); }
    – demopix
    Commented Jan 13, 2020 at 12:40
  • add_action('wp_loaded','do_stuff'); function do_stuff() { if (!is_admin()) TopAuthor(); } Commented Jan 13, 2020 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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