This thread is over two years old and may be outdated. Please create a new thread if you need help, or email us if you have an active Premium license.

Using Javascript in the widget admin interface

9 years ago · Last reply by Jim Robert Nilsson 9 years ago

Hi there. I’m building a bunch of custom widgets based on the SiteOrigin_Widget class.

This far it’s been working really well but now I need to do something a little bit more complicated and I’m having some trouble figuring out how to solve this.

Basically what I want is to include a datetime-picker for one of my widgets. As far as I’ve seen in the documentation there is no premade element in the SO_Widget form builder.

So my idea was to add a regular textfield to my widget and then through javascript target that textfield and attach a javascript datetimepicker of some sort (not sure exactly which one yet).

I have been able to get a javascript running in my admin by enqueueing a script in the inherited initialize-function from the base-class, like so:

function initialize() {
    wp_enqueue_script( 
      'featured-product', 
      get_template_directory_uri() . '/widgets/featured-product/js/featured-product.js', 
      array( 'jquery' ), // dependencies
      '1.0', // version
      true // on footer
    );
  }

Though I have a feeling that this is not the best approach to do this, it should work.
My first question is how would you suggest to go on about enqueuing scripts to use in my widgets admin interface?

Secondly I wonder if there is a simple way of targeting an element within the widget admin interface. Can in some way get the element id from the instance of my Widget class and pass it to my javascript? Or should I rather modify the textelement form field and add some kind of target that way?

Thank you so much for an awesome plugin!

This is our free support forum. Replies can take several days.

Need fast email support? Get SiteOrigin Premium

Replies

4
  1. Alex S Staff 9 years, 11 months ago

    Hi Jim,

    You can use the siteorigin_widgets_enqueue_admin_scripts hook to do this. If you haven’t seen it yet, the developer docs is a gold mine of information.

  2. Jim Robert Nilsson 9 years, 11 months ago

    Hi Alex

    I think I’ve been using the docs to much, don’t know how I could miss this, thanks!

  3. Alex S Staff 9 years, 11 months ago

    Hi Jim,

    Don’t worry, It happens. Is there anything else I can help you with today?

  4. Jim Robert Nilsson 9 years, 11 months ago

    No thank you, I have started another thread within another matter.

Replies on this thread are closed.

Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Have a different question or issue?

Start New Thread