admin管理员组

文章数量:1291320

I would like to open up certain parts of my website where the remaining pages should only be accessible as an administrator on the website.

I have made a maintaince plugin and it all works as it should. However, I have trouble filtering users by which page they are visiting.

I would like to open up a specific product category, as well as all products that belong to this category.

If I am not logged in as an administrator, I can not access the product category and products belonging to it.

My code is below - anyone can see what I'm doing wrong?

Thanks.

<?php

@return void

function ng_maintenance_mode() {
    global $pagenow;

    if ( ! current_user_can('administrator') ) {

        if ( $pagenow !== 'wp-login.php' && ! is_product_category( 232 ) && ! has_term( 232, 'product_cat' ) {

                    header( $_SERVER["SERVER_PROTOCOL"] . ' 503 Service Temporarily Unavailable', true, 503 );
                    header( 'Content-Type: text/html; charset=utf-8' );
                    if ( file_exists( plugin_dir_path( __FILE__ ) . 'views/maintenance.php' ) ) {
                        require_once( plugin_dir_path( __FILE__ ) . 'views/maintenance.php' );
                    }
                    die();

                }

        }

}

add_action( 'wp_loaded', 'ng_maintenance_mode' );

?>

I would like to open up certain parts of my website where the remaining pages should only be accessible as an administrator on the website.

I have made a maintaince plugin and it all works as it should. However, I have trouble filtering users by which page they are visiting.

I would like to open up a specific product category, as well as all products that belong to this category.

If I am not logged in as an administrator, I can not access the product category and products belonging to it.

My code is below - anyone can see what I'm doing wrong?

Thanks.

<?php

@return void

function ng_maintenance_mode() {
    global $pagenow;

    if ( ! current_user_can('administrator') ) {

        if ( $pagenow !== 'wp-login.php' && ! is_product_category( 232 ) && ! has_term( 232, 'product_cat' ) {

                    header( $_SERVER["SERVER_PROTOCOL"] . ' 503 Service Temporarily Unavailable', true, 503 );
                    header( 'Content-Type: text/html; charset=utf-8' );
                    if ( file_exists( plugin_dir_path( __FILE__ ) . 'views/maintenance.php' ) ) {
                        require_once( plugin_dir_path( __FILE__ ) . 'views/maintenance.php' );
                    }
                    die();

                }

        }

}

add_action( 'wp_loaded', 'ng_maintenance_mode' );

?>
Share Improve this question asked Jun 9, 2021 at 2:09 Oscar S.Oscar S. 191 gold badge1 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to replace the following:

if ( ! current_user_can('administrator') ) {

with

if ( ! current_user_can('manage_options') ) {

This is because there's no such capability as administrator. There are a couple of administrator-only capabilities, and manage_options is a reliable one to use.

本文标签: plugin developmentWooCommerce maintaince mode by using php