• Resolved threepears

    (@threepears)


    Hello….I searched these forums, but couldn’t seem to find what I was looking for. I am trying to remove the p tags from any image I attach to my widgets, but can’t figure out how to do so. Even when I add code to my functions.php file that works in removing p tags elsewhere on the site, the ones in these widgets seem not to budge. And even if I remove them manually in the editor, once I save a change I make to any text, the p tags come right back! Is there a way anyone knows of to prevent images from getting p tags and keeping them off for good? Many thanks!

    https://wordpress.org/plugins/black-studio-tinymce-widget/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Marco Chiesi

    (@marcochiesi)

    Hi threepears, in the current version of the plugin the widget editor behavior (in terms of HTML code rewriting) is slightly different from the native one used by WP in posts/pages, even if the final HTML output is the same.
    Our plugin uses the native TinyMCE editor behavior, which automatically adds <p> tags to HTML, while WP uses a proprietary js function that removes <p> tags and then uses a php function (wpautop) that adds them back when rendering the page.
    I suppose the code you are using removes the application of this wpautop function with a remove_filter. It does not work in widgets for two reasons: 1. the wpautop is not natively applied to widgets text and 2. BST widgets have already the <p> inside (see above), so even if you don’t apply wpautop they are present.
    I don’t exclude that future releases of the plugin may work differently from now (there’s currently some reworking about this), but in the meantime you may apply a workaround based on filtering widgets text with a function that reverses the wpautop.
    Unfortunately such a function doesn’t exist in the current WP API (there’s only a js version, not PHP), but someone created one.
    So you may try the following code snippet (not tested):

    function reverse_wpautop( $s ) {
        //remove any new lines already in there
        $s = str_replace( "\n", "", $s );
        //remove all <p>
        $s = str_replace( "<p>", "", $s );
        //replace <br /> with \n
        $s = str_replace( array( "<br />", "<br>", "<br/>" ), "\n", $s );
        //replace </p> with \n\n
        $s = str_replace( "</p>", "\n\n", $s );
        return $s;
    }
    add_filter( 'widget_text', 'reverse_wpautop' );

    Thread Starter threepears

    (@threepears)

    Thank you, Marco! I tried to use this code, but it doesn’t seem to work. I’m wondering … if the wpautotop is not natively applied to widgets and BST widgets already have the <p> inside, wouldn’t I want to find a way to remove the <p> from BST (TinyMCE) itself? I guess I’m just a little confused as to what we’re aiming to do, and it seems to me we could leave wpautotop alone if it doesn’t even touch the widgets. Am I thinking of this wrong? Thank you for all your help!

    Plugin Author Marco Chiesi

    (@marcochiesi)

    I just tested the above code and it works for me. How did you put it in your site?

    Important note: this particular behavior related to automatic paragraph addition will be changed in the upcoming 2.0 release of the plugin. The behavior will be the same as in pages/posts editor (that is the WP native one). This also means that with the new version you’ll need a different code to remove the <p> from the BSTW widgets.

    So for BSTW 1.x use the code above.

    For BSTW 2.x use the code below (not tested):

    add_action( 'init', 'remove_bstw_wpautop' );
    function remove_bstw_wpautop() {
        remove_filter( 'widget_text', array( bstw(), 'widget_text_wpautop' ), 30 );
    }

    The version 2.0 is actually in development but it’s almost complete. If you want to try it, you can get it from our GitHub repo.

    Plugin Author Marco Chiesi

    (@marcochiesi)

    The final 2.0 release has been published, and the code to remove the wpautop filter is slightly different from the one above:

    add_action( 'init', 'remove_bstw_wpautop' );
    function remove_bstw_wpautop() {
        if ( function_exists( 'bstw' ) ) {
            remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 40 );
        }
    }
    Plugin Author Marco Chiesi

    (@marcochiesi)

    Further (and I hope final) update of the code snippet to disable wpautop for version 2.0.4+:

    add_action( 'init', 'remove_bstw_wpautop' );
    function remove_bstw_wpautop() {
        if ( function_exists( 'bstw' ) ) {
            remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
        }
    }

    Thread Starter threepears

    (@threepears)

    Hello Marco…..thank you for all your replies! I’m sorry I’m just now getting back to working on this issue. The new plugin seems to work fine, and there’s even an “Automatically add paragraphs” button (which I assume adds <p> tags). I used the last code snippet you gave above in my functions.php file, and it does strip out the <p> tags….but it strips out ALL of them, it seems. My original problem was that I wanted <p> tags for paragraphs, but NOT for images. Is there a way to accomplish this? I can do what I need to make edits to the site, but I’m trying to make this easier for the client so I can set some CSS rules that will always work for them…..and to do that, I need the images to have no <p> tags. Can you help? Many thanks!!

    Thread Starter threepears

    (@threepears)

    One more note, Marco. When I attempt to make an edit in “Visual” mode and then hit “Save”, like a client would, it kicks me immediately over to “Text” mode (or HTML editing) with no changes saved and <p> tags added back to where I removed them from. This seems to happen with or without the function you listed above in my functions.php file, and happens without the “Automatically add paragraphs” button checked.

    Like I said, I wouldn’t care if it was just me, as I prefer editing in “Text” mode, but I’m trying to make this easy for the client. Any ideas about what’s happening here? Thank you very much!

    Plugin Author Marco Chiesi

    (@marcochiesi)

    Hi there,
    if you want automatic <p> applied only to text and not to images, you should code your own version of the WordPress wpautop function, and I believe it wouldn’t be an easy task.
    You may stay in Text mode, disable the “Automatically add paragraphs” option and write the <p> tags manually where you need them. This could be however not very client-friendly.
    Alternatively you may use a custom function attached to the widget_text filter hook to remove <p> around images before displaying, using a regex replace.

    As for the other issue about text/visual mode not being saved, I think it could be a bug introduced after WP 4.1 release. I will look into it.

    Plugin Author Marco Chiesi

    (@marcochiesi)

    The new version 2.2.2 should fix the bug. If you’re still having issues, please open a new topic as this is marked as resolved.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Remove p tag on images’ is closed to new replies.