Skip to content

Instantly share code, notes, and snippets.

@rochow
rochow / snippet.php
Last active October 12, 2020 21:53
PHP Current & Next City
<?php
// Dates on left (nice easy format), cities on right
$locations = array(
'1st June 2015' => 'Dubai (GMT+4)',
'8th July 2015' => 'Budapest (GMT+2)',
'1st August 2015' => 'London (GMT+1)',
'14th August 2015' => 'Scotland (GMT+1)'
);
$today = strtotime( 'now' );
@rochow
rochow / snippet.php
Last active October 12, 2020 22:07
Populate Dropdown with Post Type
<?php
add_filter( 'gform_pre_render', 'gform_prepopluate_post_type' );
add_filter( 'gform_admin_pre_render', 'gform_prepopluate_post_type' );
function gform_prepopluate_post_type( $form ){
$post_type = 'project';
$order_by = 'title';
$order = 'ASC';
$return = 'title'; // What you want the value to be, title or id
$form_id = 2;
@rochow
rochow / snippet.php
Last active December 16, 2020 21:44
Gravity Forms Don't Save Form Entries in Database
<?php
/*
USAGE
add_action( 'gform_post_submission', 'mr_remove_form_entries' ); // ALL FORMS
add_action( 'gform_post_submission_1', 'mr_remove_form_entries' ); // SPECIFIC FORM (ID = 1 in exmaple)
*/
add_action( 'gform_post_submission_1', 'mr_remove_form_entries' );
// Deletes entries from the database on the specified Gravity Forms form(s)
function mr_remove_form_entries( $entry ){
@rochow
rochow / snippet.php
Last active August 29, 2015 14:25
Gravity Forms - Don't Save Default Values
add_filter( "gform_save_field_value", "mr_filter_default_values", 10, 4);
// Don't save values when it's the inputs default value
function mr_filter_default_values( $value, $lead, $field, $form ) {
if (0 == strcmp( $value, $field['defaultValue']) ) {
$value = '';
}
return $value;
}
@rochow
rochow / snippet.php
Last active February 3, 2022 10:36
Gravity Forms - Get Longitude and Latitude of an Address
<?php
/*
The idea being a user enters their address, and on submission we grab the long & lat for the address
from Google and update 2 hidden inputs (which are most likely custom field inputs). You can then use
this data to plot pins on a map etc.
USAGE
add_action( "gform_pre_submission" , "mr_get_longlat_of_address"); // ALL FORMS
add_action( "gform_pre_submission_1" , "mr_get_longlat_of_address"); // SPECIFIC FORM (ID = 1 in exmaple)
@rochow
rochow / snippet.php
Last active August 29, 2015 14:25
Gravity Forms - Get Number of Entries
// USAGE: $form_count['total'] (int)
$form_count = RGFormsModel::get_form_counts( 1 ); // 1 = Form ID
@rochow
rochow / snippet.php
Last active October 12, 2020 22:04
Gravity Forms - Redirection Based on Answers
<?php
/*
Usage: You have a questionare. You set the value of each answer in relation to the index+1 in the array below
We then correspond each answer to see which result was selected the most then redirect to that page
For a tie we just redirect to the top item, this could be customised to select one of the top results
*/
add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $lead, $ajax ){
if( 1 == $form['id'] ) {
@rochow
rochow / snippet.php
Last active October 12, 2020 22:03
Gravity Forms - Validation (Default Value = Fail)
<?php
// Gravity Forms Validation. Rejects any required input where $_POST value = default input value (ya, placeholders didn't use to exist!)
add_filter( 'gform_validation', 'mr_custom_validation' );
function mr_custom_validation( $validation_result ){
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ){
if( '1' == $field['isRequired'] && 'checkbox' != $field['type'] ) {
if( $field['defaultValue'] == $_POST[ 'input_' . $field['id'] ] ) {
$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
@rochow
rochow / snippet.js
Last active August 29, 2015 14:25
jQuery - Simple Back to Top Link
// Back to top link. Automatically fades in once user has started scrolling
function backtotop() {
jQuery( window ).scroll(function() {
if(jQuery( this ).scrollTop() > 0) {
jQuery( '#toTop' ).fadeIn();
} else {
jQuery( '#toTop' ).fadeOut();
}
});
@rochow
rochow / snippet.js
Last active August 29, 2015 14:25
jQuery - Fancybox with Ratio
// Set the height of the video based on the width & ratio of the video
// That way even on mobile devices fancybox will be the size of the video and not have blank space
$( window ).resize( function() {
var _ratio = 0.56;
var _maxWidth = 1500;
var _width = $(window).width() - 50;
if( _width > _max) {
_width = _max;
}