Insert an image into a WordPress Post with a plugin.

Posted on Thursday the 15th of October, 2009 at 3:15 pm in Dev

So in a project I’m working on I need to use a normal form to insert a post into the database. My client wanted the form to also add an image to the post. Furthermore, the image must be inserted using normal WordPress methods. The code below is the minimum required to insert an image into the database and attach it to a specific post.

  1. include_once(ABSPATH.'/wp-admin/includes/media.php');
  2. include_once(ABSPATH.'/wp-admin/includes/file.php');
  3. include_once(ABSPATH.'/wp-admin/includes/image.php');
  4. // $id is the id of the post being inserted
  5. // $name is actually the name of the form input that uploaded the file so WP can access it using $_FILE[$name]
  6. media_handle_upload($name,$id);

If you are inserting a new post, it makes sense to have the line $id = wp_insert_post($post_data); before this.

Stripslashes all values in an array with PHP.

Posted on Friday the 14th of August, 2009 at 1:38 pm in Dev

Here is a very simple function to run stripslashes on an array and all of its child arrays. The function below is for PHP 5 and takes a single argument which should consist of an array.

  1. function unstrip_array($array){
  2. foreach($array as &$val){
  3. if(is_array($val)){
  4. $val = unstrip_array($val);
  5. }else{
  6. $val = stripslashes($val);
  7. }
  8. }
  9. return $array;
  10. }
  11. Use the following to copy and paste the code.

It loops through all elements of the array and if an element is an array it calls itself on th the child array to an infinite depth. Of course this can cause memory problems if your arrays are dynamically created with an excessively large depth, but for most uses, it will function perfectly.

Oh, as an Aside: I’m alive but still not talking to you. (From the 21st of May)

There have been no posts for a while now and none of the updates to AJAXed WordPress or my other plugins have been pushed out. There is a lot of stuff (of the good kind) that is going on in my life currently, so I’ve put everything else on the back burner.

Thank you for your understanding.

P.S. Don’t you hate posts like this? Me too!

Oh, as an Aside: Short Ajax Script (From the 13th of May)

I wanted to see how small you could make a fully functional AJAX script that worked cross-browser and degraded gracefully, so I went through an old custom AJAX script and made it as small as I possibly could. In the resulting AJAX scripts, the post version is 410 characters and the GET version is only 359 characters long. The scripts are fully functional and accept the following parameters: URL, DATA (in string form), and ELEMENT (to update).

The scripts could be a little smaller, but it would really kill readability.

“Get” AJAX Script

  1. function a(l,d,u){
  2. try{r = new XMLHttpRequest();}catch(e){try {r = new ActiveXObject('Msxml2.XMLHTTP');}catch(e){r = new ActiveXObject('Microsoft.XMLHTTP');}}
  3. if(r){
  4. r.onreadystatechange = function() {if (r.readyState == 4 && r.status == 200){document.getElementById(u).innerHTML = r.responseText;}}
  5. r.open('GET', l+'?'+d, true);r.send(d);
  6. }
  7. }

“Post” AJAX Script

  1. function b(l,d,u){
  2. try{r = new XMLHttpRequest();} catch(e){try {r = new ActiveXObject('Msxml2.XMLHTTP');} catch(e){r = new ActiveXObject('Microsoft.XMLHTTP');}}
  3. if(r){
  4. r.onreadystatechange = function() {if (r.readyState == 4 && r.status == 200){document.getElementById(u).innerHTML=r.responseText;}}
  5. r.open('POST', l, true);r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');r.send(d);
  6. }
  7. }

Combined AJAX Script

This combined script also accepts a fourth parameter ‘p’ that should evaluate true if the data is to be sent by post.

  1. function a(l,d,u,p){
  2. try{r = new XMLHttpRequest();}catch(e){try {r = new ActiveXObject('Msxml2.XMLHTTP');}catch(e){r = new ActiveXObject('Microsoft.XMLHTTP');}}
  3. if(r){
  4. r.onreadystatechange = function() {if (r.readyState == 4 && r.status == 200){document.getElementById(u).innerHTML = r.responseText;}}
  5. if(p){r.open('POST', l, true);r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');}else{r.open('GET', l+'?'+d, true);}
  6. }
  7. }

Demo: (Sorry but you will have to go to the full page so the JavaScript is loaded.)

Continue reading. »

Oh, as an Aside: Oh I am a gummy bear; yes, I am a gummy bear… (From the 29th of April)

The best thing to come out of YouTube since the Kiwi: the Gummy bear song.

Oder, ich bin ein gummi Bär; ich bin ein gummi Bär.

All right, all right. Back to your regularly scheduled broadcasting.

Equality, the goal not the signpost.

Posted on Sunday the 27th of April, 2008 at 7:23 am in Sociology

The United States of America has a long history of inequality, from its treatment of Native Americans to women’s rights, it has tended to favor one group over others, but it has attempted to repair the damage it caused. However, even though America is the “land of opportunity,” its formerly oppressed peoples are not equal, but what does it mean to be equal? Is equality the government saying you must have the same number of employees from each arbitrarily defined “race?” Does equality mean that people should be forced to be equal? In examining this issue, one must define equality itself.

There are three forms of equality: equality of outcome, of opportunity, and of perception. Equality of perception is the most basic: it dictates that for people to be equal, each person should be perceived as being of equal worth.

Continue reading. »