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

问题描述 投票:0回答:2

问题

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

CLIENT WEBSITE:

本网站面向客户,因此代码必须尽可能不引人注目。例如,一个好的解决方案是自定义短代码,或者允许我使用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);

我在发布之前回顾的链接

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

我的问题

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

  1. 如何从WordPress中的iframe,视频和图片代码中删除<p>标签?我过去2年(以及之前)的方法似乎不起作用。
  2. 我的代码出了什么问题?为什么删除wpautop过滤器不能在我的functions.php中工作?
  3. WP版本4.8-4.9有什么变化使得这些过滤黑客不再起作用吗?

UPDATE: 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 ) ) { ?>

        <br />

        <?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;
}
php wordpress content-management-system wordpress-theming youtube-iframe-api
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;
}

0
投票

您可以在自己的包装器中包装嵌入的iframe,图像,视频等。然后WP不会添加<p>标签。使用'oembed_dataparse'过滤器。

// Wrap iframe for embedded video in <div> with custom class.
// Add class-modifier if aspect ratio is 4/3. 
function wrap_oembed_dataparse($return, $data, $url) {

    $mod = '';

    if  (   ( $data->type == 'video' ) &&
            ( isset($data->width) ) && ( isset($data->height) ) &&
            ( round($data->height/$data->width, 2) == round( 3/4, 2) )
        )
    {
        $mod = 'embed-responsive--4-3';
    }

    return '<div class="embed-responsive ' . $mod . '">' . $return . '</div>';
}

add_filter( 'oembed_dataparse', 'wrap_oembed_dataparse', 99, 4 );
© www.soinside.com 2019 - 2024. All rights reserved.