Reorganization WordPress boxes in admin area - php

Actually, I have two questions here.
1: How to hide some boxes in WordPress area (for example, categories box or comments box) for subscribers?
2: How to sort categories in category box by id (looks like, by default, it sorted by name). For example, I would like to see
the next order: 1, 2, 3...
but not 10, 1, 11

For Question-2,
Please Try below code.
function sort_get_terms_args( $args, $taxonomies ) {
global $pagenow;
if( !is_admin() || ('post.php' != $pagenow && 'post-new.php' != $pagenow) )
return $args;
$args['orderby'] = 'term_id';
$args['order'] = 'ASC';
return $args;
}
add_filter( 'get_terms_args', 'sort_get_terms_args', 10, 2 );
Hope this will helpful for you !

For Question-1,
Add code as per below in your active theme's functions.php file
function my_remove_meta_boxes() {
if ( ! current_user_can( 'manage_options' ) ) {
/**
* arg-1 : id of the metabox, arg-2: page or posttype, arg-3: screen where the boxes displaying
*/
remove_meta_box( 'tagsdiv-stages', 'post', 'side' ); // as per my page https://www.screencast.com/t/VCnvf61M7ydS
remove_meta_box( 'commentsdiv', 'post', 'normal' );
}
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
You can refer : https://codex.wordpress.org/Function_Reference/remove_meta_box

Related

Change default sorting in Woocommerce order list

How can the default sorting of the Woocommerce order list be changed. For now each time I enter the order page, orders are displayed as DESC. I would like it to be ASC by default each time I enter the order page. Can anyone help?
As I found another custom order solution in the forum:
sort-orders-by-paid-date-in-woocommerce-admin-order-list
I edited the code mentioned in there to the following, which worked out:
function action_parse_query( $query ) {
global $pagenow;
// Initialize
$query_vars = &$query->query_vars;
// Only on WooCommerce admin order list
if ( is_admin() && $query->is_main_query() && $pagenow == 'edit.php' && $query_vars['post_type'] == 'shop_order' ) {
// Set
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'parse_query', 'action_parse_query', 10, 1 );
These code will do the work.
add_filter( 'woocommerce_order_query_args', function ( $args ) {
$args['order'] = 'ASC';
return $args;
} );

WordPress how to exclude certain categories via functions.php

I'm trying to exclude certain categories and all user archives
e.g.
https://example.com/category/slug
https://example.com/author/name
From my WordPress-Site via functions.php, rerouting them to home.
This is what I've tried so far:
/* Route categories to home */
function reroute_wp_categories(){
if( is_category( array('category1', 'category2', 'category3' )) || is_author() ) {
global $wp_query;
$wp_query->is_home(); //set to Home
}
}
with category x being the category slug.
This doesn't work at all and I'm running out of ideas.
Grateful for any help/ideas.
This should do the trick. In your functions.php add the following
function reroute_wp_categories( $query ) {
if ( is_author() || is_category(array('category1', 'category2', 'category3' )) ) {
wp_redirect(home_url());
exit();
}
}
add_action( 'pre_get_posts', 'reroute_wp_categories' );

How to add a custom field to the Edit Order page

I've been googling and searching here for a simple way to add an empty field box to the Edit Orders page. We will use this to put in a reference for the shipment from our courier.
We would add it to the order notes, but we want it to be searchable, and also want to add it as a column in the Orders Admin page (I have Admin Columns plugin that I think can do that bit, I just need to add this field to start).
Hope someone can help, thanks!
EDIT:
I found this question which seems to be similar, but more complicated, than what I am looking for and I cant figure out how to simplify it. Add a custom meta box on order edit pages and display it on the customer order pages
I don't need anything that will display on the front end to customers like this. Just a simple empty box that displays on every order-edit page (perhaps below the order notes) that can be searched. I will then display it in a column on the order admin page too.
Managed to get an answer, this is working great!
//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
// For displaying in columns.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'custom_column' :
echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
break;
}
}
// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_column',
__( 'Custom Column', 'your_text_domain' ),
'shop_order_display_callback',
'shop_order'
);
}
// For displaying.
function shop_order_display_callback( $post ) {
$value = get_post_meta( $post->ID, 'custom_column', true );
echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}
// For saving.
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
}
// Make sure that it is set.
if ( ! isset( $_POST['custom_column'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['custom_column'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_column', $my_data );
}
add_action( 'save_post', 'save_shop_order_meta_box_data' );
Use the default WooCommerce custom field in the order edit page bottom section.

WordPress Add custom column to custom post type

I have made a post voting plugin in WordPress and a custom post type. Now, I am trying to add a Votes column to my custom post type which show total number of votes of that post.Before that I have tried it on WordPress default Post and it worked but Its not working with custom post type.
In functions.php
// Customizing WordPress admin to show post votes
add_filter( 'manage_edit-post_columns', 'voteme_extra_post_columns' );
add_filter( 'manage_child_contest_clips_posts_columns','voteme_extra_post_columns' );
function voteme_extra_post_columns( $columns )
{
$columns[ 'votemecount' ] = __( 'Votes' );
return $columns;
}
function voteme_post_column_row( $column )
{
if ( $column != 'votemecount' )
return;
global $post;
$post_id = $post->ID;
$votemecount = get_post_meta($post_id, '_votemecount', true) != '' ?
get_post_meta($post_id, '_votemecount', true) : '0';
echo $votemecount;
}
add_action( 'manage_posts_custom_column', 'voteme_post_column_row', 10, 2 );
add_action( 'manage_child_contest_clips_posts_custom_column', 'voteme_post_column_row',
10,2);

How to hide specific page (from wp-admin) for certain users in wp?

My Image
I just wanted to hide specific page for certain users.
function remove_menus(){
// get current login user's role
$roles = wp_get_current_user()->roles;
// test role
if( in_array('administrator',$roles)){
remove_menu_page( 'edit-comments.php' ); //Posts
remove_menu_page( 'tools.php' );
remove_menu_page('edit.php');
remove_menu_page('wpcf7');
}
}
add_action( 'admin_menu', 'remove_menus' , 100 );
This what I am tried upto now and its working fine for all the page.
My question is I dont want to show home - Front page (Please see my image) If logged in user is not admin. and also I want to hide add new
You can use user's role capabilities and allow on the basis of role for add new items.
function manage_user_action() {
// get current login user's role
$roles = wp_get_current_user()->roles;
if( !in_array('administrator',$roles)){
//remove capabilities
$roles->remove_cap( 'edit_pages');
}
}
add_action( 'admin_init', 'manage_user_action');
To Remove Page from list
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '10'); // Enter your page IDs here
//don't forget to the query
return $query;
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
For more help see this link : Click Here
I have extended #dineshkashera answer to target a specific user by ID and the code should be as follows :
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 2 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '11', '12','13' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
This code will get the user with ID of 2 and remove the pages IDs 11,12 and 13 from their WP page edit screen.

Categories