The MU forums have moved to WordPress.org

How to stop kses stripping class and id from tags (26 posts)

  1. donncha
    Key Master
    Posted 14 years ago #

    OK, when I synced WPMU's kses with that in WordPress it caused a lot of bother for people. Understandably so because markup that worked before was now stripped from posts.

    Well, there's good news, and there's bad. First the bad news. Kses.php will be synced with WP's again and class and id attributes will be stripped by default.
    The good news, is that in http://trac.mu.wordpress.org/changeset/1040 I added two filters so that $allowedposttags and $allowedtags can be modified by a plugin.
    You'll be able to use a function like the to remove whatever attributes you like. Just put it into a file in mu-plugins/ somewhere.

    function addabitofclass( $tags ) {
            foreach( $tags as $tag => $attr ) {
                    if( is_array( $attr[ 'class' ] ) ) {
                            unset( $attr[ 'class' ] );
                            $tags[ $tag ] = $attr;
                    }
            }
            return $tags;
    }
    add_filter( 'edit_allowedposttags', 'addabitofclass' );

    If you're the author of a plugin that uses any of the attributes that are stripped then make sure you're using the filter above to remove that attribute from the array.

  2. conoro
    Member
    Posted 14 years ago #

    Thanks Donncha, appreciated.

    I've contacted one organisation that uses WPMU and asked them if they would consider building a generic plug-in for this with an admin interface to configure which attributes are in/out. I'll let you know if they do.

  3. donncha
    Key Master
    Posted 14 years ago #

    Bah, I don't know what I was thinking when I wrote the function above. The corrected code is on my blog post here: http://ocaoimh.ie/2007/09/10/wordpress-mu-125/

  4. alpha2zee
    Member
    Posted 14 years ago #

    Try htmLawed -- it can be used to selectively allow/restrict HTML tags/attributes, and is WordPress-compatible.

  5. woowoowoo
    Member
    Posted 14 years ago #

    Donncha... OK - I am a bit of a noob at this - patience please. I am running MU 1.3 but each time I try the code from your blog I get a nasty php error. Can you please use shorter words or something ;-) I really need to get this running.

    I'm guessing this kses.php file is the same reason some <input> tags have vanished from my posts?

  6. boonika
    Member
    Posted 14 years ago #

    Same problem.

  7. boonika
    Member
    Posted 14 years ago #

    IT'S WORKING!!!

    Create kses.php file and add this code inside:

    <?php
    
    function addabitofclass( $tags ) {
        global $allowedposttags;
        foreach( $allowedposttags as $tag => $attr ) {
            $attr[ 'class' ] = array();
            $attr[ 'id' ] = array();
            $allowedposttags[ $tag ] = $attr;
        }
        return $allowedposttags;
    }
    add_filter( 'edit_allowedposttags', 'addabitofclass' );
    ?>

    Save it and upload it to mu-plugins folder. That's it. Big thanks to Donncha and Farms (for pointing to this topic).

  8. woowoowoo
    Member
    Posted 14 years ago #

    ahh - at last!
    I had done everything right - except that the code I had copied from donncha's blog post using Safari had brought with it strange white space characters - invisible in my text editor.
    It wasn't until I did a diff with the code I copied from this page, and the visually identical lines showed up as different - that I turned on the invisible characters and saw my problem.
    Thanks so much for this!

  9. boonika
    Member
    Posted 14 years ago #

    @woowoowoo

    Have You tried displaying YouTube movies using lightwindow effect? If You did, what movie player are you using?

    Thanks

  10. stinkypup
    Member
    Posted 14 years ago #

    Can someone help me with this? All I want to do is be able to use CSS styles in my postings (e.g., div class="rightwrap"). I've added the code boonika mentions to a kses.php file in mu-plugins. Note, there's a kses.php file in the includes directory. I'm running wpmu 1.3.3

    In the kses.php file in the includes dir, there's a comment "You can override this in your my-hacks.php file." What/where is the my-hacks.php file? where should it live?

    TIA

  11. taiwanese
    Member
    Posted 14 years ago #

    @stinkypup
    I don't think you need to worry about my-hacks.php
    as long as you put the file in mu-plugin it should work

  12. stinkypup
    Member
    Posted 14 years ago #

    i put the file in mu-plugin, but it doesn't work. my tags are still being stripped.

  13. taiwanese
    Member
    Posted 14 years ago #

    Are you sure you did correctly?
    Because it works alright on my site..

  14. stinkypup
    Member
    Posted 14 years ago #

    It's working now. Seems that I need to use tic marks instead of quotes for the class, eg., class="right" doesn't work, class='right' works.

  15. tmdelamar
    Member
    Posted 13 years ago #

    I added boonka's code as well but got this error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/greetmyb/public_html/wp-content/mu-plugins/kses.php:2) in /home/greetmyb/public_html/wp-includes/pluggable.php on line 776

    Can anyone help with this?

  16. andrea_r
    Moderator
    Posted 13 years ago #

    Look for whitespace at the end of the file, especially after you've edited it.

  17. tmdelamar
    Member
    Posted 13 years ago #

    Thanks. It's working now.

  18. rengel
    Member
    Posted 13 years ago #

    Hi,
    I just (3 days ago) did a "dirty" upgrade from wp 2.7 to wpmu 2.6.5.
    I've done exactly what boonika says: create kses.php file (I assume empty page) and paste the above code into it. Upload it into mu-plugins folder (was empty except a readme txt file), and?! nothing at all. It doesn't show up as a new plugin and of course wpmu still strips my code.
    Here is the code I want to use (IFrames)

    <div align="center">
      <center>
      <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="95%" id="AutoNumber1">
        <tr>
          <td width="100%">
          <p align="center">
          <iframe name="I1" marginwidth="1" marginheight="1" width="504" align="middle" border="0" frameborder="0" height="378" scrolling="no" src="http://ralfengel.com/presentations/2070/2071_1.htm">
          El explorador no admite los marcos flotantes o no est configurado actualmente para mostrarlos.
          </iframe></td>
        </tr>
      </table>
      </center>
    </div>

    I've really been trying to find out what is wrong, but I guess I need help.

  19. andrea_r
    Moderator
    Posted 13 years ago #

    kses.php is a file that already exists in MU.

  20. diesel12
    Member
    Posted 13 years ago #

    @andrea: I hear you on kses.php aleady existing, but a previous poster mentioned they maintained that name and placed it in mu-plugins with success.... it hasn't worked for me under any name...any pointers on what this file should be named when placed in mu-plugins or if that is the right route to go? Is this the optimal way to allow tags / classes to be added to posts? Moving sites to MU from single installs and they want their tags back! :)

  21. diesel12
    Member
    Posted 13 years ago #

  22. SharkGirl
    Member
    Posted 13 years ago #

    $50 for the additional-tags plugin? I'm assuming once the membership is paid (a monthly fee of $50) then you get the script?

    Guess I'll try the other options posted to get MU 2.7 to stop stripping code. It would seem that feature should be a standard feature for MU. I don't know of anyone that wants their paragraphs and page breaks to be stripped from their paragraphs.

    Without the features, all the posts end up showing a big paragraph smashed together. Why strip simple text html code?

  23. VentureMaker
    Member
    Posted 13 years ago #

    $50 for the additional-tags plugin?

    No, $50 for monthly access to all plugins, themes, videos, extras and forum :)

  24. modifiedcontent
    Member
    Posted 13 years ago #

    Is there a working plugin for this somewhere?

    I assume boonika's code is not ready-to-use ('addabitofclass'?!). If you're not a PHP coder, it's not at all obvious what to do with it.

    I'm trying to transfer a video blog to WP MU/BuddyPress. If I can't object/embed without have to manually rewrite 165 posts I can't use WP MU.

  25. VentureMaker
    Member
    Posted 13 years ago #

  26. awarner20
    Member
    Posted 13 years ago #

    Will this accomplish what you need?
    http://wordpress.org/extend/plugins/unfiltered-mu/

    I am using it on my MU install.

    By the way, the premium membership is worth it. As VentureMaker said, the $50 per month allows you to access to everything, and it's worth it. If you only want to pay one month, that's ok too, and that's what I love about their business model over there.

    It gives the little guy (me) access to some really great plugins, themes, etc. for a price that I can afford. As I progress with my web development projects and have a need for more advanced or "niche" features, I know where to go without having to be locked into a $50 a month payment on a continuing basis.

About this Topic

  • Started 14 years ago by donncha
  • Latest reply from awarner20