The problem is that this only works when the widget is present in a sidebar.
If you need some code to run independently from widget, I reason it makes sense to add that code separately from widget class.
And I also need it to work when I call this widget with the_widget() function, for eg. inside a page. I don’t know how could I retrieve the widget options to pass them in the javascript code…
That function takes instance array with widget data. I am not sure about your specifics, but I thinks you can pass whatever you need in there.
Directly called widget has no data, other than passed in instance (or retrieved by itself).
Related Posts:
- Get the sidebar ID in which the current widget was dropped
- Count widgets of a certain type
- Update widget form after drag-and-drop (WP save bug)
- How do register_sidebar() and get_sidebar() work together?
- Add a class to links in the visual editor (how to get old dialog back)
- How to make a script load after Custom Block is loaded in the editor?
- Need help with friendly URL’s in WordPress
- How to remove hardcoded characters from playlists?
- difference between esc_attr(), strip_slashes(), strip_tags()?
- How to preserve PHP modifications while upgrading WordPress?
- Widgets vs. Theme Mods
- Widgets not working in Customizr but working in Appearance
- When using the block.getSaveElement hook can you output different markup based on whether or not you are in the edit view or the live page view?
- How to exclude certain widget from showing up on home/front page? [duplicate]
- media_handle_upload weird thing
- Customise search form in the Widget
- Comment filtering (search)
- Infinite-Scroll Plugin and Jetpack Infinite Scroll Plugin – Adding to “Thoughts” Theme
- simple expandable vertical menu, java script problem
- Schedule cron event from widget
- WP_Query with the “offset” argument
- Widgets not showing in my custom theme
- register_sidebar ‘after_widget’ on custom-built widgets not implementing, caused nested widgets
- Add ‘Right Now’ widget to custom dashboard
- dynamic external javascript generated using php
- Add filename to attachment page url
- Custom widget select options needs to stay selected after save
- How to display something in a div when the user clicks on a text in another div – no page refresh [closed]
- Custom Widget form function common elements
- How to link external JavaScript files?
- Modify a theme to insert custom widgets?
- How did I enable atom feeds in a fresh WP3 install?
- How to update preview when custom setting changed in my custom widget
- Child Theme – how to add new widget on a specific place?
- Extending the WP_Widget_Text class
- Javascript Solution Remove Specific Tag but leave the rest in a specific DIV
- Fastest way to get the comment and ping total count for a post
- Author Tracking Code!
- want to create a subcategory.php to manage subcats
- How do I change the markup only of a built-in widget?
- Customize Theme comment template to Insert VoteUp and VoteDown buttons
- Is it possible to make widget return only data (eg, array)?
- Search widget will search everything in the site, how to limit to only search gallery name
- How to add a do_action on refreshing of WP customizer?
- Disable widgets in customizer for sidebar
- Menu jumping when calling it via PHP
- Unable to edit the “customize” section and the “widget” section is populating unknown code
- Choose sidebars column in widgets.php page [closed]
- Can’t receive $args[ ] to my custom widget
- Organizing the position of widgetized areas in the backend
- Media Upload , file name changed automatically
- I’d like to move the Widgets Panel to the bottom of the list in Customizer
- Redirect to another page using contact form 7? [closed]
- Custom Login Process
- Create small dashboard widget
- Hook called before text widget save
- Widgets won’t save
- Customizing the Widget content markup
- Widget title markup in register_sidebar
- Sticky menu for WP custom menubars
- WordPress widget and customize.php not working in Ubuntu16.04 VPS
- Upload a web module (html and JS) to wordpress
- How to append custom text to the output of ‘categories widget’?
- Customise the Category Widget
- What’s the right way to share data between widgets?
- How to add WordPress JS Script Links
- custom css and javascript not working on page defined as template?
- Custom Database Table and foreach
- Customize Widget Navigation
- wp_enqueue_script JS code runs too late (after user begins interacting)
- custom email validation using javascript api with elementor pro and code snippets
- javascript file not working in a plugin template file
- Inserting a class on before_widget if checkbox is checked
- Customize section does not show my widget areas
- How could I change contact details in the footer?
- Make Widget appear on Dashboard
- WordPress Customizer: Check for value of CSS selector and replace it
- How to hide or remove a custom widget area when empty
- Automatically refresh page if widget is added to page?
- custom field – changing an element or background of id div – different versions not working
- Still desperate about multiple TinyMCEs in widgets – is there any good solution to this yet?
- how to auto fille conatct form 7 when user is logined
- Custom Admin Section
- How to implement custom Javascript to my site
- Multi-site customization
- Why isn’t my custom Javascript showing up in my custom template?
- Slider from different widgets
- Confused about customising widgets
- Edit a custom admin page
- Pass the post ID
- Widget header unique classes
- customize footer widgets area
- Edit the Publish Widget Options
- Document.body.scrollTop in WordPress
- Post thats in Two Categories, only want to display name for one
- Custom Slider Per Page created
- How do I include the sidebar (with Widgets) in a custom theme?
- Making custom pages (with an example to explain the goal)
- Prevent posts from being published if the ‘Uncategorized’-category or no category is selected
- Multi color post title by using custom javascript inside the gutenberg editor?