首页 文章

在WordPress 4.9中删除图像,视频,iframe周围的自动P标签

提问于
浏览
0

问题

我正在尝试调整我正在插入的YouTube视频的大小,并且我使用了一些有效的CSS来正确设置样式,但是WordPress不断在我的视频,图片和iframe标签周围添加<P>标签这一事实破坏了我的代码 . 我只是试图从博客内容部分(不是侧边栏,窗口小部件,页脚等)中的元素(视频/ img / iframe)中删除段落标记换行

客户网站:

本网站面向客户,因此代码必须尽可能不引人注目 . 例如,一个好的解决方案是自定义短代码,或者允许我使用CSS样式规则来定位视频的东西 .

以下是相关网站上的博文:http://ehepperle.com/in-progress/feelheal/11/how-to-create-125-px-ad-banner-gimp/

img

我的系统

我的代码

由于这是特定于WordPress过滤器而不是一般编码概念,我还没有找到一种方法来制作一个很好的演示示例,但这里有代码片段显示我尝试过的内容:

functions.php - ver . 1

// Remove P Tags Around Images 
// From: http://justlearnwp.com/remove-p-tag-around-wordpress-images/
function filter_ptags_on_images($content){
    return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

functions.php - ver . 2

// https://stackoverflow.com/questions/44539888/remove-automatic-p-tag-in-wordpress#answer-44540531
function reformat_auto_p_tags($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }

    return $new_content;
}

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'reformat_auto_p_tags', 99);
add_filter('widget_text', 'reformat_auto_p_tags', 99);

LINKS我在发布之前就已经回顾过了

我尝试过从这些不同的可能解决方案拼凑而成的解决方案,但都无济于事 .

我的问题

以下问题实际上是从不同角度收集最完整理解的同一主要问题的多个方面 .

  • 如何从WordPress中的iframe,视频和图片代码中删除<p>标签?我过去2年(以及之前)的方法似乎不起作用 .

  • 我的代码出了什么问题?为什么删除wpautop过滤器不能在我的functions.php中工作?

  • WP版本4.8-4.9发生了什么变化,使得这些过滤黑客不再起作用了吗?

更新:2018-09-05

我正在添加我的完整functions.php文件,以防任何人可以帮助识别导致冲突的内容 . 我没有看到任何东西,但也许你会看到我错过的东西 .

functions.php(完整)

<?php
add_action( 'wp_enqueue_scripts', function() {
    //* Parent CSS
    wp_enqueue_style( 'storefront', 
      get_template_directory_uri() . '/style.css' );

    //* Child CSS
    wp_enqueue_style( 'storefront-ehw-1', 
      get_stylesheet_directory_uri() . '/style.css', [ 'storefront' ] );
} );

/* Adds search box to top nav menu
add_filter( 'wp_nav_menu_items','add_search_box', 10, 2 );
function add_search_box( $items, $args ) {
    $items .= '<li class="widget widget_search">' . get_search_form( false ) . '</li>';
    return $items;
}
*/

/* Hide categories from WordPress category widget
NOTE: 59 is the id of .Test-Posts category. This hides that from users.*/
function exclude_widget_categories($args){
    $exclude = "59";
    $args["exclude"] = $exclude;
    return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");

/* Add Google fonts */
function wpb_add_google_fonts() {

wp_enqueue_style( 'wpb-google-fonts', 'http://fonts.googleapis.com/css?family=Aguafina Script|Neucha|The Girl Next Door|Quintessential|Open Sans|Raleway', false ); 
}

add_action( 'wp_enqueue_scripts', 'wpb_add_google_fonts' );


/**
 * Display the theme credit
 *
 * @since  1.0.0
 * @return void
 */
function storefront_credit() {
    ?>
    <div class="site-info">
        <?php echo esc_html( apply_filters( 'storefront_copyright_text', $content = 'Copyright &copy; ' . date( 'Y' ) . ' ' . get_bloginfo( 'name' ) ) ); ?>
        <?php if ( apply_filters( 'storefront_credit_link', true ) ) { ?>

        
<?php echo '<a href="https://erichepperle.com" target="_blank" title="' . esc_attr__( 'Eric Hepperle Designs - Affordable eCommerce WordPress websites for small business', 'storefront-ehw-1' ) . '" rel="author">' . esc_html__( 'Designed by: Eric Hepperle Designs', 'storefront-ehw-1' ) . '</a>' ?> <?php } ?> </div><!-- .site-info -->/*<?php } /* **** VARIOUS ATTEMPTS TO REMOVE P-TAGS FROM YOUTUBE VIDEO THUMBNAILS **** // Remove P Tags Around Images // From: http://justlearnwp.com/remove-p-tag-around-wordpress-images/ function filter_ptags_on_images($content){ return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); } add_filter('the_content', 'filter_ptags_on_images'); // Remove P Tags Around videos function filter_ptags_on_vids($content){ return preg_replace('/<video>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/video>/iU', '\1\2\3', $content); } add_filter('the_content', 'filter_ptags_on_vids'); // https://stackoverflow.com/questions/44539888/remove-automatic-p-tag-in-wordpress#answer-44540531 function reformat_auto_p_tags($content) { $new_content = ''; $pattern_full = '{(\[raw\].*?\[/raw\])}is'; $pattern_contents = '{\[raw\](.*?)\[/raw\]}is'; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) { $new_content .= $matches[1]; } else { $new_content .= wptexturize(wpautop($piece)); } } return $new_content; } remove_filter('the_content', 'wpautop'); remove_filter('the_content', 'wptexturize'); add_filter('the_content', 'reformat_auto_p_tags', 99); add_filter('widget_text', 'reformat_auto_p_tags', 99); // ------------ FROM STACK OVERFLOW ------------------------ // // Remove p tags from images, scripts, and iframes. function remove_some_ptags( $content ) { $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); $content = preg_replace('/<p>\s*(<script.*>*.<\/script>)\s*<\/p>/iU', '\1', $content); $content = preg_replace('/<p>\s*(<iframe.*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content); return $content; } add_filter( 'the_content', 'remove_some_ptags' ); */ // Add the filter to manage the p tags add_filter( 'the_content', 'jb_remove_autop_for_image', 0 ); function jb_remove_autop_for_image( $content ) { global $post; // Here you can write condition as per your requirement. // i have added example for your idea if ( is_single() && $post->post_type == 'image' ) remove_filter('the_content', 'wpautop'); return $content; }

2 回答

  • 0

    在这里,我发送代码从图像脚本和iframe中删除p标签 .

    // Remove p tags from images, scripts, and iframes.
    function remove_some_ptags( $content ) {
    $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
    $content = preg_replace('/<p>\s*(<script.*>*.<\/script>)\s*<\/p>/iU', '\1', $content);
    $content = preg_replace('/<p>\s*(<iframe.*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
    return $content;
    }
    add_filter( 'the_content', 'remove_some_ptags' );
    

    好心检查 .

  • 0

    请尝试下面的代码 .

    // Add the filter to manage the p tags
    add_filter( 'the_content', 'jb_remove_autop_for_image', 0 );
    
    function jb_remove_autop_for_image( $content )
    {
        global $post;
    
         // Here you can write condition as per your requirement.
         // i have added example for your idea
        if ( is_single() && $post->post_type == 'image' )
            remove_filter('the_content', 'wpautop');
    
        return $content;
    }
    

相关问题