0

This may be more suited for StackExchange, so if you think that would be the right place to direct my question - please let me know :).

So, I have a class with a property of save_list. This is an 2D array with it's values being in the format array('meta_key' => $meta_key). This class is instantiated once as $metabox within my functions.php file, and thereafter the $metabox variable is used to keep reference the object.

My problem is, when it comes to the save_post action, the object seems to lose it's save_list property, in that attempting to dump it shows an array with no values.

class Metabox
{
    public function __construct() {
        $this->save_list = array();
        add_action('save_post', array($this, 'save_postmeta'));
    }

    public function text($meta_key, $atts) {
        global $post;

        $this->save_list[] = array('meta_key' => $meta_key);
        $default_atts = array(
            'value' => false,
            'class' => ''
        );
        $atts = $atts + $default_atts;
        $atts['value'] = (!$atts['value']) ? get_post_meta($post->ID, $meta_key, true) : $atts['value'];

        extract($atts);
        print_r($this->save_list);
        ?>
        <input name="<?php echo $meta_key; ?>" type="text" class="<?php echo $class; ?>" value="<?php echo $value; ?>">
        <?php
    }

    public function save_postmeta($post_id) {
        $save_list = $this->save_list;
        foreach ($save_list as $item) {
            $meta_key = $item['meta_key'];
            update_post_meta($post_id, $meta_key, $_POST[$meta_key]);
        }
        exit(var_dump($save_list));
    }

}

However, the call to print_r($this->save_list) within the text() method works fine, and keeps scope accordingly, e.g. every call of it adds to the value of save_list when dumped with that call.

I'm wondering if this has something to do with how the save_post action works, or perhaps I'm extremely tired and have made a simple, mundane error.

Any help would be greatly appreciated!

(If you think it would help for context, my functions.php file)

$metabox = new Metabox;
function add_input_metabox() {
    add_meta_box('text1', 'text1', 'input_metabox_callback', 'eating', 'side');
    add_meta_box('text2', 'text2', 'input_metabox_callback_', 'eating', 'side');
}
add_action('add_meta_boxes', 'add_input_metabox');

function input_metabox_callback() {
    global $metabox;
    $metabox->text('text3', array('class' => 'widefat'));
    $metabox->text('text4', array('class' => 'widefat'));
}

function input_metabox_callback_() {
    global $metabox;
    $metabox->text('text1', array('class' => 'widefat'));
    $metabox->text('text2', array('class' => 'widefat'));
}
1

$metabox is not in the global scope, that is why your two functions is not working. If you turn debug on you will get a a lot of debugging notice error messages regarding the use of $metabox. Simply doing the following

$metabox = new Metabox;

doesn't make a variable global. It need to be defined as a global. But before you go on and do this, take into consideration that it is recommended to stay out of the global scope, specially with an easy-to-recreate variable like $metabox.

Just think about this, you have two plugins using the same variable $metabox. This works great. Now, you define $metabox as a global in your theme. This changes the values for all instances of $metabox which leads to your plugins using wrong data and failing in what it should do.

PROPER SOLUTION

Create yourself a "global function" which you can reuse, something like

function metabox_global_function()
{
    $metabox = new Metabox;
    return $metabox;
}

Then you can use metabox_global_function(); anywhere in any function.

| improve this answer | |

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.