dimanche 4 octobre 2015

wordpress tinymce tb_show custom php file won't insert content

I'm have a tinymce button that opens a popup using a php file in which I query a custom post type and list all the titles. However, I cannot get the insert button to place the content in the wysiwyg.

My javascript:

// closure to avoid namespace collision
(function(){
    // creates the plugin
    tinymce.create('tinymce.plugins.custom_mce_button', {
        init : function(ed, url) { // creates control instances based on the control's id
            ed.addButton('custom_mce_button', {
            title: 'Test',
            icon: 'custom-mce-icon',
            text: 'Shortcode',
            onclick : function() { // triggers the thickbox
                tb_show( 'Tabs Shortcode', '../wp-content/plugins/my-plugin-folder/get-shortcodes.php?width=600&height=400&inlineId=btn-form' );
                jQuery('#TB_ajaxContent').css({'width': '640', 'height': (jQuery('#TB_window').height()-50)+'px'});
                jQuery(window).resize(function(){
                    jQuery('#TB_ajaxContent').css({'width': '640', 'height': (jQuery('#TB_window').height()-50)+'px'});
                });
        }
    });
    }
    });

    tinymce.PluginManager.add('custom_mce_button', tinymce.plugins.custom_mce_button); // registers the plugin


    jQuery(function($){ // executes this when the DOM is ready
        // creates a form to be displayed everytime the button is clicked
        // you should achieve this using AJAX instead of direct html code like this

        var form = jQuery('#TB_ajaxContent');

        var popup_content = form.find('.popup_content');
        form.appendTo('body').hide();

        form.find('#btn-submit').click(function(){ // handles the click event of the submit button

            var shortcode = '[MyShortcode';

            var PostId = find('#shortcodeID').val();

            shortcode += ' id="'+PostId+'"';

            shortcode += ']';

            tinyMCE.activeEditor.execCommand('mceInsertContent', 0, shortcode); // inserts the shortcode into the active editor

            tb_remove(); // closes Thickbox
        });
    });
})()

My PHP file called in the javascript:

define('WP_USE_THEMES', false);
require('../../../wp-load.php');

$query = new WP_Query( array( 'post_type' => 'custom_type') );

echo'<div id="btn-form">';
echo'<div class="popup_content">';
echo'<select name="shortcodeID" id="shortcodeID">';
while ( $query->have_posts() ) : $query->the_post();
    $title = the_title();
    echo '<option value="'.$post->ID.'">'.get_the_title().'</option>';
endwhile; wp_reset_postdata();
echo'</select>';
echo'<input type="button" id="btn-submit" class="button-primary" value="Insert Shortcode" name="submit" />';
echo'</div>';
echo'</div>';

Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire