Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Created August 12, 2015 20:06
Show Gist options
  • Save LinzardMac/81d2360c227ebc325325 to your computer and use it in GitHub Desktop.
Save LinzardMac/81d2360c227ebc325325 to your computer and use it in GitHub Desktop.
Test Case for Trac Ticket "Custom fields in media modal javascript events are unbound after ajax save method"
// Enqueue other custom javascript
function enqueue_custom_js(){
//Adds your custom script
wp_enqueue_script('jquery');
wp_register_script('jsbind', get_template_directory_uri() . '/js/javascriptbind.js' , array('jquery'), '1', true );
wp_enqueue_script( 'jsbind');
}
add_action('admin_enqueue_scripts','enqueue_custom_js');
function add_attachment_location_field( $form_fields, $post ) {
$form_fields['content'] = array(
'input' => 'html',
'html' => '<h2> Start Custom Field Inputs</h2>'
);
$field_value = get_post_meta( $post->ID, 'location', true );
$form_fields['location'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'Location' ),
'helps' => __( 'Set a location for this attachment' )
);
$foo = get_post_meta( $post->ID, 'foo', true );
$fooA = get_post_meta( $post->ID, 'fooA', true );
$fooB = get_post_meta( $post->ID, 'fooB', true );
$form_fields['foo'] = array(
'label' => 'Is Foo',
'input' => 'html',
'html' => '<label for="attachments-'.$post->ID.'-foo"></br> '.
'<button class="colorButton">Trigger JS Event</button></br>
<input type="checkbox" id="attachments-'.$post->ID.'-foo" name="attachments['.$post->ID.'][foo]" value="A" '.($foo ? ' checked="checked"' : '').'/> One</label></br>
<input type="checkbox" id="attachments-'.$post->ID.'-too" name="attachments['.$post->ID.'][fooA]" value="B" '.($fooA ? ' checked="checked"' : '').' /> Two</label></br>
<input type="checkbox" id="attachments-'.$post->ID.'-three" name="attachments['.$post->ID.'][fooB]" '.($fooB ? ' checked="checked"' : '').' value="C" />Three</label> ',
'value' => $foo,
'helps' => 'Choose one or many'
);
$foo2 = get_post_meta( $post->ID, 'foo2', true );
$form_fields['foo2'] = array(
'label' => 'Is Foo2',
'input' => 'html',
'html' => '<label for="attachments-'.$post->ID.'-foo2">'.$foo2.'</label>
<select id="attachments-'.$post->ID.'-foo2" name="attachments['.$post->ID.'][foo2]" >
<option value="1" '.($foo2=='1' ? ' selected="selected"' : '').'>One</option>
<option value="2" '.($foo2=='2' ? ' selected="selected"' : '').'>Two</option>
<option value="3" '.($foo2=='3' ? ' selected="selected"' : '').'>Three</option>
<option value="4" '.($foo2=='4' ? ' selected="selected"' : '').'>Four</option> ',
'helps' => 'Choose one'
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'add_attachment_location_field', 10, 2 );
function save_attachment_location( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) {
$location = $_REQUEST['attachments'][$attachment_id]['location'];
update_post_meta( $attachment_id, 'location', $location );
}
if ( isset( $_REQUEST['attachments'][$attachment_id]['foo'] ) ) {
$foo = $_REQUEST['attachments'][$attachment_id]['foo'];
update_post_meta( $attachment_id, 'foo', $foo );
} else{
$foo = NULL;
update_post_meta( $attachment_id, 'foo', $foo );
}
if ( isset( $_REQUEST['attachments'][$attachment_id]['fooA'] ) ) {
$fooA = $_REQUEST['attachments'][$attachment_id]['fooA'];
update_post_meta( $attachment_id, 'fooA', $fooA );
}
if ( isset( $_REQUEST['attachments'][$attachment_id]['fooB'] ) ) {
$fooB = $_REQUEST['attachments'][$attachment_id]['fooB'];
update_post_meta( $attachment_id, 'fooB', $fooB );
}
if ( isset( $_REQUEST['attachments'][$attachment_id]['foo2'] ) ) {
$foo2 = $_REQUEST['attachments'][$attachment_id]['foo2'];
update_post_meta( $attachment_id, 'foo2', $foo2 );
}
}
add_action( 'edit_attachment', 'save_attachment_location' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment