Created
November 5, 2010 13:50
-
-
Save mjangda/664186 to your computer and use it in GitHub Desktop.
Creates a custom field in WordPress using Custom Metadata Manager and uses custom display and sanitize callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
x_add_metadata_field('x_fieldCustomList2', array( 'post' ), array( | |
'label' => 'Post Action Items (With Links!)' | |
, 'display_callback' => 'fieldCustomList2_display' | |
, 'sanitize_callback' => 'fieldCustomList2_sanitize' | |
)); | |
function fieldCustomList2_display( $field_slug, $field, $object_type, $object_id, $value ) { | |
$value = (array) $value; | |
$field_class = sprintf( 'field-%s', $field_slug ); | |
$count = 0; | |
?> | |
<?php if( ! empty( $value ) ) : ?> | |
<?php foreach( $value as $v ) : ?> | |
<?php | |
$text = isset( $v['text'] ) ? $v['text'] : ''; | |
$url = isset( $v['url'] ) ? $v['url'] : ''; | |
?> | |
<div class="f2_my-list-item"> | |
<label>Text</label> | |
<input type="text" name="<?php echo $field_slug; ?>_text[]" value="<?php echo esc_attr( $text ); ?>" /> | |
<label>URL</label> | |
<input type="text" name="<?php echo $field_slug; ?>_url[]" value="<?php echo esc_attr( $url ); ?>" /> | |
<?php if( $count > 0 ) : ?> | |
<a href="#" class="f2_btn-del-list-item hide-if-no-js" style="color:red;">Delete</a> | |
<?php endif; ?> | |
<?php $count++; ?> | |
</div> | |
<?php endforeach; ?> | |
<?php else : ?> | |
<input type="text" name="<?php $field_slug; ?>[]" value="" /> | |
<?php endif; ?> | |
<p><a href="#" class="f2_btn-add-list-item hide-if-no-js">+ Add New</a></p> | |
<script> | |
;(function($) { | |
$('.f2_btn-add-list-item').click(function(e) { | |
e.preventDefault(); | |
var $last = $('.f2_my-list-item:last'); | |
var $clone = $last.clone(); | |
$clone | |
.insertAfter($last) | |
.find(':input') | |
.val('') | |
; | |
}); | |
$('.f2_btn-del-list-item').live('click', function(e) { | |
e.preventDefault(); | |
$(this).parent().remove(); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
function fieldCustomList2_sanitize( $field_slug, $field, $object_type, $object_id, $value ) { | |
$values = array(); | |
$text_key = $field_slug . '_text'; | |
$url_key = $field_slug . '_url'; | |
if( isset( $_POST[$text_key] ) ) { | |
$count = 0; | |
foreach( (array) $_POST[$text_key] as $text ) { | |
$url = isset( $_POST[$url_key][$count] ) ? $_POST[$url_key][$count] : ''; | |
if( $text || $url ) { | |
array_push( $values, array( | |
'text' => $text | |
, 'url' => $url | |
) ); | |
} | |
$count++; | |
} | |
} | |
return $values; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment