Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am hoping someone can help me out.

I have created a custom meta box and trying to store an array, but when using update_post_meta, it doesn't seem to be storing it as an array.

I am getting the info via an api and it seems to be breaking down when going from the textarea to the update_post_meta.

I have tried serialize, unserialize, maybe_serialize, maybe_unserialize etc. Anyway here is my code.

$movieCast(array)

  array (
  0 => 
  array (
    'actor' => 'Anthony Hopkins',
    'actor_char' => 'Alfred Hitchcock',
    'actor_link' => 'anthony-hopkins',
    'img_path' => '/oDMLEhFmXLkTlUnErs0RxRua7kN.jpg',
  ),
  1 => 
  array (
    'actor' => 'Helen Mirren',
    'actor_char' => 'Alma Reville',
    'actor_link' => 'helen-mirren',
    'img_path' => '/dRYchPNm8WCXxWHcGtUwq9VLra1.jpg',
  ),
  2 => 
  array (
    'actor' => 'Scarlett Johansson',
    'actor_char' => 'Janet Leigh',
    'actor_link' => 'scarlett-johansson',
    'img_path' => '/dZ4uNJtLQkGlJ76eePrXYqUDWgn.jpg',
  ),...

echo "<textarea id='actors_array' name='actors_array' rows='5' cols='120'>" ?><?php var_export($movieCast); echo "</textarea>";

I have tested that the textarea is an array using echo is_array($cast) ? 'Array' : 'not an Array'; and it's returning Array.

So I figure the problem is further down at save function

    $actors_array = $_POST['actors_array'];
update_post_meta( $post_id, 'actors_array', $actors_array );

Once stored and I use $cast = get_post_meta it seems it has not been stored correctly.

When testing echo is_array($cast) ? 'Array' : 'not an Array'; it returns not an array.

echo"<pre>";print_r($cast);echo"</pre>";

returns

    array (
  0 => 
  array (
    'actor' => 'Anthony Hopkins',
    'actor_char' => 'Alfred Hitchcock',
    'actor_link' => 'anthony-hopkins',
    'img_path' => '/oDMLEhFmXLkTlUnErs0RxRua7kN.jpg',
  ),
  1 => 
  array (
    'actor' => 'Helen Mirren',
    'actor_char' => 'Alma Reville',
    'actor_link' => 'helen-mirren',
    'img_path' => '/dRYchPNm8WCXxWHcGtUwq9VLra1.jpg',
  ),
  2 => 
  array (
    'actor' => 'Scarlett Johansson',
    'actor_char' => 'Janet Leigh',
    'actor_link' => 'scarlett-johansson',
    'img_path' => '/dZ4uNJtLQkGlJ76eePrXYqUDWgn.jpg',
  ),...

So I am really hoping someone can help me out please.

Thanks :)

share|improve this question
    
A single textarea cannot hold an array of values, when you collect that data from $_POST it's already a string value. –  t31os May 27 '13 at 7:40
add comment

1 Answer

If I understand you correctly, I don't think this is a WordPress issue, just PHP.

When you var_export into the textarea, then submit that via POST, what you then have is not an array but a string. You can check is_string instead of is_array to confirm this.

If you want it to be an array again, you have to explicitly cast it as such:

$actors_array = $_POST['actors_array']; // $actors_array is a string
$actors_array = (array)$actors_array    // $actors_array is now an array
share|improve this answer
    
Turned it into an array but a var dump shows it as array (size=1) 0 => string 'array ( –  Shannon Hyland May 27 '13 at 6:31
    
I assume because its nested arrays. The underlying issue is that you're going about this wrong, trying to create a data structure from a string from a textarea. –  Milo May 27 '13 at 14:24
    
Yeah. I got it working though. Changed var_export to json_encode and then json_decode, works perfectly. Thanks for your help. –  Shannon Hyland May 28 '13 at 6:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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