最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - I cant add is_admin control to plugin

programmeradmin3浏览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
Description: Site specific code changes for example
*/
/* 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

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
Description: Site specific code changes for example
*/
/* 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

Share Improve this question asked Jan 13, 2020 at 1:24 Faruk rızaFaruk rıza 982 silver badges11 bronze badges 4
  • 1 Where in the second block of code are you trying to add this condition? And what is the specific "critical error"? – Jacob Peattie 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 – Faruk rıza Commented Jan 13, 2020 at 2:45
  • Why is TopAuthor() run during wp_loaded? That makes no sense. – Jacob Peattie Commented Jan 13, 2020 at 2:55
  • So must i delete it? can you edit the code for me? – Faruk rıza Commented Jan 13, 2020 at 2:58
Add a comment  | 

1 Answer 1

Reset to default 0

Well i think you just do everything inside of TopAuthor and

add_action( 'admin_menu', 'TopAuthor' );
发布评论

评论列表(0)

  1. 暂无评论