0

I'm a beginner with WordPress plugin api, php and ajax. I'm coding a training plugin for myself to save posts article to read them later, like favourites on web browser, with folders.

I use oop to do so.

I assumed to create a new user meta called 'favoris', wich is an array with the users created folders.

I got something like that for getting favs, creating folders and deleting'em.

/**
 * Get favorites
 */
public function fp_ajax_getFav(){
    $current_user = wp_get_current_user();
    $userid = $current_user->ID;
    $folder = get_user_meta($userid, 'favoris');
    $content =  '';
    $content .= 'Favoris';
    $content .= '';
    $content .= '';
    $content .= '';
    $content .= '';

    if (!empty($folder)) {
        $content .= 'Tout supprimer';
        $content .= 'Aucun favoris.';
    }
    else{
        $content .= 'Tout supprimer';
        $content .= 'Aucun favoris.';
    }

    $content .= '';

    if (!empty($folder)) {
        $msg = '';
        foreach ($folder as $key) {
            foreach ($key as $k => $value) {
                $content .= ' ' . ucwords($value['titre']) . '';
            }
        }
    }
    else{
    }
    $content .= '';
    $content .= '';
    return $content;
}

/**
 * Add folder
 */
public function fp_ajax_addFolder(){
    $current_user = wp_get_current_user();
    $userid = $current_user->ID;
    $meta = get_user_meta($userid, 'favoris');
    if (empty($meta)) {
        if ($_REQUEST['foldername'] !== '') {
            $folder[0]['titre'] = $_REQUEST['foldername'];
            add_user_meta($userid, 'favoris', $folder);
            var_dump($folder);
            echo ucwords($_REQUEST['foldername']);
            die();
        }
    }
    if (!empty($meta)) {
        $folder = $meta[0];
        if ($_REQUEST['foldername'] != '') {
            foreach ($folder as $key => $value) {
                $i = count($folder);
                if ($value['titre'] != $_REQUEST['foldername']) {
                    $folder[$i]['titre'] = $_REQUEST['foldername'];
                    update_user_meta($userid, 'favoris', $folder);
                    var_dump($folder);
                    echo ucwords($_REQUEST['foldername']);
                    die();
                }
            }
        }
    }
}

/**
 * Remove folder
 */
public function fp_ajax_removeFolder(){
    if ( ( !isset($_POST['folderId']) || empty($_POST['folderId']) ) && ( !isset($_POST['folderName']) || empty($_POST['folderName']) ) ) {
        echo 'Pas de dossier spécifié';
        die();
    }
    else{
        $id = $_POST['folderId'];
        $name = $_POST['folderName'];
        $current_user = wp_get_current_user();
        $userid = $current_user->ID;
        $folder = get_user_meta($userid, 'favoris');
        $folderToErase = $folder[0][$id];
        try {
            delete_user_meta($userid, 'favoris', $folder[0][$id]);
            echo $folderToErase['titre'];
            //print_r($folder[0][$id]['titre']);
        } 
        catch (Exception $e) {
            echo 'not ok';
            var_dump($folder);
            return $e->getMessage();
        }
        die();
    }
}


As far as I get or create, everything goes fine. But when I want to remove a folder by key ($id), it doesn't. But when I echo the $folderToErase, the id's right.

Maybe some help ?

6
  • The third parameter for delete_user_meta() is a meta value but you refer to it as a "key". Are you sure you are using the function correctly?
    – s_ha_dum
    Commented Mar 2, 2014 at 14:28
  • Not sure. I thought it was this way first. But when I use $folder[0][$id]['titre'] as the meta value to delete, it fails as well.
    – Neovea
    Commented Mar 2, 2014 at 14:49
  • Exactly what is var_dump($userid, 'favoris', $folder[0][$id]);?
    – s_ha_dum
    Commented Mar 2, 2014 at 15:35
  • It returns : "string(4) "test" where test is the folder's name.
    – Neovea
    Commented Mar 2, 2014 at 15:46
  • How is $folder stored in the database? Is it a serialized array?
    – s_ha_dum
    Commented Mar 2, 2014 at 16:00

1 Answer 1

0

You're data is stored in a serialized array in the database, per a comment to the question:

... it's stored as the api's working for. It looks like that in the database : `a:3:{i:0;a:1:{s:5:"titre";s:3:"sfg";}i:1;a:1:{s:5:"titre";s:4:"test";}i:2;a:1:{s‌​:5:"titre";s:4:"test";}}`

You are trying to remove only part of that serialized array by passing the key to delete_user_meta(). That is not how delete_user_meta() works. It is not going to unserialize() that value and remove the individual piece of the array. It is going to delete the row in the table where the key and the value match exactly. That is not what you want. With serialized data what you end up with is an array which you need to process yourself.

Something like this should work:

//      $id = $_POST['folderId'];
// fake $_POST['folderID']
$id = 'test';
$current_user = wp_get_current_user();
$userid = $current_user->ID;
//         $folder = get_user_meta($userid, 'favoris');
// fake the get_user_meta call
$folder = unserialize('a:3:{i:0;a:1:{s:5:"titre";s:3:"sfg";}i:1;a:1:{s:5:"titre";s:4:"test";}i:2;a:1:{s:5:"titre";s:4:"test";}}');

try {
  foreach ($folder as $k => $m) {
    if ($m['titre'] === $id) {
      unset($folder[$k]);
    }
  }
  update_user_meta($userid, 'favoris', $folder);
} 
catch (Exception $e) {
  echo 'not ok';
  return $e->getMessage();
}

What you need to do is process the array, removing the parts you don't want, and then re-save it.

Note: As I started writing this code I realized that I was not 100% sure what you were trying to accomplish. The reason behind some of your $_POST data isn't completely obvious to me. That should get you on the right track though.

5
  • Weird, when I do a get_user_meta($userid, 'favoris'), I get an array. Tried to unserialize it, of course, doesn't work as it's an array. Waiting for your code. Thanks for helping.
    – Neovea
    Commented Mar 2, 2014 at 16:38
  • @Neovea : see the edit. I was not completely clear in the original version of the answer, and I added some code.
    – s_ha_dum
    Commented Mar 2, 2014 at 16:53
  • I did a bit different finally but I'm going to study your piece of code. I did an unset on the piece of array I wanted to remove, and then an update_meta_user. I have now to reorganize the index. Thanks a lot for helping !
    – Neovea
    Commented Mar 2, 2014 at 16:55
  • What you say you did is essentially the same as what I did, even if the details differ.
    – s_ha_dum
    Commented Mar 2, 2014 at 16:57
  • Yes was just to say I succeed a bit by myself ;)
    – Neovea
    Commented Mar 2, 2014 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.