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="" 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="" 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="" 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="" 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,],
];
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/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/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/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/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,],
];
Share
Improve this question
edited Mar 18, 2020 at 22:58
Seddek Iguijji
asked Mar 18, 2020 at 18:37
Seddek IguijjiSeddek Iguijji
156 bronze badges
10
|
Show 5 more comments
1 Answer
Reset to default 0First: when you store data it gets serialized, and this is where these extra s:301:
and stuff like that coming from.
If you are using ACF plugin, at least i see it with functions get_field()
update_field()
You need to pass in just a value you want to store in, and it will serialize and unserialize data for you behind the scenes.
What I also see is by field name player_0_embed_player
is - you have a repeater with name player
and there's a sub field named embed_player
.
What you can do to get value from subfield is simply
$all_rows = get_field('player', $post_id); // you'll have array of rows
$your_value = isset( $all_rows[0]['embed_player'] ) ? $all_rows[0]['embed_player'] : ''; // get value of subfield from first row if it exists.
update_field('ab_embedgroup', $your_value, $post_id); // save this value to other field. i don't know why but your code does it.
//I also have no idea why you create some `$query` variable and assign it twice.
Value that is stored in database will be serialized by ACF when you save it.
UPD: after clarification
You have these data when you unserialize a metadata that were read somehow.
Array
(
[0] => Array
(
[ab_hostname] => #HLS
[ab_embed] => <iframe width="1263" height="480" src="https://www.youtube/embed/5Gsdtetr1zo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
[_state] => expanded
)
)
It's data structure is [ [ ab_hostname, ab_embed, _state ] ]
As I understand your target data structure in ab_embedgroup
field (which is of type Group
) is [ [ab_hostname, ab_embed ] ]
Why you can't just do this ?
$unserialized_data = maybe_unserialize($some_data_in_serialized_form_gathered_somehow);
$data_to_save = [
'ab_hostname' => $unserialized_data['ab_hostname'],
'ab_embed' => $unserialized_data['ab_embed'],
];
$data_to_save_as_group_field = [ $data_to_save ]; // it expects array of arrays with single array inside as it's group.
update_field( 'ab_embedgroup', $data_to_save_as_group_field, $post_id );
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. – Mikhail Commented Mar 18, 2020 at 20:59