How to make a partial string replacement of a url in an iframe in WordPress pages?

I have around 30 pages in WordPress, each of which contains one iframe block. Each unique iframe has the same src url except for the numeric part of the htm file at the end. Here's a couple of examples:

http://content.yudu.com/web/AAAAA/BBBBBBB/TITLE/flash/resources/2.htm
http://content.yudu.com/web/AAAAA/BBBBBBB/TITLE/flash/resources/4.htm
http://content.yudu.com/web/AAAAA/BBBBBBB/TITLE/flash/resources/6.htm

Open in new window


These are links to specific pages of online publications on the Yudu platform and for each issue, this 'constant' part changes:

AAAAA/BBBBBBB/TITLE

Open in new window


I obviously don't want to manually edit 30 pages every month so what mechanism would be best for doing a search and replace, either on the pages in the WordPress/MySQL database or on the DOM after it loads?

Some kind of base url mechanism in HMTL
Mod RewriteRule?
WordPress Plugin? (some kind of shortcode creator for example)
PHP?
JavaScript?
Other?

And based on the best approach, what code can perform the above?
LVL 15
Jamie GarrochSenior Technical Consultant at BrightCarbonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
So basically you need to search on all 30 files for a pattern and replace it, right?

Is this Linux?
If so you can use a shell command like sed.
Something like this should work:
sed -e 's/PatternToBeReplaced/Replacement/g' *.html

Open in new window

The problem is that this as is only prints the result in the STDOUT.

What you need is something more intelligent that replaces the pattern.
Here's a bash script:
filepath="*.html"
searchstring="AAAAA/BBBBBBB/TITLE"
replacestring="ZZZZZ/YYYYY/TITLE"

i=0; 

for file in $(grep -l -R $searchstring $filepath)
do
  cp $file $file.bak
  sed -e "s/$searchstring/$replacestring/ig" $file > tempfile.tmp
  mv tempfile.tmp $file

  let i++;

  echo "Modified: " $file
done

Open in new window

I was starting to write this but I went to google for something similar and found it.
I tested it and it work perfectly.

Save this script in a file named: findandreplace.bs
Put it in the same sir as the html files or make sure the filepath variable is properly configured
Executed with: bash findandreplace.bs

cheers!
Jamie GarrochSenior Technical Consultant at BrightCarbonAuthor Commented:
Hi Alexandre. Thank you for the suggestion. There aren't in fact 30 files but 30 records in a MySQL database (WordPress is written in PHP and stores web page content in a database and the server dynamically builds the HTML file on demand). So I'm looking for a solution that can work within the WordPress environment but am not sure if I should start with PHP to manipulate the DB or JAvaScript to manipulate the output document or something else.
Alexandre SimõesManager / Technology SpecialistCommented:
Ah, sorry mate :)

I never used wordpress but I think this plugin does what you need:
https://wordpress.org/plugins/search-and-replace/
Jamie GarrochSenior Technical Consultant at BrightCarbonAuthor Commented:
Thanks Alexandre. I think that plugin could come in very handy and I didn't find it while searching for "shortcode creator".

After a late night of reading, I've found another solution using the WordPress codex API for shortcode creation:

https://codex.wordpress.org/Shortcode_API

By adding this code to the theme's functions.php file:

function yudu_func( $atts ){
        $a = shortcode_atts( array(
        'pub' => 'default',
        'page' => '1',
    ), $atts );

    if ("{$a['pub']}" == "pub1")
        return "<iframe class='embeded_yudu' src='http://content.yudu.com/web/AAAAA/BBBBBBB/TITTLE/flash/resources/{$a['page']}.htm'>Loading...</iframe>";
    if ("{$a['pub']}" == "pub2")
        return "<iframe class='embeded_yudu' src='http://content.yudu.com/web/XXXXX/YYYYYYY/TITTLE/flash/resources/{$a['page']}.htm'>Loading...</iframe>";
}
add_shortcode( 'yudu', 'yudu_func' );

Open in new window


...I can then add this shortcode to the numerous WordPress pages:

[yudu pub="pub1" page="2"]

Open in new window


and then I only ever need to change the publication URLs AAAAA/BBBBBBB/TITLE and XXXXX/YYYYYYY/TITTLE for each issue in one place, the functions.php file.

This could be extended to keep the URLs in the DB but for now, this is already much more efficient.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jamie GarrochSenior Technical Consultant at BrightCarbonAuthor Commented:
the shortcode solution allows a dynamic insertion as opposed to the manual search and replace mechanism proposed by the plugin solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.