WordPress.org

Support

Support » Plugins and Hacks » Custom Plugin Refuses to Play Nice (I am going crazy!)

Custom Plugin Refuses to Play Nice (I am going crazy!)

  • I am trying to write a very basic plugin, but something is going wrong and I don’t quite know where to begin hunting this problem down.

    The code
    function xalign_filter_code($content){
    $bad_code = array('<p align="left">','<p align="center">','<p align="right">');
    $good_code = array('<p style="text-align:left">','<p style="text-align:center">','<p style="text-align:right">');
    for($x=0;$x<3;++$x){
    $content = str_replace($bad_code[$x],$good_code[$x],$content);
    }
    return $content;
    }

    add_filter('content_save_pre', 'xalign_filter_code', 10);
    add_filter('pre_comment_content', 'xalign_filter_code', 10);

    The problem is that nothing happens to the “align” attributes, but the bigger problem is that if I add something else to the arrays that I know is in the post text it is replaced!

    The plugin is meant to clean up the invalid align attribute that the rich text editor (tinyMCE) uses. Is it the hook I am using? Does tinyMCE do something weird that avoids being hooked into?

    Any help big or small would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • So it took some trickery, but the problem was actually pretty simple to fix. Apparently, the quotes are all escaped so the simple search wouldn’t find them.

    by change these two lines

    $bad_code = array('<p align="left">','<p align="center">','<p align="right">');
    $good_code = array('<p style="text-align:left">','<p style="text-align:center">','<p style="text-align:right">');

    to this

    $bad_code	= array('<p align=\"left\">','<p align=\"center\">','<p align=\"right\">');
    $good_code	= array('<p style=\"text-align:left\">','<p style=\"text-align:center\">','<p style=\"text-align:right\">');

    everything works fine, now I have proper xhtml that functions as it should.

    I was about to write this plugin. It’s been a pet peeve of mine for a long time that the tinymce editors does non xhtml aligns. I thought, why not do a quick search of the forum to see if someone else has already done this? And there this thread is. 🙂

    So, thanks, you saved me a bit of work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Plugin Refuses to Play Nice (I am going crazy!)’ is closed to new replies.