简体   繁体   中英

Wordpress Plugin Shortcodes - Automatic breaks for content but not for HTML

I am creating a private wordpress shortcode for an accordion, by using a simple plugin, created by myself.

The problem: Wordpress adds breaks and paragraphs to my HTML outcome, which leads to a broken design.

I can remove autobreaks, but then it also does not work for the real content, which has paragraphs. But I dont want to use p-tags in the backend.

How can I stop wordpress from doing autobreaks to my HTML, but still doing it for my wrapped content?

The solution has to work within a plugin - without affecting other plugins/functions.

I can remove any spacing between my shortcodes, so wordpress does not add any breaks. But thats very ugly to read.

This way it works:

[os_accordion][os_spoiler title="Title"]Content with some

breaks.[/os_spoiler][/os_accordion]

This way it does not:

[os_accordion]
[os_spoiler title="Title"]
Content with some

breaks.
[/os_spoiler]
[/os_accordion]

I tried to use a function for removing breaks, but it does not work.

//Helper to remove autops
function cleanup_shortcode_fix($content) {
    $array = array('<p>[' => '[', ']</p>' => ']', ']<br />' => ']', ']<br>' => ']');
    $content = strtr($content, $array);
    return $content;
}

//Outer accordion wrapper
function function_accordion($atts, $content = null){
    $html = '<div class="accordion">'.do_shortcode($content).'</div>';
    return cleanup_shortcode_fix($html);

}
add_shortcode('os_accordion', 'function_accordion');

//Inner wraps
function function_spoiler($atts, $content = null){

    //set default attributes and values
    $values = shortcode_atts( array(
        'title'     => '',
    ), $atts );

    //Output buffer
    ob_start();
    ?>

    <div class="toggle"><?php echo esc_attr($values['title']); ?></div>
    <div class="content"><?php echo $content; ?></div>

    <?php
    return ob_get_clean(); //Close buffer and return data
}
add_shortcode('os_spoiler', 'function_spoiler');

Any ideas how I can make the last version work? I'm stuck. :/

This should do it. It's probably just putting p tags after everything.

remove_filter('the_content', 'wpautop');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM