Luis Martins
Luis Martins

Reputation: 1481

How to hide or block the WooCommerce generated pages?

When activating WooCommerce, the plugin generates a few pages responsible for outputting the store functionality. This is all good, except I had a few occasions where the client or someone in his behalf deletes this pages. They look like empty pages anyway.

Is there any way I can prevent this from happening, maybe by hiding them from the customer (shop manager role) or block them to further editions?

Upvotes: 1

Views: 758

Answers (2)

Danijel
Danijel

Reputation: 12719

This is the solution that will automatically collect the IDs of the pages generated by Woocommerce. The plugin upon installation creates four pages ( Shop, Cart, Checkout, My Account ), and saves the IDs of that pages in the database inside wp_options table. Option names are:

  • woocommerce_shop_page_id
  • woocommerce_cart_page_id
  • woocommerce_checkout_page_id
  • woocommerce_myaccount_page_id

The example collects the IDs automatically within pre_get_posts action ( that runs before the actual query is executed ), if the current user is not an administrator WC generated pages will not be to retrieved.

The second part is for disabling the edit page link in the admin bar. Because the logged in user still has direct access to pages through the admin bar, wp_before_admin_bar_render action is used to remove edit page link, of course only for WC pages, same as in the first part.

Put this in functions.php, or create a plugin if you want theme independent solution.

add_action( 'pre_get_posts', function( $query ) {
    if ( !is_admin() || !$query->is_main_query() ) return;

    global $pagenow, $post_type;

    if ( $pagenow == 'edit.php' && $post_type == 'page' && !in_array( 'administrator', wp_get_current_user()->roles ) ) {

        $exclude = array_map( 
            function( $item ) { return get_option( $item ); }, 
            array( 'woocommerce_shop_page_id', 'woocommerce_cart_page_id', 'woocommerce_checkout_page_id', 'woocommerce_myaccount_page_id' )
        );

        $query->set( 'post__not_in', array_filter( $exclude ) );

    }

});



add_action( 'wp_before_admin_bar_render', function() {
    if ( is_admin() || !is_page() ) return;

    global $wp_admin_bar;   

    $exclude = array_map( 
        function( $item ) { return get_option( $item ); }, 
        array( 'woocommerce_shop_page_id', 'woocommerce_cart_page_id', 'woocommerce_checkout_page_id', 'woocommerce_myaccount_page_id' )
    );

    if ( in_array( get_queried_object()->ID, $exclude ) && !in_array( 'administrator', wp_get_current_user()->roles ) )
        $wp_admin_bar->remove_menu( 'edit' );

});

Here are some links that have helped me:
http://www.johnparris.com/how-to-hide-pages-in-the-wordpress-admin/
http://wpsnipp.com/index.php/functions-php/exclude-pages-from-admin-edit-pages-list/

Upvotes: 1

Howli
Howli

Reputation: 12469

Add the following code to the functions.php file will hide pages from everyone except admins.

function exclude_pages_from_admin($query) {
    if($query->is_admin) {

        $caps = get_user_meta(get_current_user_id(), 'wp_capabilities', true);
        $roles = array_keys((array)$caps);
        if( ! in_array('administrator', $roles) ){
            $ids = array('53'); // Enter Page ids to restrict here
            $query->query_vars['post__not_in'] = $ids;

            if( in_array(get_the_ID(), $ids) ) {
                wp_redirect(admin_url());
            }
        }
    }
    return $query;
}
add_filter('parse_query', 'exclude_pages_from_admin',99);

This will still show the "Edit Page" option while on the page, but trying to edit it will cause the users to be redirected to the dashboard.

The above was found here and slightly modified, it has been tested and should work

Upvotes: 1

Related Questions