Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I added a custom button to the TinyMCE editor, and I want to open WP's Thickbox when I click on it.

How can I make it so that the tb_show() function loads the content I want with ajax?

// the ajax
add_action('wp_ajax_getTheContent', 'getTheContent');
function getTheContent(){
  echo 'weqwtegeqgr'; // <- this should be displayed in the TB
  die();
}

Here's some of the editor plugin code I'm using:

init : function(ed, url) {
  ed.addButton('do_stuff', {
    title : 'Do Stuff',
    image : url + '/icon.gif',
    onclick : function() {
        OpenMyThickbox('do_stuff');
    }
  });
...

So the OpenMyThickbox javascript function should do what I want:

function OpenMyThickbox(tag){
  tb_show(tag, '...'); // <- how to load content trough ajax here ?
}
share|improve this question
    
use admin_url('admin-ajax.php'); for url –  Bainternet Apr 18 '11 at 6:50
    
I get a blank page. even if I enter google.com –  onetrickpony Apr 18 '11 at 8:32

2 Answers 2

up vote 6 down vote accepted

The second parameter for tb_show is the URL, so you'll want to use something like..

<?php 
$ajax_url = add_query_arg( 
    array( 
        'action' => 'getTheContent', 
        'query_var1' => 'value1', 
        'query_var2' => 'value2' 
    ), 
    admin_url( 'admin-ajax.php' ) 
); 
?>
tb_show(tag, <?php echo $ajax_url; ?> ); 

I'd guess you need to pass the action and any additional query vars manually(as per above), else your request is simply for admin-ajax.php, when what you are looking for is something along the lines of ... admin-ajax.php?action=getTheContent&someothervar=someothervalue, hence the add_query_arg usage above..

For clarification:

The following call to add_query_arg ...

add_query_arg( 
    array( 
        'action' => 'getTheContent', 
        'query_var1' => 'value1', 
        'query_var2' => 'value2' 
    ), 
    admin_url( 'admin-ajax.php' ) 
);

Is equivalent to and will produce ...

http://example.com/wp-admin/admin-ajax.php?action=getTheContent&query_var1=value1&query_var2=value2

However!

Having now explained myself i've come to realise we don't want the absolute URL and thus don't need the call to admin_url in there. The code should instead be.

<?php 
$ajax_url = add_query_arg( 
    array( 
        'action' => 'getTheContent', 
        'query_var1' => 'value1', 
        'query_var2' => 'value2' 
    ), 
    'admin-ajax.php'
); 
?>
tb_show(tag, <?php echo $ajax_url; ?>); 

So the resulting URL looks something like this..

admin-ajax.php?action=getTheContent&query_var1=valu1&query_var2=value2

Functions referenced in the above code samples:

share|improve this answer
    
Won't that be a $_get['action'] because WordPress ajax call looks for $_post['action']? or am i wrong? –  Bainternet Apr 18 '11 at 13:28
    
It'll be $_POST or $_REQUEST inside an ajax callback typically, unless you explicitly set your ajax request function to use get instead, in the context of a thickbox call, we use a query string, which should still show up in $_REQUEST (because that usually holds both $_GET and $_POST).. –  t31os Apr 18 '11 at 13:42
    
No, what i'm asking is if i use add_query_arg will the args added be sent as a post or get? –  Bainternet Apr 18 '11 at 13:45
    
Neither, add_query_arg appends a string(of a url) with query parameters, it doesn't do any sending, so i'm not sure i understand your question.. –  t31os Apr 18 '11 at 13:49
    
Updated my answer.. –  t31os Apr 18 '11 at 13:55

Messing up javascript and PHP is not very clever. This answer only confuse.

tb_show is javascript add_query_arg is PHP

so this solution is only valid in PHP and the proper code is

...
?>
tb_show(
  'whatever',
  <?php echo add_query_arg( array(
    'action' => 'getTheContent',
    'query_var1' => 'value1',
    'query_var2' => 'value2',
  ), 'admin-ajax.php'); ?>
);
<?php
...

And in javascript is not valid at all as we can't use add_query_arg

share|improve this answer
    
Where is that different from the solution aside from it having the <?php ?> tags in it? If that's all that you want to point out, then please make an edit to the solution to correct it. No need to add a separate answer to explain what's wrong with another answer. That's why community edits are appreciated. –  kaiser Dec 5 '12 at 13:29
    
@kaiser Edits changing the meaning of a post are usually not welcome. As a separate answer this is okay. –  toscho Dec 5 '12 at 13:32
    
@toscho And how exactly is this changing the meaning ? Imo it's correcting or adding the missing bits. –  kaiser Dec 5 '12 at 13:36
    
Bayen was correct to point out the mistake here, i was mixing PHP and Javascript, i really should of spotted that sooner. I've made the necessary changes to my answer, and thanks for the feedback bayen(i like to know when i make errors). –  t31os Apr 11 '13 at 17:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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