1
votes

I have a custom post type called request created with Toolset plugin.

I need to add a button in these admin pages (only for this custom post type):

  • new request (post-new.php)

  • edit request (post.php)

When admin submit the button I need to send an email (no problem for this) with info about the post.

How can I add the button and a callback to submit?

2
What should this button do? It's not really clear to me what it is you're trying to do. - cabrerahector
It should send an email with post info (post title, custom field, etc) every time admin submits the button - aletede91
Alright, since there's no info about the post yet when you're creating it (post-new.php) I would advice doing this on the Edit Requests screen only. Right after creating a request you'll be redirected to post.php anyways. Does that make sense? - cabrerahector
It makes sense. No need in new-post. How can I do in edit.php? - aletede91

2 Answers

6
votes

One way to add new elements to the Post Edit screen is via metaboxes.

Please check the following code:

/**
 * 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' );

What it does:

  1. Adds a custom metabox right above the Publish box on the right.
  2. When the user clicks on the "Call-to-action button text here" button (remember to rename it!), it'll send the ID of the current post (request) via Ajax to a function called wp645397_send_request_email, where you need to process the data and send the e-mail.

I added some comments in the code that should explain what's going on. If you have any questions don't hesitate to ask, ok?

0
votes

I am not sure what options toolset plugin provides but if I were to do this programatically, I would do something like

$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. 
}

Alternatively You can add a button using jquery and trigger your email function using ajax on this button click.

To do so you will need to look into admin_enqueue_script and wp_localize_script