Remove Wordpress Plugin functionality from admin side - php

I'm using a WordPress plugin https://github.com/lesterchan/wp-postratings.
It also showing ratings on admin, when i visit http://domain.com/wp-admin/edit.php.
How do i remove those ratings from admin side.

You can use the below function in your functions.php file with the manage_posts_columns filter. I'm assuming your custom post type id 'tools' and the column is referenced by 'ratings'. If they are different you can just change that in the code.
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
//assuming the id for the custom post type is 'tools'
case 'tools':
unset(
//I'm using 'ratings' but you'll have to check and see what the name for the column really is.
$columns['ratings']
);
break;
}
return $columns;
}

Related

Woocommerce - Include custom user meta in order 'filter by registered customer' Ajax search

I am trying to include a users custom meta named 'sageaccountnumber' within the 'filter by registered customer' section of the WooCommerce orders list as shown below:
I already have the custom meta field named 'sageaccountnumber' working with the following PHP code:
add_action( 'personal_options_update', 'sab_save_sageaccount_user_profile_fields' );
add_action( 'edit_user_profile_update', 'sab_save_sageaccount_user_profile_fields' );
function sab_save_sageaccount_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'sageaccountnumber', $_POST['sageaccountnumber'] );
}
When searching for a registered customer I would like to include the user meta 'sageaccountnumber' within the search and display matching results.
I understand this uses AJAX within the file class-wc-ajax.php. This is the function in question: https://wp-kama.com/plugin/woocommerce/function/WC_AJAX::json_search_customers
I do not know alot about AJAX and I have not been able to find a way to include a custom user meta value in this search. I have not found anyone else doing this.
Any guidance or suggestions would be much appreciated? Thank you.
After getting lost with trying to understand the class-wc-ajax.php file, I completely missed a more obvious and simple solution. Here is the code I am using which works perfectly. I hope this helps others looking for similar solution.
This filter-hook allows you to add an additional meta_query to the existing search parameters. This will also allow your custom user meta to display in AJAX searches from the Orders & Subscriptions pages.
If you have a custom meta field setup for your users, simply change 'sageaccountnumber' to the name of your meta and it will be included in the AJAX search results.
add_filter ('woocommerce_customer_search_customers', 'sab_sageaccount_order_subscription_search', 10, 4);
function sab_sageaccount_order_subscription_search ($filter, $term, $limit, $type){
if ($type == 'meta_query'){ $filter['meta_query'][] = array('key' => 'sageaccountnumber', 'value' => $term, 'compare' => 'LIKE');
}
return $filter;
}
If you need to include the custom meta field in the normal users search, you can use the following code, again changing 'sageaccountnumber' to your custom meta name.
add_action( 'pre_user_query', 'sab_sageaccount_user_search' );
function sab_sageaccount_user_search( $query ) {
global $wpdb;
global $pagenow;
if (is_admin() && 'users.php' == $pagenow) {
if( empty($_REQUEST['s']) ){return;}
$query->query_fields = 'DISTINCT '.$query->query_fields;
$query->query_from .= ' LEFT JOIN '.$wpdb->usermeta.' ON '.$wpdb->usermeta.'.user_id = '.$wpdb->users.'.ID';
$query->query_where = "WHERE 1=1 AND (user_login LIKE '%".$_REQUEST['s']."%' OR ID = '".$_REQUEST['s']."' OR (meta_value LIKE '%".$_REQUEST['s']."%' AND meta_key = 'sageaccountnumber'))";
}
return $query;
}

Add Custom Templates to Custom Post Type Taxonomies

I created a Custom Post Type, and it has some categories in it.
I created forms with User Frontend Pro for CPT categories.
I’m looking for a solution for assigning the custom templates to that records to show them on the website.
For example;
Assume that I have a CPT named Project.
The project post type has two categories; “Business” and “Ideas”
Users can post their entries with forms to these categories and their posts listed under the account dashboard.
The issue is that;
Business categories should be shown with the category-business.php custom template, and in a similar way, the Ideas category should be shown with category-ideas.php.
How can I add custom template to CPT categories?
P.S. I tried the template hierarchy solution that is in the WP documents, but it's not working. Because there is a custom view function for content. I looking for a code snippet that adds the template page attributes to custom posts automatically, if it is possible.
Assuming that you are just using the default WordPress category you can the pre_get_posts Hook to include the project as part of the category page if they are not showing by default
function include_project_to_cat_pages( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'project' ) );
}
}
add_action( 'pre_get_posts', 'include_project_to_cat_pages' );
I solved that issue with a filter.
add_filter( 'single_template', function( $template ) {
global $post;
$cat = get_the_category()[0];
if ( $post->post_type === 'project' && $cat->slug === 'business') {
$locate_template = locate_template( "custom-business-template.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
Thank you to all contributors and the community.

Limit users in WordPress to access defined post with custom post type

I building one plugin what need to allow admins to exclude users from reading some special contents using post ID and custom post type.
Generaly, if some post have ID 99 with custom post type lesions and admin choose that to exclude, user get 404 if go on direct link and also not see that post in list.
NOTE: I don't need to made loop, I need to somehow affect with other plugin on WP_Query with particular custom post type via some hook, filter or action.
I did not get the time to test it yet. But it should be working. The idea is to save the post ids and post types in wp_options. Then you can get those values and save them in an array. After that you compare them with current post and do the action that you want. You can put this in functions.php
function sr_excluded_users()
{
global $post;
global $wp_query;
$exclusive_post_ids = array(); // List of post ids ( you can store it in wp options )
$exclusive_post_types = array(); // List of custom post types ( you can store it in wp options )
if( in_array( $post->ID, $exclusive_post_ids ) && in_array( $post->post_type, $exclusive_post_types ) )
{
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
}
}
add_action( 'wp', 'sr_excluded_users' );
Edit : Function to remove posts from loop
function sr_excluded_users_loop($query)
{
$exclusive_post_ids = array(); // List of post ids ( you can store it in wp options )
$query->set( 'post__not_in' , $exclusive_post_ids );
}
add_action( 'pre_get_posts', 'sr_excluded_users_loop' );

Modify WooCommerce product columns

I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!
I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}

Add specific anchors under posts title

I'm new in wordpress plugins development , I'd like to create a plugin do some special edits on posts after created. I want to add a link (anchor) under the post after created (like in the attached image) to redirect to the new page that I will create ..
Can I do something like this?
You can do it by creating a hook for post_row_actions filter
add_filter( 'post_row_actions', 'wpse8170_row_actions', 10, 2 );
function wpse8170_row_actions( $actions, WP_Post $post ) {
if ( $post->post_type != 'my-custom-post-type' ) {
return $actions;
}
$actions['wpse8170-pdf'] = 'http://your/url/here';
return $actions;
}
Read this
http://pippinsplugins.com/add-custom-links-to-user-row-actions/
or read this one
http://www.codeforest.net/add-custom-links-to-row-actions-in-wordpress

Categories