Wordpress 自定义按钮/动作自定义帖子类型

ale*_*e91 1 wordpress

我有一个request使用 Toolset 插件创建的自定义帖子类型。

我需要在这些管理页面中添加一个按钮(仅适用于这种自定义帖子类型):

  • 新的request(post-new.php)

  • 编辑request(post.php)

当管理员提交按钮时,我需要发送一封包含帖子信息的电子邮件(这没有问题)。

如何添加按钮和回调以提交?

cab*_*tor 5

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' );
Run Code Online (Sandbox Code Playgroud)

它能做什么:

  1. 在右侧发布框的正上方添加一个自定义元框。
  2. 当用户点击“Call-to-action button text here”按钮(记得重命名!)时,它会通过Ajax将当前帖子(请求)的ID发送到一个名为 的函数wp645397_send_request_email,你需要在那里处理数据并发送电子邮件。

我在代码中添加了一些注释来解释发生了什么。如果你有任何问题,不要犹豫,问,好吗?