<?php
/**
* Plugin Name: Shop Owner – Restrict to Own Products & Media
* Description: Creates a “Shop Owner” role with the ability to manage their own WooCommerce products and media only, and removes access to blog posts, comments, gift vouchers, and the Elementor library.
* Version: 1.0
* Author: Your Name
*/
/**
* 1. On plugin activation, create or update the “Shop Owner” role with the needed capabilities.
*/
function sor_activate_plugin() {
// Capabilities for the Shop Owner role.
$shop_owner_caps = array(
// Basic WordPress capabilities (minimum needed to log in & upload media).
‘read’ => true,
‘upload_files’ => true,
// WooCommerce product capabilities (only their own).
‘publish_products’ => true,
‘edit_products’ => true,
‘delete_products’ => true,
‘read_product’ => true,
‘edit_product’ => true,
‘delete_product’ => true,
‘create_products’ => true,
‘edit_published_products’ => true,
‘delete_published_products’ => true,
// Product taxonomy capabilities (categories, tags, brands, etc.).
‘manage_product_terms’ => true,
‘edit_product_terms’ => true,
‘delete_product_terms’ => true,
‘assign_product_terms’ => true,
);
// If the role doesn’t exist, create it; otherwise update its capabilities.
if ( ! get_role( ‘shop_owner’ ) ) {
add_role( ‘shop_owner’, ‘Shop Owner’, $shop_owner_caps );
} else {
$role = get_role( ‘shop_owner’ );
if ( $role ) {
foreach ( $shop_owner_caps as $cap => $grant ) {
$role->add_cap( $cap, $grant );
}
}
}
// Flush rewrite rules and cache to ensure capability changes take effect immediately.
flush_rewrite_rules();
wp_cache_flush();
}
register_activation_hook( __FILE__, ‘sor_activate_plugin’ );
/**
* 2. On every ‘init’, ensure the Shop Owner role still has all the needed capabilities.
* (This helps if the role was created previously but missing some caps.)
*/
function sor_ensure_capabilities() {
$required_caps = array(
‘read’,
‘upload_files’,
‘publish_products’,
‘edit_products’,
‘delete_products’,
‘read_product’,
‘edit_product’,
‘delete_product’,
‘create_products’,
‘edit_published_products’,
‘delete_published_products’,
‘manage_product_terms’,
‘edit_product_terms’,
‘delete_product_terms’,
‘assign_product_terms’,
);
$role = get_role( ‘shop_owner’ );
if ( $role ) {
foreach ( $required_caps as $cap ) {
if ( ! $role->has_cap( $cap ) ) {
$role->add_cap( $cap );
}
}
}
}
add_action( ‘init’, ‘sor_ensure_capabilities’ );
/**
* 3. Restrict the admin “Products” list so that Shop Owners see only their own products.
*/
function sor_limit_products_to_own( $query ) {
global $pagenow;
// Only modify the query in the admin product list.
if ( is_admin() && ‘edit.php’ === $pagenow && isset( $_GET[‘post_type’] ) && ‘product’ === $_GET[‘post_type’] ) {
// If user is a Shop Owner (and not an Admin), show only their products.
if ( current_user_can( ‘shop_owner’ ) && ! current_user_can( ‘manage_options’ ) ) {
$query->set( ‘author’, get_current_user_id() );
}
}
}
add_action( ‘pre_get_posts’, ‘sor_limit_products_to_own’ );
/**
* 4. Restrict the Media Library to show only the user’s own media.
*/
function sor_limit_media_library_to_own( $query ) {
if ( is_admin() && current_user_can( ‘shop_owner’ ) && ! current_user_can( ‘manage_options’ ) ) {
// Show only attachments by this user.
$query[‘author’] = get_current_user_id();
}
return $query;
}
add_filter( ‘ajax_query_attachments_args’, ‘sor_limit_media_library_to_own’ );
/**
* 5. Prevent Shop Owners from deleting media attachments they did not upload.
*/
function sor_prevent_delete_others_attachments( $caps, $cap, $user_id, $args ) {
if ( ‘delete_post’ === $cap && isset( $args[0] ) ) {
$post = get_post( $args[0] );
if ( $post && ‘attachment’ === $post->post_type ) {
// If it’s someone else’s attachment, disallow.
if ( (int) $post->post_author !== (int) $user_id ) {
$caps[] = ‘do_not_allow’;
}
}
}
return $caps;
}
add_filter( ‘map_meta_cap’, ‘sor_prevent_delete_others_attachments’, 10, 4 );
/**
* 6. Remove menu items for posts, comments, gift vouchers, and Elementor library for Shop Owners.
*/
function sor_remove_unwanted_menus() {
if ( current_user_can( ‘shop_owner’ ) && ! current_user_can( ‘manage_options’ ) ) {
// Hide “Posts” (blog posts)
remove_menu_page( ‘edit.php’ );
// Hide “Comments”
remove_menu_page( ‘edit-comments.php’ );
// Hide gift voucher custom post type
remove_menu_page( ‘edit.php?post_type=msd_gift_voucher’ );
// Hide Elementor library
remove_menu_page( ‘edit.php?post_type=elementor_library’ );
}
}
add_action( ‘admin_menu’, ‘sor_remove_unwanted_menus’, 999 );
<?php
/**
* Plugin Name: Shop Owner – Restrict to Own Products & Media
* Description: Creates a “Shop Owner” role with the ability to manage their own WooCommerce products and media only, and removes access to blog posts, comments, gift vouchers, and the Elementor library.
* Version: 1.0
* Author: Your Name
*/
/**
* 1. On plugin activation, create or update the “Shop Owner” role with the needed capabilities.
*/
function sor_activate_plugin() {
// Capabilities for the Shop Owner role.
$shop_owner_caps = array(
// Basic WordPress capabilities (minimum needed to log in & upload media).
‘read’ => true,
‘upload_files’ => true,
// WooCommerce product capabilities (only their own).
‘publish_products’ => true,
‘edit_products’ => true,
‘delete_products’ => true,
‘read_product’ => true,
‘edit_product’ => true,
‘delete_product’ => true,
‘create_products’ => true,
‘edit_published_products’ => true,
‘delete_published_products’ => true,
// Product taxonomy capabilities (categories, tags, brands, etc.).
‘manage_product_terms’ => true,
‘edit_product_terms’ => true,
‘delete_product_terms’ => true,
‘assign_product_terms’ => true,
);
// If the role doesn’t exist, create it; otherwise update its capabilities.
if ( ! get_role( ‘shop_owner’ ) ) {
add_role( ‘shop_owner’, ‘Shop Owner’, $shop_owner_caps );
} else {
$role = get_role( ‘shop_owner’ );
if ( $role ) {
foreach ( $shop_owner_caps as $cap => $grant ) {
$role->add_cap( $cap, $grant );
}
}
}
// Flush rewrite rules and cache to ensure capability changes take effect immediately.
flush_rewrite_rules();
wp_cache_flush();
}
register_activation_hook( __FILE__, ‘sor_activate_plugin’ );
/**
* 2. On every ‘init’, ensure the Shop Owner role still has all the needed capabilities.
* (This helps if the role was created previously but missing some caps.)
*/
function sor_ensure_capabilities() {
$required_caps = array(
‘read’,
‘upload_files’,
‘publish_products’,
‘edit_products’,
‘delete_products’,
‘read_product’,
‘edit_product’,
‘delete_product’,
‘create_products’,
‘edit_published_products’,
‘delete_published_products’,
‘manage_product_terms’,
‘edit_product_terms’,
‘delete_product_terms’,
‘assign_product_terms’,
);
$role = get_role( ‘shop_owner’ );
if ( $role ) {
foreach ( $required_caps as $cap ) {
if ( ! $role->has_cap( $cap ) ) {
$role->add_cap( $cap );
}
}
}
}
add_action( ‘init’, ‘sor_ensure_capabilities’ );
/**
* 3. Restrict the admin “Products” list so that Shop Owners see only their own products.
*/
function sor_limit_products_to_own( $query ) {
global $pagenow;
// Only modify the query in the admin product list.
if ( is_admin() && ‘edit.php’ === $pagenow && isset( $_GET[‘post_type’] ) && ‘product’ === $_GET[‘post_type’] ) {
// If user is a Shop Owner (and not an Admin), show only their products.
if ( current_user_can( ‘shop_owner’ ) && ! current_user_can( ‘manage_options’ ) ) {
$query->set( ‘author’, get_current_user_id() );
}
}
}
add_action( ‘pre_get_posts’, ‘sor_limit_products_to_own’ );
/**
* 4. Restrict the Media Library to show only the user’s own media.
*/
function sor_limit_media_library_to_own( $query ) {
if ( is_admin() && current_user_can( ‘shop_owner’ ) && ! current_user_can( ‘manage_options’ ) ) {
// Show only attachments by this user.
$query[‘author’] = get_current_user_id();
}
return $query;
}
add_filter( ‘ajax_query_attachments_args’, ‘sor_limit_media_library_to_own’ );
/**
* 5. Prevent Shop Owners from deleting media attachments they did not upload.
*/
function sor_prevent_delete_others_attachments( $caps, $cap, $user_id, $args ) {
if ( ‘delete_post’ === $cap && isset( $args[0] ) ) {
$post = get_post( $args[0] );
if ( $post && ‘attachment’ === $post->post_type ) {
// If it’s someone else’s attachment, disallow.
if ( (int) $post->post_author !== (int) $user_id ) {
$caps[] = ‘do_not_allow’;
}
}
}
return $caps;
}
add_filter( ‘map_meta_cap’, ‘sor_prevent_delete_others_attachments’, 10, 4 );
/**
* 6. Remove menu items for posts, comments, gift vouchers, and Elementor library for Shop Owners.
*/
function sor_remove_unwanted_menus() {
if ( current_user_can( ‘shop_owner’ ) && ! current_user_can( ‘manage_options’ ) ) {
// Hide “Posts” (blog posts)
remove_menu_page( ‘edit.php’ );
// Hide “Comments”
remove_menu_page( ‘edit-comments.php’ );
// Hide gift voucher custom post type
remove_menu_page( ‘edit.php?post_type=msd_gift_voucher’ );
// Hide Elementor library
remove_menu_page( ‘edit.php?post_type=elementor_library’ );
}
}
add_action( ‘admin_menu’, ‘sor_remove_unwanted_menus’, 999 );



Reviews
There are no reviews yet.