我正在构建一个简单的滑块插件,我正在使用选项页面,它允许您添加新幻灯片,使用媒体上传器和删除幻灯片。但是,我无法理解或找到有关如何删除或更新插件选项的任何有用信息,或至少在哪里运行delete_option()函数。
当用户点击删除幻灯片链接时,我希望与该幻灯片相关的.s-image-row div消失,并在保存页面时从数据库中删除该选项。
请帮助,谢谢。
PHP - 选项页面(简化)
function scratch_image_value( $option ) {
$scratch_images = get_option( 'scratch_images' );
//if the option is in the database and not an empty value
if ( isset($scratch_images[$option]) && $scratch_images[$option] != "" ) {
echo $scratch_images[$option];
//if the option does not exists in the database
} else {
echo "http://localhost/my-site/wp-content/uploads/2015/09/03.jpg";
}
}
<?php
$scratch_images = get_option('scratch_images');
$n = ( count($scratch_images) == 0 ) ? 1 : count($scratch_images);
?>
<input class="s-max-id" type="hidden" name="<?php echo $n; ?>" />
<div class="s-slides-wrapper">
<?php for ($i = 1; $i <= $n; $i++) { ?>
<div class="s-image-row">
<img class="custom_media_image" src="<?php scratch_image_value($i); ?>" />
<input class="custom_media_url" id="" type="text" name="scratch_images[<?php echo $i; ?>]" value="<?php scratch_image_value($i); ?>" >
<a href="#" class="button scratch_media_upload">Upload</a>
<a class="s-delete-slide" href="#">Delete slide</a>
</div>
<?php } ?>
</div>
<a class="s-add-slide" href="#">Add slide</a>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Options', 'scratch-slider'); ?>" />
</p>
jQuery(添加新幻灯片)
jQuery( document ).ready(function(){
counter = jQuery('.s-max-id').attr('name');
counter = parseInt(counter);
counter++;
jQuery('.s-add-slide').click( function(event) {
event.preventDefault();
element = jQuery('.s-image-row').last().clone();
new_counter = counter++;
element.find('input').attr('name', 'scratch_images[' + new_counter + ']');
jQuery('.s-slides-wrapper').append(element);
return false;
});
});