Calculated Fields Form Shortcodes

Below, you’ll find a detailed guide on how to add the Calculated Fields Form Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Calculated Fields Form Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Calculated Fields Form Plugin and the shortcodes it provides:

Plugin Icon
Calculated Fields Form

"Calculated Fields Form is a dynamic WordPress plugin that allows you to create forms with automatically calculated fields for seamless user data collection."

★★★★☆ (878) Active Installs: 60000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [CP_CALCULATED_FIELDS]
  • [CP_CALCULATED_FIELDS_VAR]

Calculated Fields Form [CP_CALCULATED_FIELDS] Shortcode

The CP_CALCULATED_FIELDS shortcode is used to display a form from the Calculated Fields Form plugin. It fetches the form based on the ‘id’ attribute provided. If the ‘id’ attribute is missing, it retrieves the first form found. If the website is visited by a crawler, it returns an empty text. The shortcode also has ‘iframe’ and ‘asynchronous’ attributes for more advanced usage.

Shortcode: [CP_CALCULATED_FIELDS]

Parameters

Here is a list of all possible CP_CALCULATED_FIELDS shortcode parameters and attributes:

  • id – Unique identifier of a specific form.
  • iframe – Enables the form to be displayed inside an iframe.
  • asynchronous – Loads the iframe content asynchronously, enhancing page load speed.
  • class – Allows you to add a custom CSS class to the form.

Examples and Usage

Basic example – Displaying a form using its ID

[CP_CALCULATED_FIELDS id=1 /]

Here, the shortcode is used to display a form with the ID of 1. If the form exists, it will be displayed; otherwise, an empty string will be returned.

Advanced examples

Displaying a form within an iframe

[CP_CALCULATED_FIELDS id=1 iframe=true /]

In this example, the form with ID 1 will be displayed within an iframe. If the form does not exist, an empty string will be returned.

Displaying a form with custom JavaScript variables

[CP_CALCULATED_FIELDS id=1 var1="value1" var2="value2" /]

This example shows how to use the shortcode to display a form and also create two global JavaScript variables, var1 and var2, with the values “value1” and “value2” respectively. These variables can be used in custom JavaScript code on the page where the shortcode is used.

PHP Function Code

In case you have difficulties debugging what causing issues with [CP_CALCULATED_FIELDS] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'CP_CALCULATED_FIELDS', array( $this, 'public_form' ) );

Shortcode PHP function:

function public_form( $atts ) {
			// If the website is being visited by crawler, display empty text.
			if ( CPCFF_AUXILIARY::is_crawler() ) {
				return '';
			}
			if ( empty( $atts ) ) {
				$atts = array();
			}
			if ( ! $this->_is_admin && $this->_amp->is_amp() ) {
				$content = $this->_amp->get_iframe( $atts );
			} else {
				global $wpdb, $cpcff_default_texts_array;

				if ( empty( $atts['id'] ) ) {
					$myrow = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . CP_CALCULATEDFIELDSF_FORMS_TABLE ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
				} else {
					$myrow = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . CP_CALCULATEDFIELDSF_FORMS_TABLE . ' WHERE id=%d', $atts['id'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
				}

				if ( empty( $myrow ) ) {
					return ''; // The form does not exists, or there are no forms.
				}
				$atts['id'] = $myrow->id; // If was not passed the form's id, uses the if of first form.
				$id         = $atts['id']; // Alias for the $atts[ 'id' ] variable.

				if ( ! empty( $atts['iframe'] ) ) {
					if ( ! isset( $this->_iframe_nonces[ $id ] ) ) {
						$this->_iframe_nonces[ $id ] = wp_create_nonce( 'cff-iframe-nonce-'.$id );
					}

					$url = CPCFF_AUXILIARY::site_url( true );
					$url .= ( strpos( $url, '?' ) === false ? '?' : '&' ) . 'cff-form=' . $id . '&cff-form-target=_top&_nonce=' . $this->_iframe_nonces[ $id ];

					// The attributes excepting "id", "iframe", and "asynchronous" are converted in javascript variables with global scope
					if( count( $atts ) > 1 )
					{
						foreach( $atts as $i => $v )
						{
							if( ! in_array( $i, ['id', 'iframe', 'class' , 'asynchronous'] ) && ! is_numeric( $i ) )
							{
								$nV   = ( is_numeric( $v ) ) ? $v : sanitize_text_field( wp_unslash( $v ) ); // Sanitizing the attribute's value
								$url .= '&' . urlencode( $i ) . '=' . urlencode( $nV );
							}
						}
					}

					$iframe_tag = '<iframe ';
					if ( ! empty( $atts['asynchronous']) ) {
						$iframe_id = uniqid('cff-iframe-');
						$iframe_tag	 = '<script>window.addEventListener("load", function(){let el = document.getElementById("' . $iframe_id . '"); if(el) el.setAttribute("src", el.getAttribute("data-cff-src"));});</script>' . $iframe_tag . ' id="' . $iframe_id . '" src="about:blank" data-cff-src="' . esc_attr( $url ) . '"';
					} else {
						$iframe_tag	 .= ' src="' . esc_attr( $url ) . '"';
					}
					$iframe_tag	 .= ' style="border:none;width:100%;overflow-y:hidden;" onload="this.width=this.contentWindow.document.body.scrollWidth;this.height=this.contentWindow.document.body.scrollHeight+40;" scrolling="no"></iframe>';

					return $iframe_tag;
				}

				// Initializing the $form_counter
				if ( ! isset( $GLOBALS['codepeople_form_sequence_number'] ) ) {
					$GLOBALS['codepeople_form_sequence_number'] = 0;
				}
				$GLOBALS['codepeople_form_sequence_number']++;
				self::$form_counter = $GLOBALS['codepeople_form_sequence_number']; // Current form

				/**
				 * Filters applied before generate the form,
				 * is passed as parameter an array with the forms attributes, and return the list of attributes
				 */
				$atts = apply_filters( 'cpcff_pre_form', $atts );

				ob_start();

				// Constant defined to protect the "inc/cpcff_public_int.inc.php" file against direct accesses.
				if ( ! defined( 'CP_AUTH_INCLUDE' ) ) {
					define( 'CP_AUTH_INCLUDE', true );
				}

				$this->_public_resources( $id ); // Load form scripts and other resources

				/* TO-DO: This method should be analyzed after moving other functions to the main class . */
				@include CP_CALCULATEDFIELDSF_BASE_PATH . '/inc/cpcff_public_int.inc.php';

				$content = ob_get_contents();

				// The attributes excepting "id" are converted in javascript variables with a global scope
				if ( count( $atts ) > 1 ) {
					$content .= '<script>';
					foreach ( $atts as $i => $v ) {
						if ( 'id' != $i && 'class' != $i && ! is_numeric( $i ) ) {
							$nV       = ( is_numeric( $v ) ) ? $v : json_encode( $v ); // Sanitizing the attribute's value
							$content .= $i . '=' . $nV . ';';
							$content .= 'if(typeof ' . $i . '_arr == "undefined") ' . $i . '_arr={}; ' . $i . '_arr["_' . self::$form_counter . '"]=' . $nV . ';';
						}
					}
					$content .= '</script>';
				}
				ob_end_clean();

				/**
				 * Filters applied after generate the form,
				 * is passed as parameter the HTML code of the form with the corresponding <LINK> and <SCRIPT> tags,
				 * and returns the HTML code to includes in the webpage
				 */
				$content = apply_filters( 'cpcff_the_form', $content, $atts['id'] );
			}

			return $content;
		}

Code file location:

calculated-fields-form/calculated-fields-form/inc/cpcff_main.inc.php

Calculated Fields Form [CP_CALCULATED_FIELDS_VAR] Shortcode

The ‘CP_CALCULATED_FIELDS_VAR’ shortcode is a versatile tool that creates a custom variable. It checks for the presence of a ‘name’ attribute, assigns it a ‘value’, and encodes it into JSON format. If the ‘value’ attribute isn’t set, it checks the ‘from’ attribute to determine the source of the variable. If the source isn’t specified, it defaults to the POST, GET, SESSION, or COOKIE methods. If the variable is not found in these methods, it assigns a ‘default_value’ if provided. The shortcode then returns a script that sets the custom variable in the window object. .

Shortcode: [CP_CALCULATED_FIELDS_VAR]

Parameters

Here is a list of all possible CP_CALCULATED_FIELDS_VAR shortcode parameters and attributes:

  • name – Specifies the name of the variable.
  • value – Sets the value of the variable.
  • from – Determines the source of the variable value, such as POST, GET, SESSION, or COOKIE.
  • default_value – Sets a default value for the variable if no value is provided or found.

Examples and Usage

Basic example – A simple usage of the shortcode to create a variable with a predefined value.

[CP_CALCULATED_FIELDS_VAR name="myVar" value="100" /]

In the above example, we’re using the ‘CP_CALCULATED_FIELDS_VAR’ shortcode to define a variable named ‘myVar’ and assigning it a value of 100.

Advanced examples

Here, we’re using the shortcode to create a variable from a POST value. If the POST value doesn’t exist, it will default to a specified value.

[CP_CALCULATED_FIELDS_VAR name="myVar" from="post" default_value="50" /]

In this example, the shortcode is used to create a variable from a COOKIE value. If the COOKIE value doesn’t exist, it will default to a specified value. It also ensures the COOKIE value is sanitized before assigning it to the variable.

[CP_CALCULATED_FIELDS_VAR name="myVar" from="cookie" default_value="50" /]

These examples demonstrate the flexibility of the ‘CP_CALCULATED_FIELDS_VAR’ shortcode. It allows you to create variables from various sources and ensures the values are sanitized, providing a secure way to handle data within your WordPress site.

PHP Function Code

In case you have difficulties debugging what causing issues with [CP_CALCULATED_FIELDS_VAR] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'CP_CALCULATED_FIELDS_VAR', array( $this, 'create_variable_shortcode' ) );

Shortcode PHP function:

function create_variable_shortcode( $atts ) {
			if (
				! CPCFF_AUXILIARY::is_crawler() && // Checks for crawlers or search engine spiders
				! empty( $atts['name'] ) &&
				( $var = trim( $atts['name'] ) ) != '' // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments
			) {
				if ( isset( $atts['value'] ) ) {
					$value = json_encode( $atts['value'] );
				} else {
					$from = '_';
					if ( isset( $atts['from'] ) ) {
						$from .= strtoupper( trim( $atts['from'] ) );
					}
					if ( in_array( $from, array( '_POST', '_GET', '_SESSION', '_COOKIE' ) ) ) {
						if ( isset( $GLOBALS[ $from ][ $var ] ) ) {
							$value = json_encode( $GLOBALS[ $from ][ $var ] );
						} elseif ( isset( $atts['default_value'] ) ) {
							$value = json_encode( $atts['default_value'] );
						}
					} else {
						if ( isset( $_POST[ $var ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
							$value = json_encode( $_POST[ $var ] ); // @codingStandardsIgnoreLine.
						} elseif ( isset( $_GET[ $var ] ) ) {
							$value = json_encode( $_GET[ $var ] ); // @codingStandardsIgnoreLine.
						} elseif ( isset( $_SESSION[ $var ] ) ) {
							$value = json_encode( $_SESSION[ $var ] );
						} elseif ( isset( $_COOKIE[ $var ] ) ) {
							$value = json_encode( sanitize_text_field( wp_unslash( $_COOKIE[ $var ] ) ) );
						} elseif ( isset( $atts['default_value'] ) ) {
							$value = json_encode( $atts['default_value'] );
						}
					}
				}
				if ( isset( $value ) ) {
					return '
					<script>
						try{
						if( ! ( "cff_var" in window ) )	window["cff_var"] = {};
						window["cff_var"]["'.esc_js($var).'"]='.$value.';
						}catch( err ){}
					</script>
					';
				}
			}
			return '';
		}

Code file location:

calculated-fields-form/calculated-fields-form/inc/cpcff_main.inc.php

Conclusion

Now that you’ve learned how to embed the Calculated Fields Form Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *