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 have a custom post type with a metabox text input area that I want to have a shortcode inserted into from a thickbox when a link is clicked on from the modal window. Using the code below right now it just closes the modal window when the insert button is clicked. Please tell me where I am going wrong.

I have the thick box opening fine.

The page that is rendered has the link as this:

echo '<td><a href="#" style="display:block" class="button insertdownload" id="download-'.$d->id.'">Insert</a></td>';

I have the jQuery call as:

jQuery('.insertdownload').click(function(){

var did = jQuery(this).attr('id');

did=did.replace('download-', '');

if (jQuery('#format').val()>0) {

jQuery('#my_new_field').val('[download id="' + did + '" format="' + 

jQuery('#format').val() + '"]');

} else {

jQuery('#my_new_field').val('[download id="' + did + '"]');

}

window.parent.tb_remove();
});

I also have my metabox text field as:

<input type="text" size="25" name="myplugin_new_field" id="my_new_field">

This was the original code but I didn't want it to go to the editor window, I want it to go to my input field.

jQuery('.insertdownload').click(function(){
var win = window.dialogArguments || opener || parent || top;
var did = jQuery(this).attr('id');
did=did.replace('download-', '');
if (jQuery('#format').val()>0) win.send_to_editor('[download id="' + did + '" format="' + jQuery('#format').val() + '"]');
else win.send_to_editor('[download id="' + did + '"]');
});
share|improve this question

2 Answers 2

What your code in;

jQuery(document).ready(function(){

// your code here...

});

...and in fact your code should look like this;

jQuery(document).ready(function($){

    $('.insertdownload').click(function(){

    var did = 'this is only a test';

    did=did.replace('download-', '');

    if ($('#format').val('1')) {

    $('#my_new_field').val('[download id="' + did + '" format="' + 

    $('#format').val() + '"]');

    } else {

    $('#my_new_field').val('[download id="' + did + '"]');

    }

    window.parent.tb_remove(); 

    });

});
​

Check out the Fiddle here... LINKAGE

share|improve this answer
    
Thanks, but this doesn't work for me. I tried your code and now the window no longer closes and the value is not inserted. I also needed the check for the #format value to see if it is larger than zero (that is a select dropdown that I want to do the first part of the if/else statement if the value is larger than 0. –  Ken Apr 13 '12 at 15:40
up vote 0 down vote accepted

I got it, since it was an iframe I guess I was needing to target the parent window.

    jQuery('.insertdownload').click(function(){
    var did = jQuery(this).attr('id');
    did=did.replace('download-', '');
    if (jQuery('#format').val()>0) {
    parent.jQuery('#my_new_field').val('[download id="' + did + '" format="' + jQuery('#format').val() + '"]');
    } else {
    parent.jQuery('#my_new_field').val('[download id="' + did + '"]');
    }
    window.parent.tb_remove(); 
    });
share|improve this answer

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.