【问题标题】:Wordpress custom button/action custom post typeWordpress 自定义按钮/动作自定义帖子类型
【发布时间】:2018-07-16 14:34:47
【问题描述】:

我有一个使用工具集插件创建的名为request 的自定义帖子类型。

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

  • 新的request (post-new.php)

  • 编辑request (post.php)

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

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

【问题讨论】:

  • 这个按钮应该做什么?我不太清楚你想做什么。
  • 每次管理员提交按钮时,它都应该发送一封包含帖子信息(帖子标题、自定义字段等)的电子邮件
  • 好的,因为在您创建帖子时还没有关于帖子的信息 (post-new.php),我建议您仅在“编辑请求”屏幕上执行此操作。在创建请求后,您将被重定向到 post.php。这有意义吗?
  • 有道理。不需要在新岗位。在edit.php中怎么办?

标签: wordpress


【解决方案1】:

编辑后屏幕添加新元素的一种方法是通过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. 在右侧发布框的正上方添加一个自定义元框。
  2. 当用户点击“Call-to-action button text here”按钮(记得重命名它!)时,它会通过 Ajax 将当前帖子(请求)的 ID 发送到一个名为 wp645397_send_request_email 的函数,您需要在其中处理数据并发送电子邮件。

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

【讨论】:

    【解决方案2】:

    我不确定工具集插件提供了哪些选项,但如果我以编程方式执行此操作,我会执行类似的操作

    $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_scriptwp_localize_script

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 2013-08-03
      相关资源
      最近更新 更多