Wordpress plugin development - get all active widgets - wordpress

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.

Related

Remove description meta box from custom taxonomy

I'm trying to remove these areas from one of my custom taxonomies.
I've built them using the two plugins: Custom Post Types UI (to add them) and Advanced Custom Fields (to add fields to the taxonomy).
I can't see anything in the plugin settings to remove these things, but I'm not sure if I'm missing something.
I'm assuming I might need to add a function to the functions.php file. I've seen that hiding things using jQuery is a possibility, but I hear that this might show it initially on load and then hide it, so id like to learn how to doit properly.
I managed to get there with the link above and help from another website.
This function removed the two instances of the 'description' box:
function hide_description_row() {
echo "<style> .term-description-wrap { display:none; } </style>";
}
add_action( "measure_sectors_edit_form", 'hide_description_row');
add_action( "measure_sectors_add_form", 'hide_description_row');
and this one removed the column from the right hand side:
add_filter('manage_edit-measure_sectors_columns', function ( $columns ) {
if( isset( $columns['description'] ) )
unset( $columns['description'] );
return $columns;
});
To use with other taxonomies, just replace 'measure_sectors' with your own taxonomy slug

Hide Widget based on condition using Widget Logic Plugin

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.

WooCommerce: how to wrap Recent products widget?

Using built-in WooCommerce widget "Recents products" displays a title (text) followed by an unordered list of products. Surprisingly these two main elements (the title and the list) are not wrapped in any container, making it hard to style, integrate into a theme and ultimately hard to use. There's no option either for that in the widget menu.
Still is there any way to wrap them I may have not thought of ?
WooCommerce makes use of Filters to help us override the HTML output by the default Widgets.
For example, the WooCommerce Products, which I believe is the one you are referring to makes use woocommerce_before_widget_product_list and woocommerce_after_widget_product_list filters. You can simply hook your functions and modify the default output. Something like this :
add_filter( 'woocommerce_before_widget_product_list', 'my_theme_before_widget_product_list', PHP_INT_MAX );
add_filter( 'woocommerce_after_widget_product_list', 'my_theme_after_widget_product_list', PHP_INT_MAX );
function my_theme_before_widget_product_list( $html ) {
return '<div class="my-theme-product-list">' . $html;
}
function my_theme_after_widget_product_list( $html ) {
return $html . '</div><!-- /.my-theme-product-list -->';
}
If you are using the widgets within a sidebar, you can use before_widget, after_widget, before_title and after_title arguments of register_sidebar function.

Wordpress latest posts menu item

I'm trying to get Wordpress to give me a menu item to go to "latest posts." They come up on the frontpage, but once I navigate away, I want a menu item to get back there. It seems so obvious, but several hours later, the best I could do was create a custom menu with a link to "uncategorised" as a workaround. There MUST be a better way! And this way, I get a box saying "Archive of posts filed under the Uncategorized category. " Not wanted!
Create a custom page in your template directory (http://codex.wordpress.org/Pages#Page_Templates) with a custom query (check at http://codex.wordpress.org/Class_Reference/WP_Query, http://codex.wordpress.org/Function_Reference/query_posts or http://codex.wordpress.org/Template_Tags/get_posts).
Create a page in your admin and select the template you created.
Add a link to this page in your menu and you're done.
Maybe this will help: http://www.viper007bond.com/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/
It's a filter that will 'search and replace' placeholder anchors such as '#latestpost1' with the actual url of the latest post, and thus dynamically modify the menu before it's rendered.
I'm not sure how this is for SEO, but it's a clever solution.
Give all your posts a category name. Use something generic like "News", "Articles" or "Blogs". Then, choose the category with the name you picked from the menu page under categories. Add this category link to your menu. Rename the link whatever you wish - "Blog" - for example. And, viola - all your posts will appear when people click on that link.
Try this plugin: https://de.wordpress.org/plugins/dynamic-latest-post-in-nav-menu/ works really well and code is open sourced here: https://github.com/hijiriworld/dynamic-latest-post-in-nav-menu
simple solution:
I took this guy's code: http://www.viper007bond.com/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/
Basically what he wrote is for the menu item to link to the latest post, not posts (plural), so I just modified it and it's working:
<?php
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );
}
// Replaces a custom URL placeholder with the URL to the latest post
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {
// Loop through the menu items looking for placeholder(s)
foreach ( $items as $item ) {
// Is this the placeholder we're looking for?
if (!strpos(($item->url), 'latestpost'))
continue;
// if ( 'latestpost' != $item->url )
// continue;
// Get the latest post
$latestpost = get_posts( array(
'numberposts' => 1,
) );
if ( empty( $latestpost ) )
continue;
// Replace the placeholder with the real URL
$new_link = $item->url;
$new_link = substr($new_link, 0, strlen($new_link) - 12);
$item->url = $new_link;
}
// Return the modified (or maybe unmodified) menu items array
return $items;
}

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 );

Resources