Variable scope producing undefined variable notices in included files

I have found the answer in another thread. Passing variables through locate_template Essentially the WordPress function get_template_part changes the scope of the variable $user_choice. PHP default behaviour when using include() is that the variable from the current script is still available in the included file. However because get_template_part() is a function any variables in that … Read more

Enter a variable in the ‘category_name’ parameter

Yes you can pass the variable in category_name. But I think it should be get_the_title() instead of the_title(). $title = get_the_title(); $args = array( ‘category_name’ => $title ); Try this. But remember category_name only accepts category slug, not category name. You can also use following method if your page title/slug are different. And I think … Read more

How to fix Uninitialized string offset: error on a checkbox in WP Settings API

Most likely your $options is a string and not an array. It is always better to validate that the $options are in the format you are expecting especially in development time when you are likely to switch formats and too lazy to clean the DB when doing that. something like if (!isset($options[‘hide_avatar’])) $options[‘hide_avatar’] = 0; … Read more

Call global variable array() in woocommerce child/template

It is all fine. You just need to declare variable global first then you can set the value of this and access globally. function my_free_shipping( $is_available ) { global $woocommerce, $product_notfree_ship; // set the product ids that are $product_notfree_ship $product_notfree_ship = array( ‘1’, ‘2’, ‘3’, ‘4’, ‘5’ ); Then again declare it globally when using … Read more

How to put a variable in a instance in the widget

As far I understood you want to assign the order number like $order_number = order_generator($email) inside the widget class. Yes, you can do it that way unless the order_generator is in another class. If you order_generator function is in another class then first you need to instantiate the class then call the method like $instance_of_your_class->order_generator($email). … Read more

MySQL variable in query

See the documentation for $wpdb->prepare(). You need to pass a String, but you’re typing set @csum directly in the parameter. Your code should look like this: if ( is_user_logged_in() ) { $user = wp_get_current_user(); $balance = $wpdb->query( $wpdb->prepare( “SET @csum := (SELECT current_balance FROM exp_ten WHERE tenant_number=%d); SELECT tenant_number, transaction_amount, (@csum := @csum + transaction_amount) … Read more

Static variable and add_rewrite_rule?

You can’t add arbitrary query variables to the rewrite rules, there’s a whitelist of allowed variables. To add a query variable, use query_vars filter, e.g. this filter adds my-api as a valid query variable: function wpd_query_vars( $query_vars ){ $query_vars[] = ‘my-api’; return $query_vars; } add_filter( ‘query_vars’, ‘wpd_query_vars’ ); But that just gets you the query … Read more