hello guys I am trying to add a custom field in the database and when i update it somehow when i check it on the database i find out that there few extra characters that i did not put on the field value if you see below here is the code i am using
function set_embed($post_id) {
$player_0_embed_player = get_field( 'player_0_embed_player', $post_id );
$query = $query = 'a:1:{i:0;a:3:{s:11:"ab_hostname";s:4:"#HLS";s:8:"ab_embed";s:203:"<iframe width="1263" height="480" src="https://www.youtube.com/embed/5Gsdtetr1zo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>";s:6:"_state";s:8:"expanded";}}';
update_field( 'ab_embedgroup', $query, $post_id );
}
add_action( 'save_post_watch', 'set_embed' );
this is the value i find stored in the database
s:301:"a:1:{i:0;a:3:{s:11:"ab_hostname";s:4:"#HLS";s:8:"ab_embed";s:203:"<iframe width="1263" height="480" src="https://www.youtube.com/embed/5Gsdtetr1zo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>";s:6:"_state";s:8:"expanded";}}";
as you can see the text s:301:" in the beginning and also the "; in the end were added but i did not write them so i have to go to the database each time and remove them i am thinking of using regex for php but don't know if it is a good practice because after that i want to add variables in the query any help is much appreciated.
edit : please not that the number in s:301 is not always the same , i don't know why
edit 2 : thanks @Mikhail for the help maybe_unserialize( ); fixed the issue, this made me think to unserialize it and add it an array instead so after using unserialize tool online the value is this
Array
(
[0] => Array
(
[ab_hostname] => #HLS
[ab_embed] => <iframe width="1263" height="480" src="https://www.youtube.com/embed/5Gsdtetr1zo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
[_state] => expanded
)
)
how can i properly format this into a variable ? and thanks a lot for the help
edit 3: the filed are metabox plugin fields : however i am trying to add values to them using php the field is a group i think please check below the meta file on these values
'fields' => array(
array(
'id' => 'ab_embedgroup',
'type' => 'group',
'clone' => true,
'sort_clone' => true,
'save_state' => true,
'desc' => '<b style="color:red;">You can insert embed code or shortcode</b>',
'tab' => 'input-version',
'fields' => array(
array(
'name' => 'Host Name',
'id' => 'ab_hostname',
'type' => 'text',
),
array(
'name' => 'Embed',
'id' => 'ab_embed',
'type' => 'textarea',
'sanitize_callback' => 'none',
),
), //episode
), //input-version
array(
'name' => __( 'Shortcode Video', 'meta-box' ),
'id' => "{$prefix}embed",
'type' => 'textarea',
'clone' => 'true',
'sort_clone' => true,
'sanitize_callback' => 'none',
'tab' => 'sc-version',
),
),
for now i can fill them with just normal text after i get it fixed then i can use custom variables which is not an issue
i tried adding the array but did not work is this how it should be coded in php
$content = array(
[0] => array
(
[ab_hostname] => '#HLS',
[ab_embed] => '<iframe width="1263" height="480" src="https://www.youtube.com/embed/5Gsdtetr1zo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
[_state] => 'expanded',
)
);
edit 4 : you last code works very well but when i want to add a second ab_hostname and ab_embed it did not work i think i am doing some mistake on syntax
this works
$data_to_save = [
'ab_hostname' => $player_0_name_player
,'ab_embed' => $player_0_embed_player,
];
now i want to add more than one of them so i did this but it does not work
$data_to_save = [
['ab_hostname' => $player_0_name_player,'ab_embed' => $player_0_embed_player,],
['ab_hostname' => $player_1_name_player,'ab_embed' => $player_1_embed_player,],
];
ab_embedgroup- if it's a repeater then pass in array of associative arrays (keys of which have same names as sub field keys). Please explain what you exactly want to achieve and what is data structure of fields that you are trying to write.