• Resolved hutsori

    (@hutsori)


    Hi Sven,

    This maybe awkward but I need to get form_id for my element template.

    Top of my template says I can use $element_id, $type, $name …. etc, but $form_id isn’t listed and if I echo this, I get null.

    I am seeing get_form_id(), get_current_form_id() functions in the plugin but I am not quite sure how to use these to make the form_id available for my template.

    Could you please help?

    Thank you.

    https://wordpress.org/plugins/torro-forms/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Sven Wagener

    (@mahype)

    Hi Min,

    jsut take a look here. There is the to_json method explained:

    public function to_json( $element ) {
        // This method can be used to add additional data that can be used
        // by the frontend template. All keys in the $data array will be available
        // as individual variables in the template.
        //
        // Checkout the Torro_Element_Type base class to see what is available
        // by default. Make sure to always call the parent method (see below).
    
        $data = parent::to_json( $element );
    
        $data['my_custom_value'] = 'some-value';
    
        return $data;
      }

    Also you have the function

    torro()->forms()->get_current_form_id();

    If you combine it, you will get this:

    public function to_json( $element ) {
        $data = parent::to_json( $element );
        $data['form_id'] = torro()->forms()->get_current_form_id();
        return $data;
      }

    After you have added this to the element, the element template should have the variable $form_id.

    Thread Starter hutsori

    (@hutsori)

    Sven,

    Ah.. That’s how..

    I tried $data[‘form_id’] = $this -> get_current_form_id(); and this didn’t work.
    I somehow managed to resolve it by using javascript though.

    But I am going to use your method since it’s a lot nicer and simpler.

    THANK YOU!

    Plugin Author Sven Wagener

    (@mahype)

    Does it work?

    Thread Starter hutsori

    (@hutsori)

    YES IT DOES. 🙂

    Plugin Author Sven Wagener

    (@mahype)

    Great! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to retrieve form_id from an element template?’ is closed to new replies.