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.

Access current post

9 years ago · Last reply by Andrew Misplon 9 years ago

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

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

Need fast email support? Get SiteOrigin Premium

Replies

3
  1. Andrew Misplon Staff 9 years, 6 months ago

    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

    get_post()

    https://developer.wordpress.org/reference/functions/get_post/. Have you perhaps given that a try?

  2. Jim Robert Nilsson 9 years, 5 months ago

    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

    function initialize() {
        $this->register_frontend_scripts(
          array(
            array( 'employees', get_stylesheet_directory_uri() . '/widgets/widgets/employees/scripts/employees.js', array( 'jquery', 'widgets' ) )
          )
        );
      }
      public function filter_instance($instance, $widget){
        $employees = $instance['employees'];
        wp_localize_script('employees', 'employees', $employees);
        return $instance;
      }
  3. Andrew Misplon Staff 9 years, 5 months ago

    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

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