Jump to content

How can I best allow users to syndicate my content with one line of code?


twebman84

Recommended Posts

I want to offer my site visitors the opportunity to use some of my content on their sites. For example, a Quote of the Day or This Date in History snippet that they can place on their site. I want to host and manage the content on my site and allow the visitors to syndicate it on their sites easily. The key word here is "easily". I don't want to do this through RSS, which can be tricky for entry-level webmasters and bloggers. I want to be able to offer a line of code, say in Javascript, that the visitor can copy and paste into their site (Wordpress widget etc.) and have my content appear there.

 

Does anyone have any starting points for how to do this? I have several years of experience with PHPP, MySQL, CSS, HTML, etc., but I don't want to re-invent the wheel. I've done something similar in JSP, but not using PHP.

You could try something as simple as a dynamic PHP file which is served with Javascript headers that your users can add to their website.  Something like:

 

<?php
$daily_quote = get_daily_quote();  # assume this is a simple string
header('Content-type: application/javascript');
?>
// The output of this page should be treated as javascript by the user's browser
(function (quote){alert(quote)})(<?php echo $daily_quote; ?>);

 

Your users then just need to hotlink the JavaScript file (really PHP) hosted on your server:

<script type="text/javascript" src="//yourserver.com/daily_quote_javascript.php"></script>

 

When the code above is interpreted by the browser, the anonymous function will just alert the quote for the day (which was determined dynamically via PHP).  You'd obviously want your function to do something more advanced like wrap the quote in appropriate HTML and add it to the page but that's up to you.

 

There are more advanced ways to approach this so if you bump into any walls or limitations with this approach, you could look into JSONP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.