我使用工具集插件创建了一个名为request
的自定义帖子类型。
我需要在这些管理页面中添加一个按钮(仅适用于此自定义帖子类型):
新request
(post-new.php)
编辑request
(post.php)
当管理员提交按钮时,我需要发送有关该帖子的信息的电子邮件(对此没有问题)。
如何添加按钮和回调以提交?
答案 0 :(得分:2)
将新元素添加到 Post Edit 屏幕的一种方法是通过metaboxes。
请检查以下代码:
/**
* Adds a call-to-action metabox to the right side of the screen under the "Publish" box.
*/
function wp645397_add_cta_metabox() {
add_meta_box(
'wp645397_request_cta',
'Call-to-action title here', /* This is the title of the metabox */
'wp645397_request_cta_html',
'request', /* the request post type */
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'wp645397_add_cta_metabox' );
/**
* Output the HTML for the call-to-action metabox.
*/
function wp645397_request_cta_html() {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( 'request_send_post_details', 'request_cta_nonce' );
// Output the call-to-action button
?>
<a href="#" id="btn-call-to-action" class="button button-primary widefat">Call-to-action button text here</a>
<script>
/**
* We'll handle the button via Ajax to avoid having to reload the screen.
*/
jQuery(function($){
// Handle button click
$( "#wp645397_request_cta #btn-call-to-action" ).on("click", function(e){
e.preventDefault();
// Get the security nonce
var nonce = $(this).parent().find("#request_cta_nonce").val();
// Send the data via AJAX
$.post(
ajaxurl,
{
action: 'send_request_email',
nonce: nonce,
postid: <?php echo $post->ID; ?>
},
function( response ){
// Do something after the data has been sent
if ( 'OK' == response ) {
alert( 'Info sent' );
}
else {
alert( 'Something went wrong, try again' );
}
}
);
});
});
</script>
<?php
}
/**
* Handles CTA AJAX.
*/
function wp645397_send_request_email(){
// Invalid nonce
if ( !isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'request_send_post_details' ) ) {
wp_die( 'error' );
}
$post_ID = $_POST['postid'];
// Get post data
$post = get_post( $post_ID );
// Get post title
$post_title = $post->post_title;
// Get custom field, etc
// @TODO
// Send e-mail with data
// @TODO
wp_die('OK');
}
add_action( 'wp_ajax_send_request_email', 'wp645397_send_request_email' );
它的作用:
我在代码中添加了一些注释,这些注释应解释正在发生的事情。如果您有任何疑问,请随时提出,好吗?
答案 1 :(得分:0)
我不确定工具集插件提供了哪些选项,但是如果我以编程方式进行此操作,我会做类似的事情
$post_types = array('post', 'page');
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$fetch_post_types = get_post_types($args, $output, $operator);
foreach ($fetch_post_types as $key => $value) {
if ($value != 'vocabulary' && $value != 'augmentation') {
array_push($post_types, $value);
}
}
add_meta_box('email_sender_post_type_metabox', __('Email Options', 'seoaipdl'), 'email_sender_post_type_metabox', $post_types, 'side', 'high', null);
function email_sender_post_type_metabox() {
//Button HTML code here
}
add_action('save_post', 'save_meta_data_function', 10, 2);
function save_meta_data_function($id, $data) {
if (!current_user_can("edit_post", $id)) {
return $data;
}
if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
return $data;
}
\\$_POST variable will have your submit button here.
}
或者,您可以使用jquery添加一个按钮,并在此按钮单击时使用ajax触发电子邮件功能。
为此,您需要查看admin_enqueue_script
和wp_localize_script