亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 106 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號