我在管理员帖子编辑器中创建了一个自定义表单,以便向作者发送电子邮件模板。我在ajax调用期间遇到问题,没有重定向到我需要的页面,并且在选择的模板和点击发送选项时更新消息发布
<div id="custom-meta-files" class="row">
<div class="col-md-12">
<form>
<div class="form-group">
<label style="" for="reject_email_template">Template Name</label>
<?php
// The Query
$the_query = new WP_Query( array(
'post_type' => 'email-templates',
'posts_per_page' => -1,
));
if ( $the_query->have_posts() ) { ?>
<select style="display: block;width: 100%;margin: 10px 0;" name="_reject_email" class="form-control" id="reject_email_template">
<?php
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<option value="'.get_the_ID().'">' . get_the_title() . '</option>';
} ?>
</select>
<?php
wp_reset_postdata();
} else {
// no posts found
}
?>
</div>
<button type="submit" class="reject-email-btn btn btn-primary" style="border:none; padding: 6px 1rem;color: #fff; background: #0085ba;text-decoration: none;">Send</button>
</form>
<div id="fromPartsTable"></div>
</div>
保存自定义字段数据库中的帖子
add_action('admin_init','event_meta_init');
function event_meta_init(){
foreach (array('post') as $type){
add_meta_box('my_all_meta', 'Reject Email', 'my_meta_setup', $type, 'normal', 'high');
}
add_action('save_post','my_meta_save');}
function my_meta_setup(){
global $post;
// using an underscore, prevents the meta variable
// from showing up in the custom fields section
$meta = get_post_meta($post->ID,'_post',TRUE);
// instead of writing HTML here, lets do an include
include(MY_THEME_FOLDER . '/metafields/events-fields.php');
// create a custom nonce for submit verification later
echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}
function my_meta_save($post_id){
// authentication checks
// make sure data came from our meta box
if (!wp_verify_nonce($_POST['my_meta_noncename'],__FILE__)) return $post_id;
// check user permissions
if ($_POST['post_type'] == 'page'){
if (!current_user_can('edit_page', $post_id)) return $post_id;
}else{
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
$current_data = get_post_meta($post_id, '_post', TRUE);
$new_data = $_POST['_post'];
my_meta_clean($new_data);
if ($current_data){
if (is_null($new_data)) delete_post_meta($post_id,'_post');
else update_post_meta($post_id,'_post',$new_data);
}elseif (!is_null($new_data)){
add_post_meta($post_id,'_post',$new_data,TRUE);
}
return $post_id;
}
function my_meta_clean(&$arr){
if (is_array($arr)) {
foreach ($arr as $i => $v){
if (is_array($arr[$i])){
my_meta_clean($arr[$i]);
if (!count($arr[$i])){
unset($arr[$i]);
}
}else{
if (trim($arr[$i]) == ''){
unset($arr[$i]);
}
}
}
if (!count($arr)){
$arr = NULL;
}
}
}
我的ajax脚本是
jQuery.noConflict();
(function( $ ) {
function rejection_email(){
$.ajax({
url: "/rejection-email/",
data : {eid:$('#reject_email_template').val()},
success: function(data) {
var result = data
$('#fromPartsTable').html(result);
},
error: function() {
alert('Error occured');
}
});
}
$('.reject-email-btn').click(function(e) {
rejection_email();
});
})(jQuery);