首先,调用ajax的表单必须将一个操作与ajax挂钩的值一起归档。例如,您的ajax调用是:
add_action(\'wp_ajax_show_all_tracks\', \'show_all_tracks\');
“您的表单操作”字段必须是:
<input type="hidden" name="action" value="show_all_tracks" />
这是开始,为了添加曲目,您需要添加一个函数,将javascript添加到曲目编辑表单中,或者通过更改
function add_post_enctype() {
echo \'
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#post").attr("enctype", "multipart/form-data");
jQuery("#post").attr("encoding", "multipart/form-data");
});
</script>\';
}
至
function add_post_enctype() {
echo \'
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#post").attr("enctype", "multipart/form-data");
jQuery("#post").attr("encoding", "multipart/form-data");
jQuery("#Add Track").click(function() {
jQuery.ajax({
type: "post",url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
data: {
action: \'add_track\',
ad_type: jQuery( \'#tr_name\' ).val() ,
ad_type: jQuery( \'#tr_time\' ).val() ,
ad_type: jQuery( \'#tr_lyrics\' ).val() ,
_ajax_nonce: jQuery( \'#nonce\' ).val() ,
},
success: function(html){ //so, if data is retrieved, store it in html
if (html.indexOf(\'ok\') > -1 || html.indexOf(\'true\') > -1) {
alert(\'track added\');
}
}
});
return false;
});
});
</script>\';}
创建ajax do操作挂钩
add_action(\'wp_ajax_add_track\', \'add_track\');
然后函数本身添加曲目:
function add_track(){
check_ajax_referer( "add_track" );
/* save to the custom field array that holds the track data.
and return OK as string i know you can manage that*/
}
最后一个是更改表单:
<form id="add-track-form" name="add-track-form" action="" method="post">
<input type="hidden" name="mytheme_meta_box_nonce" value="\'<?php wp_create_nonce(basename(__FILE__)) ?>\'" />
<?php foreach($this->_meta_box[\'form\'] as $el){
$this->display_field($el);
} ?>
<input type="submit" id="add-track" name="add-track" value="Add Track" />
到
<form id="add-track-form" name="add-track-form" action="" method="post">
<input type="hidden" name="nonce" id="nonce" value="<?php echo wp_create_nonce( \'add_track\' ); ?>" />
<?php foreach($this->_meta_box[\'form\'] as $el){
$this->display_field($el);
} ?>
<input type="submit" id="add-track" name="add-track" value="Add Track" />
</form>
希望这对你有所帮助,它更像是一个草稿,但它应该会让你朝着正确的方向前进。
ohad公司。