Skip to content

Instantly share code, notes, and snippets.

@WPsites
Created October 22, 2012 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WPsites/3935417 to your computer and use it in GitHub Desktop.
Save WPsites/3935417 to your computer and use it in GitHub Desktop.
WPMU Gravity Form functions.php
<?php
/*
Filter the gravity form shortcode output to retreive the form from the main site
*/
add_filter('gform_shortcode_form', 'gform_shortcode_form_mu_override',10,3);
function gform_shortcode_form_mu_override( $shortcode_string, $attributes, $content ){
global $blog_id, $wpdb;
$old_wp_prefix = $wpdb->prefix;
$wpdb->prefix = 'wp_';
require_once(GFCommon::get_base_path() . "/form_display.php");
$new_form = GFFormDisplay::get_form($attributes['id'], $attributes['display_title'], $attributes['display_description'], $attributes['force_display'], $attributes['field_values'], $attributes['ajax'], $attributes['tabindex']);
$wpdb->prefix = $old_wp_prefix;
//replace the new forms action with one to the root site, rather than /sub-site/ which wouldn't process/submit
$shortcode_string = preg_replace('/action=\'.*#/', 'action=\'/#', $new_form);
return $shortcode_string;
}
/*
Populate the office dropdown with a list of available offices (blogs)
*/
add_filter('gform_pre_render', 'gform_pre_render_office');
function gform_pre_render_office($form){
foreach($form['fields'] as &$field){
if($field['type'] != 'select' || strpos($field['cssClass'], 'office') === false)
continue;
//loop through all live blogs and get thier id
global $wpdb;
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
AND blog_id != 1
");
global $site_list;
$site_list = array();
foreach ($blogs as $blog) {
$choices[] = array('text' => get_blog_option($blog->blog_id, 'blogname'), 'value' => $blog->blog_id);
}
$field['choices'] = $choices;
}
return $form;
}
/*
Automatically set office field id value based on site id
*/
add_filter('gform_field_value_office', 'gform_field_value_set_office');
function gform_field_value_set_office(){
global $blog_id, $switched, $switched_stack;
//if the blog has been switched with switch_to_blog() then use the switched id of the current site, we need the site they are looking at
if ($switched)
return $switched_stack[0];
return $blog_id;
}
/*
Translate the office ID to a human readable version (the blogname) for notification and entry storage
Add the office as an additional notification
*/
add_filter('gform_pre_submission_filter', 'gform_pre_submission_filter_nice_office');
function gform_pre_submission_filter_nice_office($form){
foreach($form['fields'] as &$field){
if ( preg_match('/office/', $field['cssClass']) ){
//add bcc notification for this office
switch_to_blog( $_POST["input_{$field['id']}"] );
$form["notification"]["bcc"] = of_get_option('contact_email', '') ;
restore_current_blog();
//use the sub site id to get the blog name and replace it
$_POST["input_{$field['id']}"] = get_blog_option( $_POST["input_{$field['id']}"] , 'blogname');
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment