0

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,],
    ];
10
  • number 301 is length of your serialized text string. – Mikhail Mar 18 '20 at 20:53
  • Formatting something properly into a variable -> is only a matter of what type of variable your other function or in this case field expects to receive to save\process it. as formally array is a variable as well. I have no idea on what type of field is 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 Mar 18 '20 at 20:59
  • @Mikhail please check my edit 3 thank you for your time :) – Seddek Iguijji Mar 18 '20 at 21:16
  • So what is the problem right now? you have read data as array, you can store them, and group is like a repeater but just with one row, so it's array with one array inside, with keys(field names). What is the problem? you can't store data in this group or what? – Mikhail Mar 18 '20 at 21:30
  • @mikhail is just need help to format this correctly – Seddek Iguijji Mar 18 '20 at 21:34
0

First: 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.com/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 );
3
  • yes the theme is using metabox which i have no clue on how to deal with which made me use acf to pass values to metabox fields using this method as for $player_0_embed_player i plan to use once i fix the issue to get embed code inside the $query so, in short, please ignore $player_0_embed_player what i need now is add $query inside the custom field ab_embedgroup – Seddek Iguijji Mar 18 '20 at 19:53
  • @SeddekIguijji please explain what $query should do? why it's named query? and why you are trying to put already serialized data into acf field as it treats them as string and serializes them again, you can use maybe_unserialize function to unserialize serialized data and pass in to acf update_value – Mikhail Mar 18 '20 at 20:23
  • dude you are a lifesaver this did total fix it please check the edit it will explain everything i still need small help from you if possible and thanks – Seddek Iguijji Mar 18 '20 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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