• Resolved ddevito

    (@ddevito)


    Starts off with a walk of my configuration array.

    public function initHooks()
    	{
    		add_action('add_meta_boxes', array($this, 'walkMetaBox'));
    		add_action('save_post', array($this, 'walkForMetaKey'));
    	}
    	public function walkMetaBox()
    	{
    		array_walk($this->arrConfig, array($this, 'addMetabox'));
    	}
    	public function addMetabox($settings)
    	{
    		add_meta_box(
    			$settings['id'],
    			$settings['title'],
    			array($this, 'loadMetabox'),
    			$settings['screen'],
    			$settings['context'],
    			$settings['priority'],
    			$settings
    		);
    	}
    	public function loadMetabox($post, $settings)
    	{
                    var_dump($settings);
            }

    var dump spews out an array that is not $settings….. it shows all the variables in my whole class…

    Keep in mind this is only some of my code, however it breaks when here due to the array that is being passed to loadMetabox is distorted (not what it was before it was passed).

    Any ideas? This is a really odd error, myself and my partner in coding can’t seem to figure it out, any input is appreciated :D.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    var dump spews out an array that is not $settings….. it shows all the variables in my whole class…

    Are you sure about that?

    With this code, I expect that you have some array of arrays in the $arrConfig class variable. Since you’re walking the array here, and it has a callback of loadMetabox, then it’s going to call that callback for every metabox in your arrConfig, and dump each and every one of them. So you may not be seeing one dump, but one dump for each metabox that is being called.

    And here’s a tip: All metaboxes are called, even when they’re not shown. The hide/show thing on the Post screen is JS functionality, not PHP functionality. The metabox always exists in the HTML, it’s just hidden or shown by the JS.

    Thread Starter ddevito

    (@ddevito)

    Thanks for the timely response!

    Unfortunately i dont believe that is the case. This is because the arrConfig is actually its own file that returns an array, and has none of the things that are being “distorted”.

    What i mean by this is that, the arrConfig when passed through somehow gets injected with empty array variables declared at the top of my class. That is using the arrConfig. the array file is being passed through on class instantiation in the constructor.

    Maybe thats got something to do with it?

    Thanks in advance!

    Thread Starter ddevito

    (@ddevito)

    Essentially i am designing a plugin that generates metaboxes based off of what is in the array file.

    Thread Starter ddevito

    (@ddevito)

    This is the array that is being walked….

    return array(
    	'metabox1' 		=> array(
    		'metabox_name'		=> 'metabox1',
    		'meta_single'		=> array(),
    		'meta_array'		=> array(
    			'meta_key'			=> '',
    			'meta_defaults'		=> array(),
    		),
    		'limit_to_page_id'	=> '',
    		'id'				=> '33',
    		'title' 			=> 'Title Here',
    		'priority'			=> 'default',//Screen location (high, core, default or low)
    		'view' 				=> 'view-test.php',//should be .php and an existing view file
    		'screen'			=> 'page',
    		'context'			=> 'normal',
    		'input'				=> array(	//INDEXED: Must be the same as what is on the View File!
    			'txtName',
    		),
    	),
    	'metabox2'		=> array(
    		'metabox_name'		=> 'metabox2',
    		'meta_single'		=> array(),
    		'meta_array'		=> array(
    			'meta_key'			=> '',
    			'meta_defaults'		=> array(),
    		),
    		'limit_to_page_id'	=> '',
    		'id'				=> '44',
    		'title'				=> 'Title 2 Here',
    		'priority'			=> 'default',
    		'view'				=> 'view-test.php',
    		'screen'			=> 'post',
    		'context'			=> 'normal',
    		'input'				=> array(	//INDEXED: Must be the same as what is on the View File!
    			'txtName'
    		),
    	),
    ); // end config array
    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    I don’t know what you mean by “distorted”, and without seeing the complete code along with the incorrect output, there’s not much I can help you with. There’s nothing wrong with add_meta_box.

    Thread Starter ddevito

    (@ddevito)

    class MetaBoxController{
    
    	/*******************
    	*	Variables
    	********************/
    	protected $arrConfig = array();
    	protected $arrMetabox = array();
    	protected $arrInput = array();
    
    	protected $numberOfMetaboxes = 0;
    
    	protected $is_meta_array = false; protected $meta_array = array();
    	protected $is_meta_single = false; protected $meta_single = array();
    
    	/*******************
    	*	Variables END
    	********************/
    
    	/*******************
    	*	Initiates
    	********************/
    	//Passing through the array file through the CORE plugin using the method load_config()
    	public function __construct(Config\I_Config $config)
    	{
    		$this->arrConfig = $config->load_config();
    		$this->initHooks();
    
    		foreach ($this->arrConfig as $value)
    		{
    			$this->numberOfMetaboxes = $this->numberOfMetaboxes + 1;
    		}
    	}
    
    	public function init_single_array($metabox)
    	{
    		$this->is_meta_array = $metabox['isarray'];
    
    	}
    
    	//Starts everything
    	public function initHooks()
    	{
    		add_action('add_meta_boxes', array($this, 'walkMetaBox'));
    		add_action('save_post', array($this, 'walkForMetaKey'));
    	}
    
    	/*******************
    	*	Initiate end
    	********************/
    
    	/*******************
    	*	Working Functions Start
    	********************/
    	public function walkMetaBox()
    	{
    		array_walk($this->arrConfig, array($this, 'addMetabox'));
    	}
    
    	//While the array is walked, it applies this  method to each key.
    	//Creating all the metaboxes required according to the array file.
    	public function addMetabox($settings)
    	{
    		add_meta_box(
    			$settings['id'],
    			$settings['title'],
    			array($this, 'loadMetabox'),
    			$settings['screen'],
    			$settings['context'],
    			$settings['priority'],
    			$settings
    		);
                    var_dump($settings);
    	}
    
            //Eventually going to render the view file, and call on get_post_meta for data.
    	public function loadMetabox($post, $settings)
    	{
    		var_dump($settings);
    	}

    The first var dump, dumps out everything it’s suppose to. What it is suppose to dump out is the array from my previous post.

    The second var dump (in my head) is suppose to dump out the exact same thing as I am just passing it the parameter. Yet it dumps out some of the Array it is suppose to, but nested within the array are the variables i have at the top of my class. These ones to be exact…

    protected $arrConfig = array();
    protected $arrMetabox = array();
    protected $arrInput = array();

    protected $numberOfMetaboxes = 0;

    Anyway, i thought it was quite strange.

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    Okay, I see what the issue is.

    The callback set by add_meta_box does not receive what you think it receives. Specifically, your loadMetabox will receives two arguments:

    1. The $post item.
    2. The full $metabox item. Not just the value you have in $settings.

    The full metabox item contains all the settings of the metabox, which may or may not include some of the settings from your setup.

    If you define the function like this:

    public function loadMetabox($post, $metabox)

    Then your actual $settings variable will be in $metabox[‘args’].

    See the example on this page:
    https://codex.ww.wp.xz.cn/Function_Reference/add_meta_box#Callback_args

    Also read the description of $callback carefully here:
    https://codex.ww.wp.xz.cn/Function_Reference/add_meta_box#Parameters

    Thread Starter ddevito

    (@ddevito)

    oh ok, I understand. See under parameters and callback_args it states:

    “Arguments to pass into your callback function. The callback will receive the $post object and whatever parameters are passed through this variable.”

    So when I read whatever parameters are passed through I thought it was a direct pass of the parameter…

    Anyway, thanks so much for your help my man!

    Is there somewhere I can give you a gold star or something? haha

    (you can mark as resolved!)

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘add_meta_box Callback Argument being distorted…’ is closed to new replies.