0

Working in Wordpress, I have a snippet of code that I want to share with my users:

<script type="text/javascript">
document.write('<div class="rslr1">Our Reviews');
new rssdisplayer("rslrdiv1", "http://mydomain.com/blogname/rss-feed/", 4, "description");
document.write('<a href="#">Add a Review</a></div>');
</script>

Again, I'm not running this code, I am sharing it between tags, ans escaping all the brackets.

The tricky bit here is that I want to replace 'blogname' in the third line of code with the actually blogname, which is a PHP variable.

I see lots of answers here about using a PHP variable in javascript, but I'm not running the script. I'm simply posting it in a blog post for my users to grad, and need to dynamically insert the blogname into the RSS feed URL.

Any help much appreciated.

Larry

0

2 Answers 2

1
<script type="text/javascript">
  document.write('<div class="rslr1">Our Reviews');
  new rssdisplayer("rslrdiv1", "http://mydomain.com/<?php echo $blog_name ; ?>/rss-feed/", 4, "description");
  document.write('<a href="#">Add a Review</a></div>');
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Since I'm not running the javascript, simply displaying it for others to copy and use, the PHP echo solution doesn't work. I'd have to be running the Javascript for that to work. Other ideas?
0

you should replace blogname with

<?php echo $blogname; ?>

instead of simply typing "blogname"

Please also note that the document in which the script is run must be enabled for php handling (in a simple setup, it must be named .php)

You seem picky with your answers, although the answers above are correct this might suit you better?

<pre><?php 
$blogname='WHATEVER-YOU-WISH';

$output="<script type='text/javascript'>
document.write('<div class=\"rslr1\">Our Reviews');
new rssdisplayer('rslrdiv1', 'http://mydomain.com/blogname/rss-feed/', 4, 'description');
document.write('<a href=\"#\">Add a Review</a></div>');
</script>";
$output=str_replace('blogname',$blogname,$output);
$output=str_replace('<','&lt;',$output);
$output=str_replace('>','&gt;',$output);
echo $output;
?></pre>

Woking example of the above code: http://allanthya.net/test.php

5 Comments

Remember, I am NOT running this code. . . simply disp-laying it so my visitors can put the code on their own site. Since I am not running it, the PHP bit doesn't get replaced.
Did you try it? still the same solution. PHP runs before the code even leaves the server, JS is run clientside, in order to view the script with the actual code from a php variable you need use that variable. above is how you do that.
@inbound-mktg I updated my answer to better suit your question.
This second solultion works great! Hadn't even considered doing it this way. Excellent- thank you, Iesus.
@inbound-mktg Do i deserve a +1? or perahaps a "correct answer?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.