How are Wordpress Dynamic Sidebar Widgets rendered? - wordpress

I have been reading through the Wordpress Source, trying to get a better understanding of how dynamic sidebars are rendered.
However, I am hitting a sticking point...
894 | do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );
I can't find where add_action('dynamic_sidebar', ... ) is defined. Without that part, I am sort of lost in what happens.
See the code here:
https://github.com/WordPress/WordPress/blob/b7c13e27c255e1fc1f03ab2ab432f1652a0ac212/wp-includes/widgets.php#L894
And to give more context, I am trying to figure out how to grab an array of widgets from a specific sidebar, and from there, I need to know how would you render each widget within that array.
I need finer control than dynamic_sidebar(...); gives me

Well, that specific line permits you to access each registered Widget properties, and it's used like:
<?php
/* Plugin Name: Test registered widgets */
add_action( 'dynamic_sidebar', 'sidebar_widgets_so_18666065' );
/**
* As this is an action hook, we don't return nothing
* use the passed values to do your stuff
*/
function sidebar_widgets_so_18666065( $registered_widget )
{
# Each registered widget passes the following array
/*
$registered_widget = Array
(
[name] => Meta
[id] => meta-2
[callback] => Array
(
[0] => WP_Widget_Meta Object
(
[id_base] => meta
[name] => Meta
[widget_options] => Array
(
[classname] => widget_meta
[description] => Log in/out, admin, feed and WordPress links
)
[control_options] => Array
(
[id_base] => meta
)
[number] => 2
[id] => meta-2
[updated] =>
[option_name] => widget_meta
)
[1] => display_callback
)
[params] => Array
(
[0] => Array
(
[number] => 2
)
)
[classname] => widget_meta
[description] => Log in/out, admin, feed and WordPress links
)
*/
}
Relevant search query at WordPress Answers.

Related

How can woocommerce programmatically get all the links to the downloaded product if the product is variable?

Please help me to get programmatically links to files from the downloaded product.
the product is variable, if I output an array of data, it shows me a link only to the default variation.
You need to get a link of the form variation 1: link, variation 2: link
global $product;
echo "<pre>";
print_r ($product);
echo "</pre>";
[shipping_class_id] => 0
[downloads] => Array
(
[96f6c36d5ebe77d9caf11be7a070b5a3] => WC_Product_Download Object
(
[data:protected] => Array
(
[id] => 96f6c36d5ebe77d9caf11be7a070b5a3
[name] => file0001.pdf
[file] => https://link.com/202209/files/file0001.pdf
[enabled] => 1
)
)
)
I tried to get an array of product data, but there is only one link out of two

shortcode_atts not parsing custom values instead defaults loaded

I' making my own shortcode function and while the call of the shortcode works, and my page query within returns results - it never uses any settings but the defaults as if $att is null.
function test_shortcode( $atts ) {
$filter = shortcode_atts(
array(
'type' => 'major',
'sort' => 'name',
'size' => 'large',
'links' => 'yes',
),
$atts,
'customshortcode'
);
echo 'ATTS:';
print_r($atts);
echo'FILTER';
print_r($filter);
//code to query posts removed
}
add_shortcode( 'customshortcode', 'test_shortcode' );
In the post I can then add..
[customshortcode type:"other" size:"small" sort:"rand" links:"no"]
To see the result
ATTS
Array
(
[0] => type:"other"
[1] => size:"small"
[2] => sort:"rand"
[3] => links:"no"
)
FILTER
Array
(
[type] => major
[sort] => name
[size] => large
[links] => yes
)
and I can see the $atts values are received in the function but the $filter is not updated. I'm expecting both arrays to be the same at the point they are being printed out. As far as I can tell I'm following the coxed formatting here https://codex.wordpress.org/Function_Reference/shortcode_atts
You are passing attributes in the wrong way.
It should use = instead of :.
Please go through https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ to know more about shortcode with parameters.
Try with [customshortcode type="other" size="small" sort="rand" links="no"]

Different restrictions on media library modal

I know it's possible to restrict the media library modal's items using the ajax_query_attachments_args filter, but is it possible to do so differently wether you're editing a field or another of a same post ?
For example : i have an ACF Gallery field on a post type, and i want its images to only be a selection of images, while the other media libraries on the page (featured image, editor add media, ...) still displays all the images.
Thanks
ACF Support provided me an answer, so i'm sharing it here :
Using the ajax_query_attachments_args filter, it is possible to identify the field initiating the request, as "_acfuploader" :
Array
(
[action] => query-attachments
[post_id] => 838
[query] => Array
(
[post_mime_type] => image
[orderby] => date
[order] => DESC
[posts_per_page] => 40
[_acfuploader] => field_579b87ce88001
[paged] => 1
)
)
So with ajax_query_attachments_args, it is then possible to modify the query :
add_filter( 'ajax_query_attachments_args', 'filter_product_attachments', 10, 1 );
function filter_product_attachments( $query = array() ) {
if ($_POST['query']['_acfuploader'] == $THE_FIELD_ID){
// Filter media query here
}
return $query;
}

Drupal hook before or after render() function

I have a problem with my drupal page. We use the opigno tool (its a module to that allows to create course - nodes which can be bought).
Now we have a problem with the view of the course-node. Everything works fine until a user bought the course and the drupal commerce status is on "pending". The problem now is that the node is showing nothing except the title of the course.
I began to think whats going on and took a look to the view settings for the course-view. Nothing (no Permission was set, no filter criteria ...)
Then I took a look at the template override of the course-view. And then I saw smth ...
print render($content['body']);
Ah! A render function to render the content of the view. So I asked myself: What is in content ?
After printing out the $content variable I saw this:
Array
(
[body] => Array
(
[#theme] => field
[#weight] => 1
[#title] => Description
[#access] =>
[#label_display] => hidden
[#view_mode] => full
[#language] => und
[#field_name] => body
[#field_type] => text_with_summary
[#field_translatable] => 0
[#entity_type] => node
[#bundle] => course
[#object] => stdClass Object
(
[vid] => 185
[uid] => 60
[title] => Demo Course
[log] =>
[status] => 1
[comment] => 1
[promote] => 0
[sticky] => 0
[vuuid] => 16e7999b-28bb-4f3e-8bed-54a21b17f398
[nid] => 176
[type] => course
[language] => und
[created] => 1406897520
[changed] => 1407145285
[tnid] => 0
[translate] => 0
[uuid] => 22baf19a-dc32-4137-b698-89f1bb4adc59
[revision_timestamp] => 1407145285
[revision_uid] => 1
[body] => Array
(
[und] => Array
(
[0] => Array
(
[value] =>
This is a sample course for beta testers, it will show you the structure of the courses.
Here usually is displayed the description of the course.
Please "enroll" if you want to see the contents of this demo course.
[summary] => This is a sample course for beta testers to help you see the structure of a course. Please have a look on it!
[format] => html
[safe_value] =>
This is a sample course for beta testers, it will show you the structure of the courses.
Here usually is displayed the description of the course.
Please "enroll" if you want to see the contents of this demo course.
[safe_summary] => This is a sample course for beta testers to help you see the structure of a course. Please have a look on it!
)
)
)
)
Aaah! So $content has the content, but its not printed out with render()! But why ?
I searched in google for a hook which are able to "override" the render function and maybe deny the output when the user is on "pending". But... nothing.
I found this hook:
{hook}_node_view()
I searched for a using of this hook in all modules but when deleteing these functions from the modules did not changed anything. Are there other options to "override" the render() function ? Another hooks I can search for ? Or maybe you had the same problem with opigno ?
I am sorry - the problem was the Opigno Field Access Module. I deactivated it and it works like a charm.

how to populate widgets on sidebar on theme activation

What I am trying to do is pre-populate the sidebar widget area with some default widgets on theme activation.
if ( ! dynamic_sidebar( 'sidebar' ) ) :
does add the widgets but it doesnot show up in the sidebar of widgets section and
if ( is_active_sidebar( 'sidebar' ) ) {
this function doesnot work if the widgets are not loaded in the sidebar widgetized area.
I know it is possible but I am just out of idea. I googled but didnot find any solutions. Thank you for any help in advance.
It isn't clear from your answer if you use the after_switch_theme hook but that the moment you need to set the widgets.
To activate the widgets I suggest writing it directly into the database with get_option('sidebars_widgets') which should give an array, and save it with update_option('sidebars_widgets', $new_activated_widgets).
This should help you get started.
/**
* set new widgets on theme activate
* #param string $old_theme
* #param WP_Theme $WP_theme
*/
function set_default_theme_widgets ($old_theme, $WP_theme = null) {
// check if the new theme is your theme
// figure it out
var_dump($WP_theme);
// the name is (probably) the slug/id
$new_active_widgets = array (
'sidebar-name' => array (
'widget-name-1',
'widget-name-2',
'widget-name-3',
),
'footer-sidebar' => array(
'widget-name-1',
'widget-name-2',
'widget-name-3',
)
);
// save new widgets to DB
update_option('sidebars_widgets', $new_active_widgets);
}
add_action('after_switch_theme', 'set_default_theme_widgets', 10, 2);
Tested, just paste it in functions.php of your theme.
If anyone else needed to know how to add multiple default widgets (different instances) to multiple sidebars at the same time, the following code will add the widgets both to the page and under the admin widget tab. I realize that this may have been obvious to everyone but me.
So based on janw and kcssm's hard work:
function add_theme_widgets($old_theme, $WP_theme = null) {
$activate = array(
'right-sidebar' => array(
'recent-posts-1',
'categories-1',
'archives-1'
),
'footer-sidebar' => array(
'recent-posts-2',
'categories-2',
'archives-2'
)
);
/* the default titles will appear */
update_option('widget_recent-posts', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('widget_categories', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('widget_archives', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('sidebars_widgets', $activate);
}
add_action('after_switch_theme', 'add_theme_widgets', 10, 2);
This will however delete any other settings, so tread carefully!

Resources