Get Field Value from Last Gravity Form Submission

This is a little snippet that I put together for a client who wanted to do something with the previous form entry. I believe it was a field that was set dynamically, and the end-client wanted to either auto-increment, or make sure it was greater than the previous submission.

<?php
//Form ID
$form_id = 1;

//Field ID
$field_id = 1;

//Pulls only the most recent entry
$paging = array( 'offset' => 0, 'page_size' => 1 );
$entries = GFAPI::get_entries( $form_id, array(), null, $paging );

//Value that we want
$value = $entries[0][$field_id];

Encrypt & Decrypt Plugin Options

Sometimes, you want to collect information in your plugin settings page that you may not want to be easily readable from the database as plain text. For example, you might want to store a site owner’s API key for a service. There are a few considerations that must be met:

  1. The site-owner must be able to enter, update, and remove the API key if desired.
  2. If anyone looks in the database at the plugin option, or opens up the settings page, the key should not be human readable.
  3. The key must be recoverable, since we’ll need to use it to make API calls.

With these things in mind, here’s the approach we’ll take:

<?php
//Registers the pre_update_option filter for elements to encrypt. Format pre_update_option_(option name)
//Only do this for ones you want encrypted... in this case, first and third options.
function yourplugin_init() {
	add_filter( 'pre_update_option_your_first_option', 'yourplugin_update_option', 10, 2 );
	add_filter( 'pre_update_option_your_third_option', 'yourplugin_update_option', 10, 2 );
}
add_action( 'init', 'yourplugin_init' );

//Converts option value to encrypted form, only if it has changed
function yourplugin_update_option( $new_value, $old_value ) {
	if ($new_value != $old_value) {
		$cypher = 'aes-256-cbc'; //Pick the cypher to use
		$key = md5(SECURE_AUTH_SALT); //SECURE_AUTH_SALT can be replaced with any unique string.
    $new_value = openssl_encrypt("$new_value","$cypher","$key");
	}
	return $new_value;
}

//A function to retrieve the decrypted value of the options, to use them in your plugin
function yourplugin_decrypt_option($option) {
	 $cypher = 'aes-256-cbc'; //Be sure to use the same cypher as above
	 $key = md5(SECURE_AUTH_SALT); //Be sure to use same $key as above
	 $decrypted_value = openssl_decrypt("$option","$cypher","$key");
	 return $decrypted_value;
}

Pre-populate Gravity Forms List Field

This is a little snippet I put together when looking at using Gravity Forms dynamic population to pre-populate a list type field. The task in question had the list field value saved to user meta (via the User Registration add-on), so that’s why we’re checking to see if it’s empty before assigning the value, in case the user has already modified and saved newer information than the default.

<?php
/******************************************************
* gform_user_registration_user_data_pre_populate runs when data is pulled from user meta and displayed
******************************************************/
add_filter( 'gform_user_registration_user_data_pre_populate', 'gf_prepopulate_meta_mapped_data', 10, 3 );
function gf_prepopulate_meta_mapped_data( $mapped_fields, $form, $feed ) {
    $form_id = $form['id']; //Gets the form ID that is being pre-populated
    if($form_id == 24) {  //Runs only on form number 24
        //Array of list field values, just like you defined in your original example (no changes)
        $field_one_value = array(
            array(
                'Item' => 'Rent',
                'Cost per Month' => ' ',
            ),
            array(
                'Item' => 'Phone',
                'Cost per Month' => ' ',
            ),
            array(
                'Item' => 'Internet',
                'Cost per Month' => ' ',
            ),
            array(
                'Item' => 'Electricity',
                'Cost per Month' => ' ',
            ),
            array(
                'Item' => 'Add Option',
                'Cost per Month' => ' ',
            ),
        );
    
        //If there isn't something saved to the mapped meta field, then use the array above and map to field 1
        if(empty($mapped_fields[1])) {
            $mapped_fields[1] = maybe_serialize($field_one_value);
        }
    }
    
    return $mapped_fields;
}

Check for Gutenberg

This snippet is useful when you are updating an older plugin and don’t want to break backwards compatibility for users on older versions of WordPress. This allows you to conditionally include Gutenberg blocks only if the appropriate Gutenberg core files are available.

<?php
/*******************************************************************
* Checks to see if Gutenberg is set up on the site before attempting to load blocks
*********************************************************************/
function myplugin_setup_blocks() {
   if(function_exists('register_block_type')) {
       require_once( plugin_dir_path( __FILE__ ) . 'blocks/my-block-slug.php');
   }
}
add_action( 'plugins_loaded', 'myplugin_setup_blocks' );

In this example, blocks/my-block-slug.php is your block’s (or blocks’, if multiple) PHP loader file.

Above: Shiny Code ( https://github.com/cedaro/shiny-code )