Skip to content

Instantly share code, notes, and snippets.

@stephywells
Last active August 29, 2015 14:02
Show Gist options
  • Save stephywells/fb60fc55adb212cb0dd8 to your computer and use it in GitHub Desktop.
Save stephywells/fb60fc55adb212cb0dd8 to your computer and use it in GitHub Desktop.
Formidable snippets
<?php
//* Do NOT include the opening php tag
// Add this to your theme functions.php or a new plugin
// to add a list of predefined choices on the bulk options popup.
add_filter('frm_bulk_field_choices', 'add_custom_frm_options');
function add_custom_frm_options($opts){
// change 'Set Name' to whatever label you want shown on the left side of the bulk field options popup
// add each option to the array
$opts['Set Name'] = array('Option 1', 'Option 2', 'Option 3');
return $opts;
}
<?php
//* Do NOT include the opening php tag
// Add this to your theme functions.php or a new plugin
// to automatically delete files when an entry is deleted.
add_action('frm_before_destroy_entry', 'delete_entry_uploads', 10, 2);
function delete_entry_uploads( $entry_id, $entry = false ) {
if ( empty( $entry ) ) {
$entry = FrmEntry::getOne( $entry_id );
}
if ( empty( $entry ) ) {
// If there is no entry, don't continue checking for files
return;
}
$upload_fields = FrmField::getAll(array('fi.type' => 'file', 'fi.form_id' => $entry->form_id));
foreach ( $upload_fields as $upload_field ) {
$media_id = FrmEntryMeta::get_entry_meta_by_field($entry->id, $upload_field->id);
if ( !$media_id && $entry->post_id ) { //if this is a post field, get the value
if ( !isset($upload_field->field_options['post_field']) ) {
$upload_field->field_options['post_field'] = '';
}
if ( !isset($upload_field->field_options['custom_field']) ) {
$upload_field->field_options['custom_field'] = '';
}
if ( $upload_field->field_options['post_field'] ) {
$media_id = FrmProEntryMetaHelper::get_post_value(
$entry->post_id, $upload_field->field_options['post_field'],
$upload_field->field_options['custom_field'], array('type' => 'file')
);
}
}
if ( !$media_id ) {
continue;
}
$media_id = maybe_unserialize($media_id);
if ( is_numeric($media_id) ) {
wp_delete_attachment($media_id, true);
} else if ( is_array($media_id) ) {
foreach ( $media_id as $m ) {
if ( is_numeric($m) ) {
wp_delete_attachment($m, true);
}
}
}
unset($upload_field, $media_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment