Hi there, is there any way to access the current post from a SiteOrigin_Widget child class?
I want to pass some info from the current post to a javascript tied to the widget using wp_localize_script.
something like:
function initialize() { $this->register_frontend_scripts( array( array( 'venue-map', get_stylesheet_directory_uri() . '/widgets/widgets/venue-map/scripts/venue-map.js', array( 'jquery', 'widgets' ) ) ) ); $current_post = // Get current post. // wp_localize_script('venue-map', 'currentPost', $current_post) }
Hi Jim
Thanks for reaching out, sorry we weren’t able to reply sooner.
I don’t think that we’re doing anything weird that would prevent you from using
https://developer.wordpress.org/reference/functions/get_post/. Have you perhaps given that a try?
It looks like you can actually do get_post(). Not sure why I couldn’t figure this out, guess I had some kind of tunnel vision. I ended up with another solution to this specific question though but I came back here for another problem.
Although it looks like you cannot use wp_localize_script as I have above because I always end up with the “currentPost” object being undefined in my javascript.
If you still need to localize your script and pass it data from your widget you can do it on the ‘siteorigin_widgets_instance_’ . $this->id_base filter (https://siteorigin.com/docs/widgets-bundle/advanced-concepts/filters/widget-instance/).
Just to formalize this thread and possibly help others stumbling upon it, here’s an example:
In my widget class
Thanks for reporting. I’ve chatted to our developers and this does seem like a limitation at the moment. If you could log this bug it would be helpful: https://github.com/siteorigin/so-widgets-bundle/issues