Insert an image into a WordPress Post with a plugin.

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.

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: AJAXed WordPress (From the 10th of November)

AJAXed Wordpress (AWP) harnesses the power of both AJAX and Wordpress to improve the user experience, the administration capabilities and the design potential of any Wordpress based blog. It works on all WordPress versions from 2.1 – 2.6.

NEW VERSION AND NEW WEBSITE!

AJAXed WordPress has its own website at ajaxedwp.com. All updates and support are now given there.

Oh, as an Aside: PHP include ladder to find include path. (From the 10th of September)

I needed a way to be able to include particular files from files that are themselves included. because the working directory was not necessarily the same, so I needed a way to find what the actual include path should be. The following code snippet finds a specific folder or file and returns the ladder (relative path) to get to it.

  1. function find_path($loc,$dir=0){
  2. for($i = 0; $i <10; $i++){
  3. if($dir){
  4. if(is_dir($ladder.$loc)){
  5. return $ladder;
  6. break;
  7. }
  8. }elseif(is_file($ladder.$loc)){
  9. return $ladder;
  10. break;
  11. }
  12. $ladder .='../';
  13. }
  14. }
  15. Use the following to copy and paste the code.

This code is NOT user-input safe: any checks you do should be hard coded otherwise you might as well just give out your passwords.

Super Category (multi-blog) plugin with no hacks

This is an old buggy version. Please use the newest.

NOTE:
I believe the only part that is missing is where it says to follow the instructions in the Up and Running section. In this section it basically said to make a group of links or categories belong to only one site add them to a category with the same name as the url (eg site: http://anthologyoi.com would have all its links under the anthologyoi.com category).

Don’t worry I will rewrite the detailed section as soon as I get time (read: over the weekend)
Download the Super Category Plugin here
About a month and a half ago I released a first version of this plugin. In it I had to use a combination of hacks along with a plugin to have the same functionality that this version has with no hacks what-so-ever.

Continue reading. »