I am new to sage theme. I am trying to add custom widget in lib/setup.php file, but i get Class 'Roots\Sage\Setup\WP_Widget' not found in {path} error.
Following is my code :
class Banner_Widget extends WP_Widget {
function __construct() {
$this->WP_Widget('Banner-Widget', __('Banner Widget', 'blogerist'), $widget_ops);
add_action('save_post', array(&$this, 'flush_widget_cache'));
add_action('deleted_post', array(&$this, 'flush_widget_cache'));
add_action('switch_theme', array(&$this, 'flush_widget_cache'));
}
public function form($instance) {
//form content
}
function widget($args, $instance) {
//widget content
}
function update($new_instance, $old_instance) {
$instance = array_map('strip_tags', $new_instance);
$this->flush_widget_cache();
$alloptions = wp_cache_get('alloptions', 'options');
if (isset($alloptions['Banner-Widget'])) {
delete_option('Banner-Widget');
}
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('Banner-Widget', 'widget');
}
}
When you want to create your class by extending the WP_Widget then you should use the global namespace.
Add a \ sign before WP_Customize_Control.
class Banner_Widget extends \WP_Widget {
//....
}
Related
WordPress 5.2.4
class ved_currencies extends WP_Widget {
function __construct() {
parent::__construct(
‘ved_currencies’,
"Ved currencies",
array( 'description' =>'Show currencies'));
}
private function request_currencies(){
$date_req= time();
$currencies = ["USD", "CNY", "EUR", "JPY", "BYN", "KZT", "UAH"];
}
public function widget( $args, $instance ){
request_currencies(); // Line 30.
echo "test";
}
}
Result:
Uncaught Error
: Call to undefined function request_currencies() in
C:\OSPanel\domains\ved\wp-content\plugins\ved-currencies\ved-currencies.php
on line
30
Line 30 is marked in the code example.
Could you help me understand why this error appeared?
this keyword is used inside a class, generally withing the member functions to access non-static members of a class(variables or functions) for the current object.
class ved_currencies extends WP_Widget {
function __construct() {
parent::__construct(
‘ved_currencies’,
"Ved currencies",
array( 'description' =>'Show currencies'));
}
private function request_currencies(){
$date_req= time();
$currencies = ["USD", "CNY", "EUR", "JPY", "BYN", "KZT", "UAH"];
}
public function widget( $args, $instance ){
$this->request_currencies(); // Line 30.
echo "test";
}
}
I have call the function using $this->request_currencies();
For your better understanding please visit this link
I thought this would work without question but I am missing something.
In my wordpress child functions.php:
function HelloWorldShortcode() {
return My_Custom_Plugin_Public::display_custom_block();
}
add_shortcode('helloworld', 'HelloWorldShortcode');
display_custom_block() function:
public static function display_custom_block() {
echo "hello world hello world";
}
Unfortunately the page just cannot load this shortcode. Am I not able to call a class function from within a shortcode?
Whole class:
<?php
defined( 'ABSPATH' ) or die();
class My_Custom_Plugin_Public {
private $plugin_name;
private $version;
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
public static function display_custom_block() {
echo "hello world hello world";
}
}
First of all import the class file to functions.php as you said it's custom
require_once( __DIR__ . '/YourCustomClass.php'); //Path of file
Then in your function you can call like this
function HelloWorldShortcode() {
return My_Custom_Plugin_Public::display_custom_block();
}
add_shortcode('helloworld', 'HelloWorldShortcode');
This should work
when we want to do add_action inside a class we usually do like this:
class Myclassprefix_Some_Class extends Some_Class {
function __construct(){
add_action('wp_head', array($this, 'myfuncprefix_add_meta_tag'));
}
function myfuncprefix_add_meta_tag(){
echo '<meta name="description" content="This is an example meta tag" />';
}
}
Now I want to call the parent method instead.
I want to change:
add_action('wp_head', array($this, 'myfuncprefix_add_meta_tag'));
to:
add_action('wp_head', array($parent, 'some_parent_method'));
How to change $parent correctly? I know I can do like this:
add_action('wp_head', array($this, 'some_parent_method'));
function some_parent_method(){
parent::some_parent_method();
}
Is there any other way to do this without writing the function to call parent function?
You can call parent class method without writing a new function to do that. The only requirement is that parent class method is defined as public.
class ParentClass {
public function someMethod() {
print('Hi');
}
}
class ChildClass extends ParentClass {
public function __construct() {
add_action('wp_head', [ $this, 'someMethod']);
}
}
I am creating my own plugin for woocommerce and realized, that my custom product_type is not saved when the product has been created or updated. I am sure that the problem is coming from my plugin, but I am not sure where to look at.
Function to add the product_type (according to a post)
function extend_woocommerce()
{
Class WC_Product_Connected_To_General_Product extends WC_Product_Simple
{
public function __construct($product)
{
$this->product_type = 'connected_to_general_product';
$this->manage_stock = 'yes';
parent::__construct($product);
}
}
}
Class which is called in the plugin
Class general_stock {
/**
* general_stock constructor.
*/
function __construct()
{
if($this->check_if_woocommerce_is_active()){
add_action('init',[$this, 'add_woocommerce_product_type'])
add_filter('product_type_selector', [$this,'add_woocommerce_product_type_general_connected_product']);
...
}
/**
* add_woocommerce_product_type
*/
function add_woocommerce_product_type(){
extend_woocommerce();
}
/**
* add_woocommerce_product_type_general_connected_product
* #param $types
* #return mixed
*/
function add_woocommerce_product_type_general_connected_product($types){
$types[$this->product_type_name] = __('Connected to general Product','ln-general-stock');
return $types;
}
However "everything" works so far: I am able to select the new product type in backend and saving it aswell. (It is selected when I edit the product).
But when I query the Product in frontend and dump it, the value of product_type equals simple which I think should be connected_to_general_product or is this information stored in another value?
Thank you in advance for your help!
Remember the extends of product class name has to start with WP_Product_ followed with the producto type. Eg.:
class WC_Product_Moto extends WC_Product {
protected $product_type = 'moto';
public function get_type(){
return 'moto';
}
}
I would probably re-arrange the structure of your plugin a bit. I like to load the plugin on the woocommerce_loaded plugin, then you don't even need a conditional check to see if WooCommerce is active. But I think the problem is the array key/value pair that you are adding to the product_type_selector filter.
Class general_stock {
/**
* pseudo constructor.
*/
public static function init()
{
include_once( 'path-to/class-wc-product-connected-to-general-product.php' );
add_filter('product_type_selector', array( __CLASS__, 'add_woocommerce_product_type_general_connected_product' ) );
}
...
/**
* add_woocommerce_product_type_general_connected_product
* #param $types
* #return mixed
*/
public static function add_woocommerce_product_type_general_connected_product($types){
$types['general-product'] = __('Connected to general Product','ln-general-stock');
return $types;
}
}
add_action( 'woocommerce_loaded', 'general_stock::init');
Class WC_Product_Connected_To_General_Product extends WC_Product_Simple
{
public function __construct($product)
{
$this->product_type = 'connected_to_general_product';
$this->manage_stock = 'yes';
parent::__construct($product);
}
}
Also, add this code if doesn't work.
add_filter( 'woocommerce_product_class', array($this,'load_custom_product_type_class'), 10, 2 );
public function load_custom_product_type_class( $classname, $product_type ){
if ( $product_type == 'your_custom_product_type_here' ) {
$classname = 'YOUR_PRODUCT_TYPE_CLASS_NAME';
}
return $classname;
}
I had the same problem and found that I had to define a missing function in my case
if (class_exists('WC_Product_Simple')) {
class WC_Product_Ebook_Store extends WC_Product_Simple {
public function __construct( $product ) {
$this->product_type = 'ebook_store';
parent::__construct( $product );
}
public function get_type() {
return 'ebook_store';
}
}
}
I am talking about making a widget which extends a child widget of WP_Widget.
Using this as reference: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
Example:
My_WidgetBase extends WP_Widget
My_Widget extends My_WidgetBase
I want to know how to get My_Widget to show up with my other widgets (it is not currently). I have gotten My_WidgetBase to work.
This is important for my framework. I understand that widget(), update(), and form() must be overridden, but I could not find anything about making a grandchild of WP_Widget anywhere.
Example:
class My_WidgetBase extends WP_Widget {
public function __construct($id = 'my-widget-base', $desc = 'My WidgetBase', $opts = array()) {
$widget_ops = array();
parent::__construct( $id, $desc, $widget_ops );
}
public function widget( $args, $instance ) {
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
}
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
public function form( $instance ) {
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
return 'noform';
}
}
function RegisterMyWidgets() {
register_widget('My_WidgetBase');
}
add_action( 'widgets_init', 'RegisterMyWidgets' );
Solved digging further into this thread: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
The problem was not with my implementation of the classes, but how I included the parent in my framework. I was not working with both child and grandchild in the same php file, but two separate files with different directories.
Solving this problem was a matter of using require_once() correctly in the directory of my grandchild class.