Skip to main content
Bumped by Community user
Bumped by Community user
added 355 characters in body
Source Link
Kev
  • 57
  • 2
  • 12

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is complicated by the fact that the custom option is an array, not just a variable/value. The data is stored in an options field with the name:

swcs_postpage_id

My test data currently contains two items in the array:

a:2:{i:0;s:2:"10";i:1;s:3:"605";}

These were added by the user selecting a number of options in a form in the main plugin file. In my test data the two values are '10' and '605'.

I am not sure how to go about removing an item from the array. E.g. if the user selects '605' to delete, how do I remove that name/value from the array, leaving all the others?

I have read up on delete_option and update_option but neither are making an awful lot of sense to me at the moment!

UPDATE -based on MMK's help below, here's the code:

$toremove = 605 // I actually use a var like so $toremove = $_REQUEST['somevar']; I'm just using 605 as an example
$selectid = get_option('swcs_postpage_id');
$key = array_search ($toremove, $selectid);
unset($selectid[$key]);
update_option(swcs_postpage_id, $selectid);

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is complicated by the fact that the custom option is an array, not just a variable/value. The data is stored in an options field with the name:

swcs_postpage_id

My test data currently contains two items in the array:

a:2:{i:0;s:2:"10";i:1;s:3:"605";}

These were added by the user selecting a number of options in a form in the main plugin file. In my test data the two values are '10' and '605'.

I am not sure how to go about removing an item from the array. E.g. if the user selects '605' to delete, how do I remove that name/value from the array, leaving all the others?

I have read up on delete_option and update_option but neither are making an awful lot of sense to me at the moment!

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is complicated by the fact that the custom option is an array, not just a variable/value. The data is stored in an options field with the name:

swcs_postpage_id

My test data currently contains two items in the array:

a:2:{i:0;s:2:"10";i:1;s:3:"605";}

These were added by the user selecting a number of options in a form in the main plugin file. In my test data the two values are '10' and '605'.

I am not sure how to go about removing an item from the array. E.g. if the user selects '605' to delete, how do I remove that name/value from the array, leaving all the others?

I have read up on delete_option and update_option but neither are making an awful lot of sense to me at the moment!

UPDATE -based on MMK's help below, here's the code:

$toremove = 605 // I actually use a var like so $toremove = $_REQUEST['somevar']; I'm just using 605 as an example
$selectid = get_option('swcs_postpage_id');
$key = array_search ($toremove, $selectid);
unset($selectid[$key]);
update_option(swcs_postpage_id, $selectid);
Source Link
Kev
  • 57
  • 2
  • 12

Remove a custom options name/value pair

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is complicated by the fact that the custom option is an array, not just a variable/value. The data is stored in an options field with the name:

swcs_postpage_id

My test data currently contains two items in the array:

a:2:{i:0;s:2:"10";i:1;s:3:"605";}

These were added by the user selecting a number of options in a form in the main plugin file. In my test data the two values are '10' and '605'.

I am not sure how to go about removing an item from the array. E.g. if the user selects '605' to delete, how do I remove that name/value from the array, leaving all the others?

I have read up on delete_option and update_option but neither are making an awful lot of sense to me at the moment!