WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add_action / do_action not working in a class ? (9 posts)

  1. imjscn
    Member
    Posted 3 years ago #

    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!

  2. s_ha_dum
    Member
    Posted 3 years ago #

    Add a function to the class that will return the value you want.

  3. imjscn
    Member
    Posted 3 years ago #

    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!

  4. s_ha_dum
    Member
    Posted 3 years ago #

    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.

  5. imjscn
    Member
    Posted 3 years ago #

    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'];
    }
  6. imjscn
    Member
    Posted 3 years ago #

    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!

  7. s_ha_dum
    Member
    Posted 3 years ago #

    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];
    }
  8. imjscn
    Member
    Posted 3 years ago #

    I tried

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

    it outputs nothing.

  9. imjscn
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic