yaseen cruise
yaseen cruise

Reputation: 53

How to update wordpress content

add_filter('the_content','filter_trendland_content');


//create a the function to get the content of the page by using the hook the_content
function filter_trendland_content($content) {
    $getdata = $content;
    if ( preg_match_all( '#(?:<a.+?href=["|\'](?P<link_url>.+?)["|\'].*?>\s*)?(?P<img_tag><img.+?src=["|\'](?P<img_url>.+?)["|\'].*?>){1}(?:\s*</a>)?#is', $getdata, $images ) ) {
        foreach ( $images[img_url] as $key => $value) {
            $subdomain = rand( 1, 3 );
            $newurl="yaseen.media$subdomain.com";
            $content = preg_replace('/\bmedia.trendland.com\b/', $newurl, $value);
            print $content;
        }
        print $getdata;
    }

    //return $getdata;
}   

I am using the above method to replace the url's with the new one and update the content of the page but the page content is still the same so please help me with this..

Upvotes: 0

Views: 59

Answers (1)

PassKit
PassKit

Reputation: 12581

Looking at your function, it does not return a value.

Instead of printing the output, return the content back to the filter.

Also, you are iterating through an empty object - $images[img_url]. Check the documentation for preg_match_all. It returns a multi-dimensional array of matches.

You code looks over-engineered and the word boundary in your regex would probably prevent a match. The following may achieve what you need providing that all instances of media.trendland.com require replacing.

function filter_trendland_content($content) {

    $subdomain = rand( 1, 3 );
    $newurl='yaseen.media' . $subdomain . '.com';
    $content = preg_replace('/media.trendland.com/', $newurl, $content);

    return $content;
}   

Upvotes: 1

Related Questions