0

I've got a function hooked into load-edit.php that prints out print content:

add_action('load-edit.php', array($this, 'generate_bulk_print_content'));

public function generate_bulk_print_content() {
    //check for bulk action & print
}   

I prepared the bulk action trigger with the following (common) hack:

add_action( 'admin_footer-edit.php', array(&$this, 'add_to_bulk_actions'));

public function add_to_bulk_actions() {
    global $post_type;

    if ( 'shop_order' == $post_type ) {
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('<option>').val('custact').text('<?php _e( 'Custom action', 'woocommerce' )?>').appendTo("select[name='action']");
            jQuery('<option>').val('custact').text('<?php _e( 'Custom action', 'woocommerce' )?>').appendTo("select[name='action2']");

        });
        </script>
        <?php
    }
}       

all of this works like one would expect, except for the fact that I can only see my print content when I do die() or exit() after the content. However, I would like this print content to be displayed in a thickbox on the admin page rather than on a new (otherwise empty) page. When I try to access the action variables in a different location, it cannot access the action variables anymore. What's happening in between my click on the action button and the next moment when I'm on the edit page again? Where does my print content go?

Are there other ways to use the existing buttons and perhaps get my stuff into admin-ajax rather than the edit page (which is a full reload I think)?


edit: got a bit further, I realised I probably shouldn't use the default action of reloading the page when the button is clicked, and now I'm using the following JS:

$("#doaction, #doaction2").click(function (event) {
    var actionselected = $(this).attr("id").substr(2);
    if ($('select[name="' + actionselected + '"]').val() == "bulkprint") {
        event.preventDefault();
        var checked = [];
        $('tbody th.check-column input[type="checkbox"]:checked').each(
            function() {
                checked.push($(this).val());
            }
        );

        data = {
            action: 'bulkprint',
            orders: checked
        };

        $.post(ajaxurl, data, function (response) {
            //how to output this to the standard thickbox?
        });
    }
});

Now the last piece of the puzzle is to get my ajax response into a thickbox. tb_show is not going to work because that requires a regular link, whereas I would like to pass post data to the admin-ajax (instead of using arguments).

2
  • I really cannot understand what is "print content"... do you mean the <script>? And what thickbox is this? ::: Bulk actions do refresh the page. What does Ajax has to do with this? ::: Maybe some screenshots would be useful, and a bit more of code to give an idea of what's going on. ::: Is your problem like this?
    – brasofilo
    Commented Jan 17, 2013 at 0:09
  • Sorry for not being clear! By "print content" I mean plain HTML output that is formatted to be printed out (on paper). I know bulk actions refresh the page, but they also pass on the post id's. Now I'm able to get them into my generate_bulk_print_content() function by checking $wp_list_table = _get_list_table('WP_Posts_List_Table'); $action = $wp_list_table->current_action(); but only so when I die() at the end of that function. If I don't, the edit page get's loaded normally - even though I qould just like to add my script output to the page.
    – Ewout
    Commented Jan 17, 2013 at 8:18

1 Answer 1

0

Ok, I've solved my problem, though my question (why I need to put die() at the end of my load-* function to let it show) remains unanswered.

I couldn't find any way to use tb_show and at the same time send super global post variables to the destination url. tb_show simply calls a url and puts it in an iframe. I guess if I want the output to be truly ajax, I need to put a div somewhere in the page to be used by thickbox, and let the ajax call output to that div.

I finally settled for a method in which I pass the variables (which are just post id's in this case anyway) to the url and read those with $_GET.

javascript:

$("#doaction, #doaction2").click(function (event) {
    var actionselected = $(this).attr("id").substr(2);
    if ( $('select[name="' + actionselected + '"]').val() == "bulkprint") {
        event.preventDefault();
        var checked = [];
        $('tbody th.check-column input[type="checkbox"]:checked').each(
            function() {
                checked.push($(this).val());
            }
        );

        var order_ids=checked.join('x');
        url = 'admin-ajax.php?action=bulkprint&order_ids='+order_ids;

        tb_show('', url + '&TB_iframe=true&width=720&height=460');
    }
});

(nifty simple url encoding trick taken from stackoverflow)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.