0

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 :)

1
  • A single textarea cannot hold an array of values, when you collect that data from $_POST it's already a string value. Commented May 27, 2013 at 7:40

1 Answer 1

1

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
4
  • Turned it into an array but a var dump shows it as array (size=1) 0 => string 'array ( Commented May 27, 2013 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. Commented May 27, 2013 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. Commented May 28, 2013 at 6:43
  • @ShannonHyland If that helped, please mark it as solution (the checkmark below the voting arrows on the upper left of this answer). Commented May 17, 2014 at 19:05

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.