Essentially, you need to build an update method for your posts.
The Basics
Your update method needs to get the data of the custom fields, create a new array, and then store that data.
if ( get_post_meta( $post_ID, 'updated', true ) != 1 ) :
$old_data = get_post_custom( $post_ID );
$new_data = array(
'name' => $old_data[ 'name' ],
'url' => $old_data[ 'url' ]
);
Then, store that data in post meta – it will be serialized.
update_post_meta( 'custom_data', serialize( $new_data ) );
Then, remove the old fields and set a flag to show you’ve updated:
delete_post_meta( $post_ID, 'name' );
delete_post_meta( $post_ID, 'url' );
update_post_meta( $post_ID, 'updated', 1 );
endif;
As for integrating this with the plugin you linked to … there’s a bit more you need to do to get that set up. I leave that as a project for you.
Update: Splitting an existing string
According to your comments, the existing data isn’t stored in separate custom fields, but in a single custom field like name, url $ name, url $ name, url
. You can split this string into a few arrays and iterate through it to create your new data.
$oldData = "name1, url1 $ name2, url2 $ name3, url3";
$newData = array();
$dataArrays = explode( " $ ", $oldData );
// $dataArrays = ["name1, url1", "name2, url2", "name3, url3"];
foreach( $dataArrays as $dataArray ) {
$separated = explode( ", ", $dataArray );
// $separated = ["name1", "url1"];
array_push( $newData, array(
'name' => $separated[0],
'url' => $separated[1]
) );
}
/* $newData = array(
array(
'name' => 'name1',
'url' => 'url1'
),
array(
'name' => 'name2',
'url' => 'url2'
),
array(
'name' => 'name3',
'url' => 'url3'
)
);
*/