Hide Widget based on condition using Widget Logic Plugin - wordpress

My website (http://www.chicagokaraokenight.com/wordpress) is a karaoke directory where bars can list karaoke nights for free OR upgrade to a premium or featured premium listing (paid packages) to receive a more robust profile.
As part of the benefit to upgrading to a paid package, I'd like to have some widgets disappear on the paid listings.
My theme author (Listify) recommended the plugin Widget Logic and said the following:
Use https://wordpress.org/plugins/widget-logic/ to show/hide a widget depending on certain criteria.
Using something like:
wc_paid_listings_get_user_package( $package_id )
I'm still a little unclear on exactly what I should edit the logic to say and hoping someone can help. I know how to get the package ID's (if I hover over the packages on the Product page I can see them).
The widgets I wish to hide based on a listing being a paid package are Google ads, recent listings, and featured listings.
Thanks!
UPDATE: The WC Paid Listing Plugin developer has given me this code and info after I mentioned the Free package ID is 971:
global $post;
$used_package = get_post_meta( $post->ID, '_package_id', true );
if ( 971 === $used_package ) {
// Free
}
You could wrap this in a custom function to use in widget logic:
function job_was_posted_with_package( $package_id ) {
global $post;
$used_package = get_post_meta( $post->ID, '_package_id', true );
return $package_id == $used_package;
}
Called via:
job_was_posted_with_package( 971 );
Do I have what I need now? Can someone help me identify what needs to go into functions.php, what should go in Widget Logic, etc?

Widget Logic is pretty straight forward in form of inner workings.
I assume you have a widget already created what you would need to do is make or use a function that returns a Boolean
function is_paid_member(){
// Verification Code here
return TRUE;
}
Then place is_paid_member() in the widget logic field of the affected Widget.
If you can print some code for the verification method i can most likely edit the Answer to make it workable.
Please clarify the following, is the Widget always going to be available in the Administrative interface? If yes then it would go outside of the Widget Logic scope which affect mostly activated Widgets displaying on the frontend.

Per Mike Jolley from WP Paid Listings, the correct code is:
function job_was_posted_with_package( $package_id ) {
global $post;
$used_package = get_post_meta( $post->ID, '_package_id', true );
return $package_id == $used_package;
}
which should be placed in the functions.php file.
And then:
( ! job_was_posted_with_package( ID ) && ! job_was_posted_with_package( ID ) )
or
job_was_posted_with_package( ID )
should be the condition entered into Widget Logic on the widget you want to show/hide.

Related

Hide and show Complete action shortcut in woocommerce

I searched a lot but didn't find any solution that work for me.
I am developing a plugin for wordpress that will work only with woocommerce. So i want that when my order status is in pending state i want to hide complete action shortcut on orders listing page.
In the above picture i want to hide middle button when my order is in pending state. I want to do it in plugin.
Any suggestion.
Thanks in advance.
Yes this can be done using 'woocommerce_admin_order_actions' filter and adding a function to a filter.
Code to be added in you plugin:
add_filter('woocommerce_admin_order_actions','wdm_verify_product_limitation',5,2);
function wdm_verify_product_limitation( $actions, $the_order ){
if ( $the_order->has_status( array( 'pending','on-hold') ) ) {
unset($actions['complete']);
}
return $actions;
}
Here i am checking if the order is in 'pending' or 'on-hold' state then don't show the Complete button for that order.(in your case remove 'on-hold' from the code )
Have tested the same. Do let me know if this works for you

Remove screen options tab if not admin

As the title suggests, I am looking for a way to remove the screen options tab in the post/page editor screen. I have found the following...
function remove_screen_options(){ __return_false;}
add_filter('screen_options_show_screen', 'remove_screen_options');
...but it removes the tab for all users. I would like to keep it for admins.
Regards,
John
Found the answer after collaboration of all of your efforts. Thank you.
get_currentuserinfo() ;
global $user_level;
function remove_screen_options(){ __return_false;}
if( $user_level <= 8 ) add_filter('screen_options_show_screen', 'remove_screen_options');
If you place the following snippet of code into your functions.php file, the Screen Options tab will disappear across the whole backend for all users except the admin. Having said that, it is a good practice to modify the php file of your child theme.
functions.php code:
function remove_screen_options_tab()
{
return current_user_can('manage_options' );
}
add_filter('screen_options_show_screen', 'remove_screen_options_tab');
You just need to conditionally check if the current user is an admin. If they aren't, then remove the screen options like so:
if ( !is_admin() ) {
function remove_screen_options(){ __return_false;}
add_filter('screen_options_show_screen', 'remove_screen_options');
}
Here are the official Wordpress docs detailing this function: http://codex.wordpress.org/Function_Reference/is_admin
Using capabilities the way Spencer suggests is usually the best method.
I'm thinking most people find roles & capabilities to be overly confusing. Using the actual user role with 'current_user_can' is more often than not your best bet for this or any other similar 'permission' based situation.
More often than not you will eventually end up adding/removing capabilities for a specific role, so if you ever give someone else the 'manage_options' capability, like perhaps the owner of the business, you all of a sudden gave them back the screen options as well (On a side note 'activate_plugins' is usually safe since it is only for someone that has level 10 access). You can check out all permissons on the User Level Capability Table at the bottom of the page to get a better grasp on it all.
Insert this in functions php:
if( !current_user_can('administrator') ) {
// hide screen options for everyone but the admin
add_filter('screen_options_show_screen', 'remove_screen_options_tab');
}
if( current_user_can('administrator') ) {
// code here is shown to the admin
}
Following this format you can do the same thing with other roles. Also you dont have is change administrator to editor, author, contributor and subscriber, or any other roles you create.
Try this
if(!current_user_can('manage_options')) add_filter('screen_options_show_screen', 'remove_screen_options');
Use Adminimize, A WordPress plugin that lets you hide 'unnecessary' items from the WordPress backend.
http://wordpress.org/plugins/adminimize/
You're looking for the function current_user_can:
if( current_user_can( 'manage_options' ) ) {
// executes when user is an Administrator
}
Here's the CSS method for all the designers who rather stay away from php. It hooks into the admin_body_class and adds user-{role} as a body class.
functions.php code:
function hide_using_css_user_role( $classes ) {
global $current_user;
foreach( $current_user->roles as $role )
$classes .= ' user-' . $role;
return trim( $classes );
}
add_filter( 'admin_body_class', 'hide_using_css_user_role' );
Using this you can hide/show anything on the admin side per user role. In this case just use the :not css selector to make sure it's only hidden for non-admins.
function add_my_custom_user_css() {
ob_start(); ?>
<style type="text/css">
:not(.user-administrator) #screen-options-link-wrap,
:not(.user-administrator) #contextual-help-link-wrap  {
display:none !important;
}
</style>
<?php
echo ob_get_clean();
}
add_action ( 'admin_head', 'add_my_custom_user_css', 999);
This is a pretty hacky way to do things but sometimes good for a temporary quick fix when you don't know the correct filter/action to hide or change things in wordpress. Adding the 999 will make sure it gets loaded at the end of the head tag. Note that it's only hiding using css, so don't use this for anything vitally important, because the code is still visible in the source file.
To remove it from the source use jquery instead. Just replace the styles above with the following:
<script type="text/javascript">
jQuery(document).ready(function($)) {
$( ":not(.user-administrator) #screen-options-link-wrap" ).remove();
}
</script>
'admin_body_class' already does us the favor of adding the page to body class, so to target specific pages as well, just check the source code and in the body tag you can see the current page. So for example, the dashboard uses .index-php. Just attach that to .user-administrator or whatever user you're targeting and you can customize the admin for any user just using css and javascript.

Get WordPress Sidebars Fom The Current Front End

Is there a function to retrieve the WordPress sidebar(s) that is displayed in the current front end?
I posted a solution that might work, here: Checking If A WordPress Widget Displayed In The Current Front End
dynamic_sidebar doesn't register that it's been called for that and that sidebar. Neither is there a suitable hook to do this yourself. So I'm afraid you would have to tell Wordpress for each template what sidebars are being displayed. One way to do this would be to create a wrapper function like this
function wrap_dynamic_sidebar( $sidebar_id )
{
global $sidebars_in_this_template;
$sidebars_in_this_template[] = $sidebar_id;
return dynamic_sidebar( $sidebar_id );
}
And replace dynamic_sidebar with this everywhere (but I understand that it is very likely this solution will not be feasible for you).
If you want to display a list of all sidebars, you could use $wp_registered_sidebars
global $wp_registered_sidebars;
$sidebar_ids = array_keys( $wp_registered_sidebars );

Wordpress, filter pages in Admin edit screen

Is it possible to 'filter' which pages are shown in the 'edit' screen for pages ( http://cl.ly/6nLC ) in Wordpress? I have looked in the action / hook section of Wordpress for plugin developers but I could not find any.
What I am trying to accomplish is is that certain users can edit certain pages (and child pages) and other persons cannot edit those pages but might be able to edit other pages.
I have allready written a plugin which makes it possible to put different users in differtent groups, which now just needs to have different rights, which user is member of which group is stored in the user_meta table.
However if there is 'any' filter hook / method for this, can someone point this out, I think I will be able to go further from there.
Kind regards.
You can use a posts_where filter to add a condition to the SQL query to filter out some pages. A load-{filename} action can be used to ensure the filter is only applied when managing pages.
add_action('load-edit.php', 'my_load_edit_php_action');
function my_load_edit_php_action() {
if ($_GET['post_type'] !== 'page') return;
add_filter('posts_where', 'my_posts_where_filter');
}
function my_posts_where_filter($sql) {
if (current_user_can('your_capability')) {
global $wpdb;
$sql = " AND $wpdb->posts.ID NOT IN (1,2,3)" . $sql;
}
return $sql;
}

Wordpress plugin development - get all active widgets

I am developing a wordpress plugin that creates a widget that would act upon another widget. I have searched but cant seem to find (if it exists) a hook that would give details of all active instances of widgets. any help would be appreciated if you have come across this. thanks
get_option('sidebars_widgets') gives you an associative array that contains a list of widgets for each sidebar plus a list of all inactive widgets.
get_option('widget_widgetname') will give you an associative array that contains the settings of all instances of your widget.
for example to remove a widget from a page
add_filter( 'sidebars_widgets', 'disable_widgets' );
function disable_widgets( $sidebars_widgets ) {
global $qode_options_proya;
//print_r($sidebars_widgets);//gives a list of widgets
if(is_admin()){return $sidebars_widgets;}
if(get_post_meta( get_the_ID(), 'hide_allwidgets_checkbox', true )=="on"){return false; }
if(get_post_meta( get_the_ID(), 'hide_footer', true )=="on"){unset($sidebars_widgets["footer_column_1"]);}
if(get_post_meta( get_the_ID(), 'hide_topmenu_checkbox', true )=="on"){unset($sidebars_widgets["header_top"]); }
return $sidebars_widgets;
}
This might vary depending on your theme, so use print_r($sidebars_widgets); to verify the widgets available on your case
You can get all the active widgets of sidebar as follows::
$sidebars_widgets = get_option( 'sidebars_widgets' );
it will give you an associative array containing a list of widgets per sidebar and a list of all inactive widgets.

Resources