为了账号安全,请及时绑定邮箱和手机立即绑定

Wordpress:自动更改 the_content 部分中的 URL

Wordpress:自动更改 the_content 部分中的 URL

PHP
www说 2022-12-23 13:03:04
这里的解决方案并没有解决我们的问题。我已经有了一个解决方案来更改我们主题中字段表单中的所有链接。我为每个网络使用不同的数组,例如$example_network_1 $example_network_2为每个附属网络使用 PHP foreach。现在我需要一个解决方案来将相同的数组用于 WordPress 帖子的内容。此解决方案适用于一个网络,但它会导致 YouTube 视频出现 404e 错误。我们从 YouTube 视频中输入 URL,WordPress 会自动生成一个嵌入视频。使用以下代码,我们会得到一个404 错误 iframe或类似的东西。我们需要针对多个网络的解决方案。我非常感谢您的每一次帮助!    $example_network_1 = array(            array('shop'=>'shop1.com','id'=>'11111'),               array('shop'=>'shop2.co.uk','id'=>'11112'),        );    $example_network_2 = array(            array('shop'=>'shop-x1.com','id'=>'11413'),             array('shop'=>'shop-x2.net','id'=>'11212'),        );    add_filter( 'the_content', 'wpso_change_urls' ) ;    function wpso_found_urls( $matches ) {     global $example_network_1,$example_network_2;           foreach( $example_network_1 as $rule ) {            if (strpos($matches[0], $rule['shop']) !== false) {                $raw_url = trim( $matches[0], '"' ) ;                return '"https://www.network-x.com/click/'. $rule['id'].'lorem_lorem='.rawurlencode($raw_url ) . '"';                }  /*      foreach( $example_network_2 as $rule ) {            if (strpos($matches[0], $rule['shop']) !== false) {                $raw_url = trim( $matches[0], '"' ) ;                return '"https://www.network-y.com/click/'. $rule['id'].'lorem='.rawurlencode($raw_url ) . '"';                  }  */        }    }    function wpso_change_urls( $content ) {        global $example_network_1,example_network_2;               return preg_replace_callback( '/"+(http|https)(\:\/\/\S*'. $example_network_1 ['shop'] .'\S*")/', 'wpso_found_urls', $content ) ;    //     return preg_replace_callback( '/"+(http|https)(\:\/\/\S*'. $example_network_2 ['shop'] .'\S*")/', 'wpso_found_urls', $content ) ;        }
查看完整描述

3 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

autoembed挂在the_content优先级为 8 的wp-includes/class-wp-embed.php:39


尝试降低the_content过滤器的优先级,以便 URL 替换发生在嵌入之前,如下所示:


add_filter( 'the_content', function ( $content ) {

    /*

     * Here, we define the replacements for each site in the network.

     * '1' = main blog

     * '2' = site 2 in the network, and so on

     *

     * To add more sites, just add the key number '3', etc

     */

    $network_replacements = [

        '1' => [

            [ 'shop' => 'shop1.com', 'id' => '11111' ],

            [ 'shop' => 'shop2.co.uk', 'id' => '11112' ],

        ],

        '2' => [

            [ 'shop' => 'shop-x1.com', 'id' => '11413' ],

            [ 'shop' => 'shop-x2.net', 'id' => '11212' ],

        ]

    ];


    // Early bail: Current blog ID does not have replacements defined

    if ( ! array_key_exists( get_current_blog_id(), $network_replacements ) ) {

        return $content;

    }


    $replacements = $network_replacements[ get_current_blog_id() ];


    return preg_replace_callback( '/"+(http|https)(\:\/\/\S*' . $replacements['shop'] . '\S*")/', function( $matches ) use ( $replacements ) {

        foreach ( $replacements as $rule ) {

            if ( strpos( $matches[0], $rule['shop'] ) !== false ) {

                $raw_url = trim( $matches[0], '"' );


                return '"https://www.network-x.com/click/' . $rule['id'] . 'lorem_lorem=' . rawurlencode( $raw_url ) . '"';

            }

        }

    }, $content );

}, 1, 1 );

这不是复制和粘贴解决方案,但应该可以帮助您。您可能需要调整您的“preg_replace_callback”代码,但您说它正在运行,所以我就这样离开了。


查看完整回答
反对 回复 2022-12-23
?
红颜莎娜

TA贡献1842条经验 获得超13个赞

如果阻止 wp 自动嵌入有效,则只需将此行添加到您的主题中functions.php

remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );


查看完整回答
反对 回复 2022-12-23
?
12345678_0001

TA贡献1802条经验 获得超5个赞

我没有测试就写了解决方案。如果没有您的网站,您的代码很难测试,但我认为问题出在正则表达式上。回调很难调试。我的版本如下。


第一步,改变你的结构。我怀疑域是唯一的。一维数组更有用。


$domains =  array(

            'shop1.com'=>'11111',   

            'shop2.co.uk'=>'11112',

            'shop-x1.com'=>'11413', 

            'shop-x2.net'=>'11212',

        );

下一个:


$dangerouschars  = array(

  '.'=>'\.',


);

function wpso_change_urls( $content ) {

   global $domains,$dangerouschars;

   foreach($domains as $domain=>$id){

      $escapedDomain = str_replace(array_keys($dangerouschars),array_values($dangerouschars), $domain);


     if (preg_match_all('/=\s*"(\s*https?:\/\/(www\.)?'.$escapedDomain.'[^"]+)\s*"\s+/mi', $content, $matches)){

        // $matches[0] - ="https://example.com"

        // $matches[1] - https://example.com

        for($i = 0; $i<count($matches[0]); $i++){

                $matchedUrl = $matches[1][$i];

                $url = rawurlencode($matchedUrl);

                //is very important to replace with ="..."

                $content = str_replace($matches[0][$i], "=\"https://www.network-x.com/click/{$id}lorem_lorem={$url}\" ", $content);

        }

     }

   }

   return $content;

}


查看完整回答
反对 回复 2022-12-23
  • 3 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号