Jetpack JSON Api – outputting custom fields

Instead of using rss to retrieve data from a wordpress site I wanted to try out the Jetpack JSON API, aka WordPress Rest api. I came across some smallish challenges, so just wanted to write a quick post with the solution for those getting stuck on the same.

Problem one: The json api doesn’t display customfields as default.
Problem two: Problems retrieving data from a wordpress network (multisite)
Problem three: Weight

Problem one

We use customfields for frontpage images and to give our posts better metadata, but by default these are hidden from the public api.
To solve it, turn on Jetpack Json api, then add the following code to your WP theme functions.php:

As you can see, you have to manually add the fields you want exposed.

(Via Stackexchange)

Problem two

Error on multisite. The format to call the json api looks like this https://public-api.wordpress.com/rest/v1/sites/pappmaskin.no/posts/?pretty=true&number=3, but if you’re running a multisite install and change pappmaskin.no to pappmaskin.no/urltosubsite/ then the request will fail. Luckily the solution was simple: Just urlencode the site url to pappmaskin.no%2Furltosubsite%2F. (Via alpha1 on Stackexchange)

It is also possible to use the Jetpack Site ID instead of the encoded url. That ID is returned in most queries to the api, in the site_ID field. (Thanks to Jeremy Herve, via WordPress.org). The only way to get to a subsite site_ID (as far as I have found), is to actually do the urlencode trick first.

So for this site, this will also work.

https://public-api.wordpress.com/rest/v1/sites/6464046/posts/?pretty=true&number=3

Problem three: Weight

The posts endpoint outouts (almost) all the content and data associated with your posts, which might be a bit overkill. The api actually lets you filter exactly the fields you need. Like so:

https://public-api.wordpress.com/rest/v1/sites/pappmaskin.no/posts/?pretty=true&number=3&category=diy&fields=title,excerpt,URL,date,metadata

Which gives you a nice and tidy little JSON package:

How to make the Get The Image WordPress plugin to work with TimThumb and Related posts

I’ve been using the wordpress plugin ‘Get the Image‘ by developer Justin Tadlock for a while. ‘Get the image’ does a fantastic job searching through posts looking for images, so you don’t have to add thumbnails manually, but it always bothered me that it downscaled images in the html instead of scaling and cropping them properly. I also use the lovely timThumb script on my blog, to scale, scrop and cache images on the fly, and I wanted to find a way to combine the two scripts into one.

Fortsett å lese «How to make the Get The Image WordPress plugin to work with TimThumb and Related posts»

Delicious Screenshots – my first wordpress plugin

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /customers/7/5/b/pappmaskin.no/httpd.www/wp-content/plugins/delicious_screenshots/wp_delicious_sceenshots.php on line 75Warning: file_get_contents(http://feeds.del.icio.us/v2/json/mskogly/art?Fcount=6): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /customers/7/5/b/pappmaskin.no/httpd.www/wp-content/plugins/delicious_screenshots/wp_delicious_sceenshots.php on line 75Warning: Invalid argument supplied for foreach() in /customers/7/5/b/pappmaskin.no/httpd.www/wp-content/plugins/delicious_screenshots/wp_delicious_sceenshots.php on line 79

If this works there should be about 6 thumbnails on this page, showing links bookmarked using Delicious, and tagged with “art”.


Download:
Download delicious_screenshots.zip here, version 0.7 of my very first wordpress plugin.

Usage:
Activate plugin. And add your del.icio.us username to the options page. (under settings in the WordPress toolbar)
https://i2.wp.com/pappmaskin.no/wp-content/2011/07/20081020-jq6mtqhq3k4pu475bffhjuppcs.jpg?w=525

Changelog:
14.02.2017: Changed from rss to json and cleaned the code up a bit. Also removed Snapcasa from options.

What’s next:

  1. The first version of this plugin used Snapcasa for creating thumbnails, but I switched to thumbalizr.com to get a higher success rate.
  2. The count param doesn’t seem to do anything when calling the del.icio.us api, it always returns 10 results, so I need to handle that in the code instead. I’ve notified them of the bug.
  3. Testing other screenshotting services or making my own.
  4. Choosing an image from the page metadata when available.

Timelope takes lifestreaming to a new level!

First off I just want to say that I think this is probably NOT a very good idea, but I downloaded the Timelope extension for firefox today, that basically publishes every single webpage you visit to timelope.com and as an rss.

Smart? No, maybe not, but kind of fun!

Whyyy!
Some people would probably say that publishing every singe page you visit on the web as kind of overkill, and also a sliiiiight security nightmare, and both are probably correct, but is just so darn cool to have!

On a more serious note: It is also a good reminder of just how much information you leave behind while surfing. Perhaps you can even change some habits? Think: If it aint safe to show on the web for all to see, then perhaps it’s not very good for your brain eighter!

Good control over what to display
Timelope has a lot of options to make it easy to control what people see:

  1. You can choose to make everything private, and just use it as a log for yourself.
  2. By default localhost and all visits to urls starting with https:// is hidden (that means online banking and alle secure logons)
  3. You can create a whitelist consisting of sites you want to display.
  4. You can create a blacklist with all the sites or parts of websites you don’t want to show (like the link to phpmyadmin, wp-admin, or just any boring site you don’t want to bother people with telling about.
  5. There is a convenient checkbox to hide any porn sites you visit.
  6. There is a small button in Firefox where you easily can turn off all publishing.
  7. And lastly, you can manually delete stuff that you feel clutter up your lovely stream, and you can at the same time choose hide all later visits to knitting.com or other embarrasing sites automatically.

Displaying the Timelope – feed in WordPress:
You can use the wonderful RSS Stream WordPress plugin to display you browser history.

This plugin only supports RSS 2.0, and timelope has chosen to deliver an atomfeed, so you have to fetch your timelope stream and reformat it with for instance feedburner. Log on to feedburner.com and create a new feed, paste in your feedurl from timelope (http://timelope.com/mskogly/feed), when you are done with the wizard you must turn off Smartfeed and then look for Convert Format Burner. Choose Rss 2.0 and then activate.

Then you can go into wordpress admin and insert the feed into Rss Stream, and you are set for some serious information overload fun for all.

Your lifestream will suddenly be flooded with lovely stuff like this:

https://i0.wp.com/pappmaskin.no/wp-content/2011/07/20081014-xijsd2x8nmy3m9dbgf76dd1cei.jpg?w=525

Update: 26th of april 2009: Decided to turn off Timelope, because it took to much time to administrate and weed out the sites I didn’t want to show.

m.

Processing: Sketch_081001a, and how to embed a sketch in a wordpress post

Screenshot of processing app

I downloaded my first copy of Processing (Processing.org) a few years ago, but I have never gotten past the initial few demos and small tutorials. I’ve been interested in generative computer art for many years, ever since I first saw the work of Marius Watz in the mid nineties and had a stint reading dadaist poetry and cutups, but I’ve never had the time to play with this stuff myself. Or the brains to handle the math, hehe. But then I came across this tutorial in Computer Arts #149 (The June 2008 issue), where there are a few really interesting tutorials, which basically gives you enough info to understand the key consepts that you need to create some very interesting apps, like the one below (slightly modified of course, I added random colors among other things).

(java applets no longer functions in Google Chrome). Nothing you can do about that.)

Oh, and I had quite a hard time finding out how to embed my app in my wordpress blog. I kept getting some heavy errors when I tried to post the html the Processing software generates straight into WordPress, but I eventually got it to work. Since I couldn’t find any tutorials on how to do this, I decided to write my own. So here it is:

How to embed a processing java application in wordpress:

First, you have to turn off the Visual editor for your user, if you don’t, wordpress will 100% garanteed screw up your code. And remember, if you turn the Visual editor back on after finishing editing your post, then DON’T open the post for editing again. When I did this wordpress replaced my embed code for java with a flash embed code!!! Luckily I had saved this article as a Google Docs document, and could simply copypaste it in here again.

(* Update: This might also be related to Adblock plus, but needs to be verified)

Second, paste in this code (just remember to replace the variables with where you’ve put your own .jar file etc. You get all the info you need when you choose File and Export in Processing, and open up the resulting index.html file in an editor of your choice. Note: the applet tag is slightly depricated, so I guess I have to figure out to do this with a “proper” object + embed.

<APPLET name="sketch_081001a" WIDTH="500" HEIGHT="500" archive="https://pappmaskin.no/opensource/processing/sketch_081001a/sketch_081001a.jar" standby="Loading Processing software…" codebase="https://pappmaskin.no/opensource/processing/sketch_081001a/" code="sketch_081001a" mayscript="true" scriptable="true" image="https://pappmaskin.no/opensource/processing/sketch_081001a/loading.gif" boxmessage="Loading Processing software…" boxcolor="#FFFFFF"><IMG SRC="https://pappmaskin.no/opensource/processing/sketch_081001a/loading.gif" WIDTH="1" HEIGHT="1" /></APPLET>

Source Code:

And here is the source code for this lille app, just create a new Sketch in Processing and paste it in and run it. Should work like a charm.
int x1;
int x2;
int x3;
int y1;
int y2;
int y3;

void setup() {
size(500,500);

x1 = width/2;
y2 = height/2;
x2 = x1;
y2 = y1;
x3 = x1;
y3 = y1;
}

void draw(){
//for (int i=0; i<1000; i+=1) {
x2 = constrain( (x2-20+(round(random(40)))), -30, width+30);
y2 = constrain( (y2-20+(round(random(40)))), -30, width+30);
smooth();

//draws a triangle
fill(round(random(255)),50,100);
triangle(x1, y1, x2, y2, x3, y3);

// the triangles are joined here

x3=x2;
y3=y2;
//}
//noLoop();

}

Or get the source code here: sketch_081001a