Home>Support>Access current post

Access current post

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. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 8 years, 1 month ago Andrew Misplon
    Hi, I Work Here

    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. 8 years, 1 month ago Jim Robert Nilsson

    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. 8 years, 1 month ago Andrew Misplon
    Hi, I Work Here

    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.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More