do_action inside class issue with add_meta_boxes hook - php

Ok, this is a little specific. I might be missing something but since it killed me enough time , even though I found a way around it, I need to know if there's a way to do it properly.
Basically I want to use add_meta_box (http://codex.wordpress.org/Function_Reference/add_meta_box ) inside a class.
What I am doing:
//an array variable I am trying to pass in the class to a callback function as a parameter
$the_array = array(
'something',
'my meta box'
);
//a class where everything happens
class some_class {
//public function that has the array and initiates the add_meta_boxes hook
public function add_box($class_array) {
//add meta boxes hook to add the meta box properly
add_action('add_meta_boxes', array($this, 'adding_custom_meta_boxes'), 10, 2);
//passing the array variable to the callback function
do_action('add_meta_boxes',$class_array);
}
//the callback function of the add_meta_boxes hook
public function adding_custom_meta_boxes($class_array) {
add_meta_box('my-meta-box', __($class_array[1]), 'render_my_meta_box', 'page', 'normal', 'default');
}
public function render_my_meta_box(){
//the code to generate the html of the meta box goes here
}
}
$class_var = new some_class();
$class_var->add_box($the_array);
I get this error:
Fatal error: Call to undefined function add_meta_box() in C:\xampp\ht.....
but only if I use do_action to pass the variables to the hook callback function
I found a way around it with global variables, but , does anybody know a correct way of doing this?
I am trying to create the meta box from inside a class and this happens. It works well from outside a class. Anybody any ideas?

your not too far off. to correct the above change to this:
public function adding_custom_meta_boxes($class_array) {
add_meta_box('my-meta-box', __($class_array[1]), array($this, 'render_my_meta_box'), 'page', 'normal', 'default');
}
remove "do_action"
do_action tells the script to perform the attached actions right now and the function add_meta_boxes has yet to load (google the load process for wp functions). Thats the whole point of add_actions / Filters!

Related

Wordpress add_filter('query_vars', 'my_new_vars'); not working as expected

I have an issue with Wordpress (there's a shocker), where it removes my get parameter, which i understand thats a WP feature for security and some other reasons.
What i'm trying to achieve is the following:
Load product page
When customer clicks book now they are redirected to an enquire now form
On enquire now form there is widget that retrieves what product the customer was looking at and using a GET parameter i can retrieve this product
I've tried to add the get parameter as follows:
# functions.php
function gpd_register_query_vars($vars)
{
$vars[0] = 'my_product_id';
return $vars;
}
add_filter('query_vars', 'gpd_register_query_vars');
Within my widget
class GPD_Get_Product_Widget extends WP_Widget
{
// ...
function widget($args, $instance)
{
global $wp_query;
var_dump($wp_query->query_vars['my_super_unique_var']);
extract($instance);
//output code
echo $args['before_widget'];
include 'widget.php';
echo $args['after_widget'];
}
}
//function to register the widget
function gpd_get_product_widget()
{
register_widget('GPD_Get_Product_Widget');
}
add_action('widgets_init', 'gpd_get_product_widget');
However, whenever i try to get the parameter it doesn't exist.
Wordpress isn't the easiest to navigate or work with. I'm really confused to why WP has made such a simple thing such as $_GET params so difficult.
Any help is much appreciated.
I found the answer and not entirely sure why this is but if you pass 2 params in your URL like so /my-page?a=1&b=2 and then use a plain old $_GET, you'll find that the the first element is a q and the second is your b param.
array(2) {
'q' =>
string(29) "/request-a-quote/a=1"
'b' =>
integer(1) "2"
}
It looks like the first param is occupied by q (reserved var) by Wordpress and anything after that is additional params which are yours (unless they are reserved by WP).
So if I was to build my URL like so:
add_query_arg(['type' => 'holiday', 'product_id' => 12345], get_permalink($page_id) );
You have to add a first param that will be ignored and then the second will be available as a $_GET.
I might be doing something wrong but this works for me for now. Any help and pointers to what I'm doing wrong would be great - as this feels wrong but works.

php Warning: Missing argument 2 for {closure}()

I have encountered a warning I am not understanding, and I'd like to know in which way this may affect my code. As far as I can tell, the code works and everything looks like it should.
I am working in a WordPress setting, however it seems to me that the problem is a misunderstanding on how to use anonymous function, and not strictly related to WordPress.
However, this is the situation. I have a class with a few methods creating standard pages, which I need to repeat for several instances of that class. The class methods return the content of the page and they work correctly.
I want to add these pages to the wordpress admin menu, and normally I would simply call the function
$t = new My_Class();
//the method of the class that generates the content
$function_name = "foo";
add_submenu_page( $parent_slug, $title, $menu_title , 'manage_options', $function_name, array( $t, $function_name) );
However, in this case the function returns the content instead of echoing it, so that would result in a blank page. So I tried this
add_submenu_page( $parent_slug, $title, $menu_title , 'manage_options', $function_name, function ($t, $function_name ) use ($t, $function_name) {
echo $t->$function_name();
} );
As I said, this works, but it generates that warning and it made me wonder if I am doing something wrong or if I am misunderstanding what the code is doing (thus potentially leading to unwanted behaviours in the future).
PS: I KNOW I could simply add a new method that echoes the content of the other one, or add a parameter to echo the content instead of returning it. However, I'd rather understand what the problem is, if any.
To get rid of the error message do this
add_submenu_page( $parent_slug, $title, $menu_title , 'manage_options', $function_name, function () use ($t, $function_name) {
echo $t->$function_name();
});
I'm not familiar with WP so I don't know what add_submenu_page() does but it seems that it passes one argument to the anonymous function, which you overwrite by adding the same named $t to the anonymous function lexical scope with use ($t, $function_name).
Essentially, second argument to function ($t, $function_name) is not being passed, but in the end it works because you have same variable names, and you overwrite them with the variables 'injected' with the use keyword

Working with smarty template engine for WHMCS. Need to use php function from external php file in .tpl file

Trying to fetch output in A.tpl but not getting any output. I think i'm doing something wrong to call php function in tpl file.
A.tpl
{myModifier}
B.php
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template)
{
return "100.70";
}
}
$smarty1 = new Smarty();
$smarty1->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
I 've already used this code. And this doesn't seem to work. It also breaks my existing working code in A.tpl post this use.
My Need here is to get output from the php function in A.tpl from an external php file.
Thanks in Advance. Sorry for being noob.
To add this modifier to Smarty and use it in your template, you're best to use a WHMCS hook.
If you create a new PHP file under the '~/includes/hooks/' directory (you can name it anything - in this case, let's use 'myhook.php'), WHMCS will automatically inject this hook on each request.
For this, you're going to want to use the ClientAreaPage hook. Inside your hook, you can then access the global $smarty variable.
Example:
function MySmartyModifierHook(array $vars) {
global $smarty;
// I recommend putting your Geolocation class in a separate PHP file,
// and using 'include()' here instead.
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template) {
return "100.70";
}
}
// Register the Smarty plugin
$smarty->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
}
// Assign the hook
add_hook('ClientAreaPage', 1, 'MySmartyModifierHook');
That should do the trick. If you want to explore with other hooks, you can take a look at the Hook Index in the WHMCS documentation.
The function names in each of your hook files must be unique.
As a side note, if you're only wanting to run this hook on specific pages, you can check the templatefile key in the passed $vars array. For example, say you only wanted this hook to run on the 'View Cart' page on the order form:
function MySmartyModifierHook(array $vars) {
global $smarty;
// If the current template is not 'viewcart', then return
if ($vars['templatefile'] != 'viewcart')
return;
// ... your code here ...
}
Also, note that with hooks like the 'ClientAreaPage' hook, returning an array of keys and values will automatically add them as Smarty variables. So if your hook function ended with return ['currentTime' => time()];, you could then use {$currentTime} in your Smarty template to output its value.

Callback_handler won't fire WooCommerce

I am building a payment Gateway for WooCommerce where the payment takes place in an offsite URL. I need that page to be able to message back to the WooCommerce plugin, and a "callback" URL is really all I need.
WooCommerce seems to have this, but I can't get it to work. You're supposed to be able to ping:
http://yoursite/wc-api/WC_your_gateway
And then you're supposed to add
add_action( 'woocommerce_api_callback', 'callback_handler' );
And then it's supposed to fire a function like this
public function callback_handler() {}
But when I go to that URL, all I see is a 1 on my page - my handler should be redirecting to another page (that's what I set it to do to make it obvious). What I'd LOVE is if anyone has an example of this working. I've tried placing the add_action and the handler function lots of places, no luck.
I have the same problem. Try to add exit; or wp_die(); in the end of your callback function.
This works for me.
I had the same problem, so, this is what worked for me:
class WC_mygateway extends WC_Payment_Gateway {
public function __construct() {
//'woocommerce_api_'.strtolower(get_class($this)) will result in 'woocommerce_api_wc_mygateway'
add_action('woocommerce_api_'.strtolower(get_class($this)), array(&$this, 'handle_callback'));
}
function handle_callback() {
//Handle the thing here!
}
}
function woocommerce_mygateway_add_gateway( $methods ) {
$methods[] = 'WC_mygateway';
return $methods
}
add_filter( 'woocommerce_payment_gateways', 'woocommerce_mygateway_add_gateway');
Make sure you are not missing any of those details, other wise it wont work. Also you can call it using http://example.com/?wc-api=wc_mygateway or http://example.com/wc-api/wc_mygateway
Hope this work for everyone getting stuck with this issue!
Have you tried using http://yoursite/wc-api/WC_your_gateway/ (add slash at the end)?
Also the add_action should be "woocommerce_api_{class_name}" instead of" woocommerce_api_callback". So for your example, it should be "woocommerce_api_wc_your_gateway".

Wordpress Plugin add_meta_box to functions.php instead of inside plugin class

I'm using a Wordpress plugin that adds a metabox to the post page via
add_meta_box( 'WPInsights', 'Insights', array(&$this,'draw_insights'), 'post', 'normal', 'high' );
I want to add a meta box the same way through my functions.php file, but it says the first argument is supposed to be a valid callback (I assume it is referring to the use of &$this because WPInsights is a class and draw_insights() is a function inside that class.
How can I write an add_meta_box function for my functions.php file that uses the WPInsights class?
Hook into the action add_meta_boxes like this:
add_action('add_meta_boxes', array(get_class(), 'draw_insights'));
I believe that the add_meta_box function does not function the same say the add_action function. The add_action reference states that it takes a callback but the add_meta_box function states that it takes a string that it uses as a callback. While the argument is named 'callback' it does not state that it will take any php callback as the add_action one does.

Categories