Bug with URL Rewriting
-
Hello,
This otherwise great plugin breaks when used together with the WPML Plugin.
In the TinyMCE Templates 1.5 the author uses rewrite rules to add a mapping in WordPress. This is bad on several levels, first of all, the wpml uses rewriting to prepend the en/ prefix to all rules, and since the templates plugin can not find its rules, the flush_rewrites happens on each call to a foreign page. Secondly (and even worst problem) the rewrite flush happens BEFORE init, which is a big no-no according to the WordPress Codex. By flushing the rewrites before init, the rewrites for custom post types are not being generated and permalinks to these pages are broken. In a normal configuration the problem can be easily solved by going to the Permalinks page of the settings and by clicking on update. However, when the wpml plugin is used, the rewrite flush happens on each call of the foreign language page and the website breaks completely.Suggested fix
Don’t use rewrite for the ajax call, write a php wrapper that instantiates and calls the function statically. Adjust the loadTinyMCEPlugin and get_templates functions a bit ($this->list_url = TINYMCE_TEMPLATES_PLUGIN_URL.'/includes/mce_templates.php?mce_templates=true';
andpublic function get_templates(){ if (isset($_GET['mce_templates'])) { $u = wp_get_current_user(); if (!$u->ID) { header("HTTP/1.1 404 Not Found"); echo "404 Not Found."; exit; } global $wp_rewrite; global $wpdb; $list_url = TINYMCE_TEMPLATES_PLUGIN_URL.'/includes/mce_templates.php?mce_templates=true&'; if( isset($_GET['id']) && strlen($_GET['id']) ){ $sql = "select html from ".$wpdb->prefix."mce_template where (<code>ID</code>=%s) and (<code>author</code>=%d or <code>share</code>=1) order by <code>modified</code> desc"; $sql = $wpdb->prepare($sql, $_GET['id'], $u->ID); $template = $wpdb->get_var($sql); if ($template) { echo apply_filters( "tinymce_templates", wpautop(stripslashes($template)), stripslashes($template) ); } exit; } $sql = "select * from ".$wpdb->prefix."mce_template where <code>author</code>=%d or <code>share</code>=1 order by <code>modified</code> desc"; $sql = $wpdb->prepare($sql, $u->ID); $row = $wpdb->get_results($sql); header( 'Content-Type: application/x-javascript; charset=UTF-8' ); echo 'var tinyMCETemplateList = ['; $arr = array(); foreach ($row as $tpl) { $ID = esc_html($tpl->ID); $name = $tpl->name; $desc = esc_html($tpl->desc); $arr[] = "[\"{$name}\", \"{$list_url}id={$ID}\", \"{$desc}\"]"; } echo join(',', $arr); echo '];'; exit; } }
) and remove the addrewriterules.class completely
The php wrapper should look something like this:ob_start(); /// load wordpress framework define( 'IFRAME_REQUEST' , true ); if ( !defined('ABSPATH') ) { $wp_relative_root = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/' ; require_once( $wp_relative_root . 'wp-admin/admin.php'); } if ( !is_admin() ) { wp_die ( __( 'Invalid form submission.' ) ); } ob_end_clean(); require_once("TinyMCETemplate.class.php"); TinyMCETemplate::get_templates();
- The topic ‘Bug with URL Rewriting’ is closed to new replies.