• Resolved imjscn

    (@imjscn)


    I’m trying to add a post meta box. The data I want to add in the post meta is calculated within a class, some of the data is private viriables so that I can’t make the meta box outside the class. But, if try to do_action or add_action within the class, I get error message.
    Need help to sort out how to add post meta box which contains private viariable in a class.
    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Add a function to the class that will return the value you want.

    Thread Starter imjscn

    (@imjscn)

    Thanks! I tried your solution, it works– saved the group of data into post meata, including those private variables under other name.

    Now, in the class, when the project is not new, I need to obtain the meta data and assign the value to each variable–

    $boxes = Array( 'height', 'width' );
    
    foreach ( $boxes as $data ){
    $project = get_post_meta($post->ID, $data, true);
    }
    
    $item_a = $project->height;
    $item_b = $project->width;

    I tried var_dump, can’t get $item_a and $item_b.
    Please help!

    Well, get_post_meta() doesn’t ever return an object. It will return an array or a single value. Using it as you are, with the third parameter set to true, it will return a single value, not an array.

    The second problem you are going to have is that the $project variable is being overwritten at each loop iteration, so the only value you will ever get is the last value in the loop. You need to set it differently, like this: $project[$data] = .... That way you can get your height and width like this: $item_a = $project['height'];

    You might be able to use get_post_custom() to get everything at once and avoid the loop. Its worth thinking about.

    Thread Starter imjscn

    (@imjscn)

    Thanks again!
    I will try the get_post_custom. Is this correct?–

    $fields = get_post_custom( $this->id);
    if ( !empty( $fields['width'] ) ){
    $this->item_a = $fields['width'];
    $this->item_b = $fields['height'];
    }

    Thread Starter imjscn

    (@imjscn)

    Seems the ‘width’ and ‘height’ is an anrray in itself, can print the value but the key is always 0. So, I changed to do like this:

    $boxes = Array ( 'width', 'height');
    $fields = get_post_custom( $post->ID);
    foreach ( $boxes as $data ){
    $project = $fields[$data];
    foreach ( $project as $k => $v )
    echo $data . " => " . $v . "<br />";
    }

    This gets the output of
    width => xxx
    height =>yyy

    Now I need to say:
    $item_a = xxx;
    $item_b = yyy;

    How?
    Thanks!

    Pretty close. Earlier you were assuming that you would have only single values. If that is true and you will only have one height and width value then you should be able to do this:

    $fields = get_post_custom( $this->id);
    if ( !empty( $fields['width'][0] ) ){
        $item_a = $fields['width'][0];
    }
    if ( !empty( $fields['height'][0] ) ){
        $item_a = $fields['height'][0];
    }
    Thread Starter imjscn

    (@imjscn)

    I tried

    $item_a = $project['height'];
    echo $item_a;

    it outputs nothing.

    Thread Starter imjscn

    (@imjscn)

    Great! your code works!
    Now I got the private variable’s value connected to its original name.
    Thanks a lot!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘add_action / do_action not working in a class ?’ is closed to new replies.