Torro Forms // Using torro()->element_answers()

This page contains the technical documentation for the Torro Forms torro() functions and is written for developers. If you're not a developer you may want to review the Form Builder.

Base functions

There are some basic functions to handle element answers with the torro() function. Here is a list of the most important functions.

    torro()->element_answers()->create( $element_id, $args );
    torro()->element_answers()->update( $element_answer_id, $args );
    torro()->element_answers()->get( $element_answer_id );
    torro()->element_answers()->delete( $element_answer_id );
    torro()->element_answers()->exists( $element_answer_id );
    torro()->element_answers()->copy( $element_answer_id, $element_id );

Create an element answer

Parameters

The create() function needs the parameter $element_id and $args. The parameter $args contains an array with the data for the element answer. The function will give back the element answer object or an WP_Error.

    
    $args = array(
        'answer' => 'Soccer',
	'sort'	=> 0,
    );
    $element_answer = torro()->element_answers()->create( $element_id, $args );

Return Values

You will get back a Torro_Element_Answer object by this function. To get the ID of the created container, get the variable of the object by using $element_answer->id. To catch all eventually occured errors, you have to check if $element_answer is a WP_Error and print out the error message.

    if( ! is_wp_error( $element_answer ) ) {
        $element_answer_id = $element_answer->id;
    } else {
        echo $element_answer->get_error_message();
    }

Update an element answer

Parameters

The update() function needs the parameters $element_answer_id and $args. The $args parameter contains an array with the data for the element answer. The function will give back the element answer object or an WP_Error.

    $args = array(
        'answer' => 'Football',
        'sort' => 1
    );
    $form = torro()->element_answers()->update( $element_answer_id, $args );

Return Values

You will get back a Torro_Element_Anwer object by this function. To get the ID of the created element answer, get the variable of the object by using $element_answer->id. To catch all eventually occured errors, you have to check if $element_answer is a WP_Error and print out the error message.

    if( ! is_wp_error( $element_answer ) ) {
        $element_answer_id = $element_answer->id;
    } else {
        echo $element_answer->get_error_message();
    }

Getting element answer data

Parameters

To get an element answer you can use the following code.

    $container = torro()->element_answers()->get( $element_answer_id );

Return Values

You will get back a Torro_Element_Answer object by this function. To get further information you can also use the deeper chained functions to access the container object parameters. Take a look at the autocomplete functionality of your programming IDE.

Deleting an element answer

Parameters

Input the element answer ID to the function delete.

    $deleted = torro()->element_answers()->delete( $element_answer_id );

Return Values

To catch all eventually occured errors, you have to check if $deleted is a WP_Error and print out the error message.

    if( is_wp_error( $deleted ) ) {
        echo $deleted->get_error_message();
    }

Checking existance of a an element answer

Parameters

Input the element answer ID to the function exists.

    torro()->element_answers()->exists( $container_id );

Return Values

The function returns true if the element answer exists and false if not.

Copy a container

Parameters

To copy an element answer from one element to another, put in the $element_answer_id which have to be copied and the $element_id where to copy the element answer. The element answer and all the content of the element answer will be copied to the element.

    torro()->element_answers()->copy( $element_answer_id, $element_id );

Return Values

The function return a Torro_Element_Answer object or a WP_Error.

Fork me on GitHub