Last active
March 31, 2021 19:30
-
-
Save spivurno/2cb857c17b10731828fb to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Set Entry Creator by Field Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Set Entry Creator by Field Value | |
* | |
* Use in combination with [Populate Anything](https://gravitywiz.com/documentation/gravity-forms-populate-anything/) to | |
* populate a Drop Down field (or any other single value field type) with WordPress users and then set the selected user | |
* as the creator of the entry. | |
* | |
* @version 1.1 | |
* @author David Smith <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/ | |
* | |
* Plugin Name: GF Set Entry Creator by Field Value | |
* Plugin URI: http://gravitywiz.com/ | |
* Description: Set the entry creator by field value. The value must be a valid WordPress user ID. | |
* Author: Gravity Wiz | |
* Version: 1.1 | |
* Author URI: http://gravitywiz.com | |
*/ | |
class GW_Set_Entry_Created_By { | |
public function __construct( $args = array() ) { | |
// set our default arguments, parse against the provided arguments, and store for use throughout the class | |
$this->_args = wp_parse_args( $args, array( | |
'form_id' => false, | |
'field_id' => false | |
) ); | |
// do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
add_filter( 'gform_entry_post_save', array( $this, 'update_created_by' ) ); | |
} | |
public function update_created_by( $entry ) { | |
if ( $entry['form_id'] != $this->_args['form_id'] ) { | |
return $entry; | |
} | |
$field_value = rgar( $entry, $this->_args['field_id'] ); | |
if ( ! $field_value ) { | |
return $entry; | |
} | |
// set the property in the DB | |
GFAPI::update_entry_property( $entry['id'], 'created_by', $field_value ); | |
// update the property in the current $entry object that will be used for the rest of the submission process | |
$entry['created_by'] = $field_value; | |
return $entry; | |
} | |
} | |
# Configuration | |
new GW_Set_Entry_Created_By( array( | |
'form_id' => 123, // update to the ID of your form | |
'field_id' => 1 // update to the ID of the field whose value should be used for the "created_by" entry property | |
) ); |
This would go in your theme’s functions.php file. More troubleshooting steps here:
https://gravitywiz.com/documentation/snippet-troubleshooting/
👉 This Gist has been migrated to the Gravity Wiz Snippet Library:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I add your code on my function.php but it don't work.
Where i do add this php page ?
Regards