Skip to content

Instantly share code, notes, and snippets.

View rafaehlers's full-sized avatar

Rafael Ehlers rafaehlers

View GitHub Profile
@rafaehlers
rafaehlers / gk_trigger_gravityexport_feeds.php
Created September 13, 2023 21:44
Trigger a GravityExport Save feed after editing an Entry in GravityView
<?php // DO NOT COPY THIS LINE
add_action( 'gravityview/edit_entry/after_update', 'gk_trigger_gravityexport_feeds', 10, 3 );
function gk_trigger_gravityexport_feeds( $form = array(), $entry_id = array(), $object ) {
$form_ids = array( 10, 85, 77 );
// Check if the form ID matches any of the expected values
if ( ! in_array( (int) $form['id'], $form_ids ) ) {
@rafaehlers
rafaehlers / gk_math_second_form.php
Last active December 21, 2023 21:13
Filter for GravityMath to pull records from a secondary form
<?php // DO NOT COPY THIS LINE
add_filter( 'gravityview/math/shortcode/before', function ( $formula ) {
preg_match_all( '/~[^~]*?:(\d+(\.\d+)?|[a-z_]+)(:(.*?))?~/mi', $formula, $merge_tags, PREG_SET_ORDER );
foreach ( $merge_tags as $merge_tag ) {
$updated_merge_tag = str_replace( '~', '', $merge_tag[0] );
$updated_merge_tag = sprintf( '{%s}', $updated_merge_tag );
$formula = str_replace( $merge_tag[0], $updated_merge_tag, $formula );
@rafaehlers
rafaehlers / gk-urlify-list-fields.php
Last active August 29, 2023 21:24
List Field Merge Tag Modifier to convert text URLs into clickable links
<?php // DO NOT COPY THIS LINE
add_filter( 'gform_merge_tag_filter', 'gv_list_columns', 30, 6 );
function gv_list_columns( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
if ( $field->type == 'list' && $merge_tag != 'all_fields' && 'urlify' === $modifier ) {
$values = unserialize( $raw_value );
$output = '<ul>';
@rafaehlers
rafaehlers / gk-search-states.php
Created August 28, 2023 20:05
Convert state name search to state abbreviations
add_filter( 'gravityview_fe_search_criteria', function ( $search_criteria ) {
if ( empty( $search_criteria['field_filters'] ) ) {
return $search_criteria;
}
foreach ( $search_criteria['field_filters'] as &$filter ) {
if ( $filter['value'] == 'Alabama' ) { $filter['value'] = 'AL';}
if ( $filter['value'] == 'Alaska' ) { $filter['value'] = 'AK';}
if ( $filter['value'] == 'Arizona' ) { $filter['value'] = 'AZ';}
@rafaehlers
rafaehlers / gv-entry-approval-filters.php
Created February 28, 2023 18:16
GravityView filters for entry approval
<?php // DO NOT COPY THIS LINE
add_action( 'gravityview/approve_entries/disapproved', 'gv_custom_function',10,1);
add_action( 'gravityview/approve_entries/unapproved', 'gv_custom_function',10,1);
add_action( 'gravityview/approve_entries/updated', 'gv_custom_function',10,1);
add_action( 'gravityview/approve_entries/approved', 'gv_custom_function',10,1);
function gv_custom_function($entry_id){
$entry = GFAPI::get_entry( $entry_id );
$form = GFAPI::get_form( $entry['form_id'] );
@rafaehlers
rafaehlers / gv-edit-entry-validation.php
Last active February 22, 2023 23:59
GravityView - Prevent a required field from being required on Edit Entry page
<?php // DO NOT COPY THIS LINE
add_filter( 'gform_pre_render', 'gravityview_edit_entry_dont_require_fields' );
add_filter( 'gform_pre_validation', 'gravityview_edit_entry_dont_require_fields' );
function gravityview_edit_entry_dont_require_fields( $form ) {
if ( ! class_exists( '\GravityView_View' ) ) {
return $form;
}
@rafaehlers
rafaehlers / gv_claim_entry_by_user_data.php
Created November 29, 2022 21:13
Modify whether the currently logged-in user can edit an entry that was not created by him - Several Views
<?php // DO NOT COPY THIS LINE
add_filter('gravityview/edit_entry/user_can_edit_entry', 'gv_claim_entry_by_user_data', 20, 3 );
/**
* Modify whether the currently logged-in user can edit an entry that was not created by him
*
* @param boolean $user_can_edit Can the current user edit the current entry? (Default: false)
* @param array $entry Gravity Forms entry array
* @param int $view_id ID of the view you want to check visibility against {@since 1.15}
*
@rafaehlers
rafaehlers / gv-highlight-row-field.php
Created June 10, 2022 20:41
Highlight an entire row if entry was created by currently logged-in user
<?php // DO NOT COPY THIS LINE
add_filter( 'gravityview/template/table/entry/class', function( $class, $context ) {
if ( ! is_user_logged_in() ) {
return $class;
}
$view_id = $context->view->ID;
$current_user_id = get_current_user_id();
@rafaehlers
rafaehlers / gv_approval_timestamp.php
Created May 18, 2022 18:21
Records a timestamp into a field when the entry approval status is changed
<?php // DO NOT COPY THIS LINE
add_action( 'gravityview/approve_entries/updated', 'gv_approval_timestamp', 10, 2 );
function gv_approval_timestamp($entry_id, $status){
if( !class_exists( 'GFAPI' ) ) {
gravityview()->log->error( 'GFAPI does not exist' );
return false;
}
@rafaehlers
rafaehlers / gfexcel-add-title.php
Last active January 28, 2022 22:13
Add a title to the CSV exported file
<?php //DO NOT LINE
add_filter('gfexcel_renderer_matrix', static function (array $rows) {
// Make up the title
$title = 'text';
// Wrap in array to become a row.
$title = [$title];
array_unshift($rows, $title);