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

这里的解决方案并没有解决我们的问题。我已经有了一个解决方案来更改我们主题中字段表单中的所有链接。我为每个网络使用不同的数组,例如$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 ) ;

        }


www说
浏览 134回答 3
3回答

蓝山帝景

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”代码,但您说它正在运行,所以我就这样离开了。

红颜莎娜

如果阻止 wp 自动嵌入有效,则只需将此行添加到您的主题中functions.phpremove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );

12345678_0001

我没有测试就写了解决方案。如果没有您的网站,您的代码很难测试,但我认为问题出在正则表达式上。回调很难调试。我的版本如下。第一步,改变你的结构。我怀疑域是唯一的。一维数组更有用。$domains =&nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'shop1.com'=>'11111',&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'shop2.co.uk'=>'11112',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'shop-x1.com'=>'11413',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'shop-x2.net'=>'11212',&nbsp; &nbsp; &nbsp; &nbsp; );下一个:$dangerouschars&nbsp; = array(&nbsp; '.'=>'\.',);function wpso_change_urls( $content ) {&nbsp; &nbsp;global $domains,$dangerouschars;&nbsp; &nbsp;foreach($domains as $domain=>$id){&nbsp; &nbsp; &nbsp; $escapedDomain = str_replace(array_keys($dangerouschars),array_values($dangerouschars), $domain);&nbsp; &nbsp; &nbsp;if (preg_match_all('/=\s*"(\s*https?:\/\/(www\.)?'.$escapedDomain.'[^"]+)\s*"\s+/mi', $content, $matches)){&nbsp; &nbsp; &nbsp; &nbsp; // $matches[0] - ="https://example.com"&nbsp; &nbsp; &nbsp; &nbsp; // $matches[1] - https://example.com&nbsp; &nbsp; &nbsp; &nbsp; for($i = 0; $i<count($matches[0]); $i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $matchedUrl = $matches[1][$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = rawurlencode($matchedUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //is very important to replace with ="..."&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content = str_replace($matches[0][$i], "=\"https://www.network-x.com/click/{$id}lorem_lorem={$url}\" ", $content);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;return $content;}
打开App,查看更多内容
随时随地看视频慕课网APP