after some trial and error I have managed to edit some of my shop_manager roles. See below.
However, when I remove_cap( 'manage_woocommerce'); it removes the menu, but then it adds 2 new menus for 'orders' and 'discount voucher'. I seem unable to remove these 2 items as they are new in the menu, but I cannot find the relevant property. Does anybody know how to remove these?
(all I want is the shop manager to add and edit products, nothing else)
$result = add_role(
'shop_manager',
__( 'Shop Manager' ),
array(
$role = get_role( 'shop_manager' ),
$role->remove_cap( 'edit_published_posts' ),
$role->remove_cap( 'edit_published_pages' ),
$role->remove_cap( 'delete_published_pages' ),
$role->remove_cap( 'delete_published_posts' ),
$role->remove_cap( 'edit_theme_options' ),
$role->remove_cap( 'view_woocommerce_reports' ),
$role->remove_cap( 'moderate_comments' ),
$role->remove_cap( 'manage_woocommerce' ),
$role->remove_cap( 'manage_woocommerce_orders' ), // does not seem to work
$role->remove_cap( 'manage_woocommerce_coupons' ), //does not seem to work
)
);
I already looked at the following sources but cannot figure it out:
https://github.com/woocommerce/woocommerce/blob/250fcc96a3ea4b1150eb3e94fa45e968ce4340aa/includes/class-wc-install.php#L942
You can change role and capabilities of each user, see an example below for shop_manager.
Please note that when you run this in functions.php these will be stored in your database. In order to change it back you need to add/remove. Simply removing the code won't undo this as it's stored in the database.
$result = add_role(
'shop_manager',
__( 'Shop Manager' ),
array(
$role = get_role( 'shop_manager' ),
$role->remove_cap( 'edit_published_posts' ),
$role->remove_cap( 'edit_published_pages' ),
$role->remove_cap( 'delete_published_pages' ),
$role->remove_cap( 'delete_published_posts' ),
$role->add_cap( 'edit_posts' ),
$role->add_cap( 'edit_pages' ),
$role->remove_cap( 'edit_woocommerce_coupons' ),
$role->remove_cap( 'edit_shop_coupons' ),
$role->remove_cap( 'export' ),
)
);
Related
I have added this multi-check field in WordPress user profile page
$tprofile_user_fight_info->add_field(
array(
'id' => '_user-fighting-style',
'name' => __( 'Your fighting styles', 'tprofile' ),
'type' => 'multicheck',
'options' => array(
'fs_one' => __('MMA', 'tprofile' ),
'fs_two' => __( 'Kikboxing', 'tprofile' ),
'fs_three' => __( 'Boxing', 'tprofile' ),
'fs_four' => __( 'Judo', 'tprofile' ),
'fs_five' => __( 'Taekwondo', 'tprofile'),
'fs_six' => __( 'Karate', 'tprofile' ),
'fs_seven' => __( 'Brazilian Jiu-jitsu', 'tprofile' ),
'fs_eight' => __( 'Wrestling', 'tprofile' ),
'fs_nine' => __( 'Capoeira', 'tprofile' ),
'fs_ten' => __( 'Muay Thai', 'tprofile' ),
'fs_eleven' => __('Karate Kyokushinkai', 'tprofile' ),
'fs_twelve' => __('Kudo Daido Juku', 'tprofile' ),
)
)
);
And I am trying retrieve the checked values in wp_list_table. But in the table there showing the keys instead of values. Please help me, how can I get the values in the table? Below is the code where I trying to retrieve the values:
$tprofile_users = get_users( array( 'role__in' => array( 'subscriber' ) ) );
foreach( $tprofile_users as $tprofile_user ){
global $current_user;
$user_id = $current_user->ID;
$tprofile_user_cmeta = get_the_author_meta( $user_id );
$data_array[] = [
'fighting_style' => esc_html( $tprofile_user->$tprofile_user_cmeta['_user-fighting-style'][0] ),
}
return $data_array;
Result is showing like this:
a:3:{i:0;s:6:"fs_one";i:1;s:7:"fs_four";i:2;s:8:"fs_eight";}
I created my custom fields through code and then I registered it with this code:
/**
* Content blocks for all pages
*/
$location_page = new ACF_Group_Location;
$location_page->add_rule( 'post_type', 'page');
$location_slots = new ACF_Group_Location;
$location_slots->add_rule( 'post_type', 'slots' );
$location_card = new ACF_Group_Location;
$location_card->add_rule( 'post_type', 'card' );
ACF_Group::create( 'content-blocks', __( 'Content blocks', 'dw' ) )
->add_location( $location_page )
->add_location( $location_slots )
->add_location( $location_card )
->set_attr( 'label_placement', 'top' )
->set_attr( 'hide_on_screen', array( 'the_content', 'comments' ) )
->add_fields( array(
array(
'label' => __( 'Content blocks', 'dw' ),
'name' => 'cb',
'type' => 'flexible_content',
'button_label' => __( 'Add content block', 'dw' ),
'layouts' => $cb
),
)
)->register();
add_rule() includes:
public function add_rule( $param, $value, $operator = '==' ) {
$this->rules[] = compact( 'param', 'value', 'operator' );
return $this;
}
and my question is what code do I need to add if I wanna disable them on my page template?
So you're using ACF programatically using this
instead of adding your custom fields to all pages and then try to disable it for a particular template just add your custom field group to the template you need.
<?php
add_action( 'register_acf_groups', 'my_theme_register_acf_groups' );
function mytheme_register_acf_groups() {
ACF_Group::create( 'my_group', 'My Group' )
->add_location_rule( 'post_type', 'page' )
->add_location_rule( 'page_template', 'template-contact.php' ) // <- see here
->set_attr( 'label_placement', 'left' )
->add_fields(array(
array(
'label' => 'Test Field',
'name' => 'test_field',
'type' => 'text'
),
))
->register();
}
How do I add color settings to the Gutenberg Header block? I've added theme support for paragraphs with this...
function apache_theme_supported_features() {
add_theme_support( 'editor-color-palette', array(
array(
'name' => esc_html__( 'One', '##textdomain' ),
'slug' => 'one',
'color' => esc_html( get_theme_mod( 'one', '#e63946' ) ),
),
array(
'name' => esc_html__( 'Two', '##textdomain' ),
'slug' => 'two',
'color' => esc_html( get_theme_mod( 'two', '#97efe9' ) ),
),
) );
}
Currently this feature is not supported by the new editor - while there are more and more support requests asking for it.
Yeah, one of the bad news related to the new WP editor.
The main support ticket is here: https://github.com/WordPress/gutenberg/issues/6012
Please comment it, upvote the comments and contribute to make this feature a high priority.
I want to add product meta in my Online Cosmetics Store like the above screenshot.
This meta data is exists in an opencart website shop4shops.in. I want to add the same meta data in my Woocommerce store.
Your help, will be appreciated.
i will give you small demo for how to add meta for product.
in function.php file add following code.
add_filter( 'rwmb_meta_boxes', 'pharmacy_meta_boxes' );
function pharmacy_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'title' => __( 'Extra Product Info', 'pharmacy' ),
'fields' => array(
array(
'id' => 'unit',
'name' => __( 'Unit', 'pharmacy' ),
'type' => 'text',
'datalist' => array(
'options' => array(
__( 'Box', 'pharmacy' ),
__( 'Blister pack', 'pharmacy' ),
__( 'Packet', 'pharmacy' ),
__( 'Bottle', 'pharmacy' ),
),
),
),
array(
'id' => 'dosage_form',
'name' => __( 'Dosage form', 'pharmacy' ),
'type' => 'text',
'datalist' => array(
'options' => array(
__( 'Capsule', 'pharmacy' ),
__( 'Tablet', 'pharmacy' ),
__( 'Liquid', 'pharmacy' ),
__( 'Ointment', 'pharmacy' ),
),
),
),
);
return $meta_boxes;
}
And to save that meta write following in your function
add_action( 'woocommerce_product_meta_end', 'pharmacy_extra_info' );
function pharmacy_extra_info()
{
if ( $meta = rwmb_meta( 'unit' ) )
{
echo '<strong>' . __( 'Unit:', 'pharmacy' ) . "</strong> $meta<br>";
}
if ( $meta = rwmb_meta( 'dosage_form' ) )
{
echo '<strong>' . __( 'Dosage form:', 'pharmacy' ) . "</strong> $meta<br>";
}
}
I hope this following code help you to add meta in your product and you can add more product meta like this way.
and you can also follow
this link
Thank You.
I have created a custom post type named banners. Thereby I register a new taxonomy called location that specifies on which page the banner is to be shown. Everything is great however when I click on the custom posts type 'Banner' in the admin window I see all the banners created however the table does not have a column for the taxonomy 'Location'.
In other words I want to be able to see what location the banner is in, in the banners listing.
For those interested, the register_taxonomy register_taxonomy function, as of WordPress 3.5, now offers an argument for show_admin_column (false by default). Set to true, it automatically displays the taxonomy column in the admin.
You can use the manage_post-type_custom_column and manage_edit_post-type_columns filters to add your taxonomy column to the post type listing.
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
add_filter( 'manage_edit-banner_columns', 'my_new_custom_post_column');
add_action( 'manage_banner_custom_column', 'location_tax_column_info', 10, 2);
}
function my_new_custom_post_column( $column ) {
$column['location'] = 'Location';
return $column;
}
function location_tax_column_info( $column_name, $post_id ) {
$taxonomy = $column_name;
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, $taxonomy);
if (!empty($terms) ) {
foreach ( $terms as $term )
$post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
echo join('', $post_terms );
}
else echo '<i>No Location Set. </i>';
}
//what version of wordpress you are using
//since wp 3.5 you can pass parameter show_admin_column=>true
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );
// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'book' ), $args );
}
In addition to Jonathan Wold answer, you can add the ability to filter by your custom taxonomy like this:
add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);
function filter_post_type_backend_by_taxonomies($post_type, $which) {
if( 'your_post_type' !== $post_type) {
return;
}
$taxonomies = array( 'your_custom_taxonomy' );
foreach( $taxonomies as $taxonomy_slug ) {
$taxonomy_obj = get_taxonomy( $taxonomy_slug );
$taxonomy_name = $taxonomy_obj->labels->name;
$terms = get_terms($taxonomy_slug);
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}
}