dimanche 1 février 2015

jquery tinymce and bundled wp plugins


So I managed to get dynamically generated versions of jquery tinymce to work in wordpress and work with the bundled wplink js plugin - albeit pretty badly hacked together.


Was wondering if anyone has figured out or can deduce from my hack a more elegant way to load the exact scripts I need to get wp link working. I checked this thread but cannot seem to get the bundled wp link plugin to load properly. I get the error



wplink.js?ver=4.1:142 Uncaught TypeError: Cannot read property 'select' of undefined


The start of my more elegant solution. This initialises the editor and its css;



$js_src = includes_url('js/tinymce/') . 'tinymce.min.js';
$css_src = includes_url('css/') . 'editor.css';

// wp_enqueue doesn't seem to work at all
echo '<script src="' . $js_src . '" type="text/javascript"></script>';

wp_register_style('tinymce_css', $css_src);
wp_enqueue_style('tinymce_css');

// Something needs to be called before this...
wp_enqueue_script('wplink');

...tinymce.init({ ...


My hacked together version (I use a wordpress dev stack, so some dir paths are different);



// Get the necessary scripts to launch tinymce
$baseurl = includes_url('js/tinymce');
$version = 'ver=' . $tinymce_version;

$cssurl = includes_url('css/');
$css = $cssurl . 'editor.css';

$compressed = $compress_scripts && $concatenate_scripts && isset($_SERVER['HTTP_ACCEPT_ENCODING'])
&& false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');

if ( $compressed ) {
echo "<script type='text/javascript' src='{$baseurl}/wp-tinymce.php?c=1&amp;$version'></script>\n";
} else {
echo "<script type='text/javascript' src='{$baseurl}/tinymce.min.js?$version'></script>\n";
echo "<script type='text/javascript' src='{$baseurl}/plugins/compat3x/plugin.min.js?$version'></script>\n";
}



add_action( 'wp_print_footer_scripts', array( '_WP_Editors', 'editor_js' ), 50 );
add_action( 'wp_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ), 1 );

wp_enqueue_script('wplink');

require_once(get_home_path() . 'wp/' . WPINC . "/class-wp-editor.php");

_WP_Editors::wp_link_dialog();

wp_register_style('tinymce_css', $css);
wp_enqueue_style('tinymce_css');

// initialise the editor
...
tinymce.init({
...stuff...
plugins: ["wordpress wpdialogs wplink"], // Throwing stuff at the wall here
toolbar: "bold italic underline | link unlink"
})...

apply_filters ('tiny_mce_plugins', array('wplink'));


I have an idea I will need to load the 'wp dialogs' plugin before as 'wp link' seems to rely on this (as my colleague helped me hack in).


The analogy of 'don't fix what ain't broken' comes to mind, but considering there seems to be next to no information on this stuff I was hoping this could help people in future.





Aucun commentaire:

Enregistrer un commentaire