How can I create a customizable widget with random posts from my blog that can be published on another site as js, iframe or div.
Is there any WordPress plugin for that?
Leave a Reply
You must be logged in to post a comment.
How can I create a customizable widget with random posts from my blog that can be published on another site as js, iframe or div.
Is there any WordPress plugin for that?
You must be logged in to post a comment.
To create a special output of random posts:
'template_redirect'
and return your output depending on the details of the requested endpoint. Thenexit
.Thatâs all.
Well ⦠some code may help to understand the details. 🙂
Letâs build a small plugin for HTML and JSON output.
I have named it T5 Random Posts Endpoint; all code goes into a class with static methods to keep the global namespace clean.
As you can see, we call a central
init
method on'init'
to ⦠initialize our plugin. Creative naming, huh?The endpoint is named
randomposts
and attached to the root. So after activation you can get the out put onexample.com/randomposts/
orexample.com/randomposts/json/
.But ⦠if you call the first URL the endpoint works and the request variable
randomposts
is set â but it is empty. Thatâs why we hook into'request'
and give the variable a default value we can work with later. Letâs usehtml
as default:Now we hook into
'template_redirect'
so WordPress will not use the default templateindex.php
but our own output.First we check if we are really on our endpoint by looking into
get_query_var( 'randomposts' )
. We filled this variable in our request filter, so if it is still empty, we know for sure that is not our business and we return quickly.Then we test if the request was for one of our predefined output variants
html
orjson
. If someone is calling/randomposts/css
or/randomposts/lala
we raise a 414 error with a helpful message. And exit.Then we get finally our posts with
get_posts()
and remove all the data we donât need in a dedicated method namedprepare_post_data()
:We pass this simple array to a method named exactly like our output:
html()
orjson()
.Letâs start with
json()
; thatâs the easy part. See How to encode post content as JSON? for some background.Easy, isnât it? The
html()
is not that much harder, just longer:This is how it looks with the theme test data:
Now our output is finished â we are still in
render()
â and we callexit
because otherwise WordPress would include theindex.php
from the active theme.Now we need just a method to refresh the permalink settings:
We prevent our default
init
hook here because on activation we are on the plugin list where we donât need the endpoint, and on deactivation we do not want to re-register the now unused permalink.We attach this method to (de)activation with:
And now we can say: Thatâs all. Really.
If you made it this far grab the complete plugin from GitHub.
I think in essence what you’re talking about is a Custom Widget that will parse and display RSS Feed of your Blog. Here, you can build a Custom Widget of your own and hard-code the RSS Feed URL of your Blog in your Widget PHP itself to make sure Widget User doesn’t change the URL; so your widget will show RSS of your Blog only.
Now, once you finalize to use RSS to fork your Blog, you’ve several options open in front of you. Either you can use WordPress’s own RSS Feed abilities to save you time (but this will impose many limitations) OR write your own RSS Feed maker code as Web Service installed on your Server/Web Hosting that will interact with your Widget. So, make your Widget PHP to send request to your Blog’s RSS Feed Publishing Web Service; then parse the results returned by your Server and display RSS Feed items.
If you’ve time to invest and serious about this Widget, then better you implement your own Web Service. I don’t think it’ll be too hard for you… just play with WP_Query and return the results in proper RSS Feed XML Format. This way you will also be able to offer Category list in Drop-down to your Widget User to choose from and make your Widget PHP to request RSS of Posts only from selected Category of your Blog. Plus, you’ll have better control over how many Posts to return in RSS Feed requested by your Widget.
I hope this helps.
Cheers,
Ruturaaj.