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.
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
On the Wordpress Codex page for 'register widget' there is basic example code given for registering a widget via your plugin:-
class MyNewWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, 'My New Widget Title' );
}
function widget( $args, $instance ) {
// Widget output
}
function update( $new_instance, $old_instance ) {
// Save widget options
}
function form( $instance ) {
// Output admin widget options form
}
}
function myplugin_register_widgets() {
register_widget( 'MyNewWidget' );
}
add_action( 'widgets_init', 'myplugin_register_widgets' );
In this code, as you can see the three functions I mentioned are provided. I want to know if I can change their names or are they pre-created Wordpress functions?
You can name your members in MyNewWidget whatever you like, but the point of extending WP_Widget is that WP_Widget's methods are all available to MyNewWidget but you can override them by writing a method of the same name. This may help explain.
I have an issue where I need to instantiate a class in wordpress so that in the constructor I can use the function get_post_types and have that hook happen before the publish_post hook (which I assume is around the publish_CPT hooks).
Here is the code I have so far
class Transient_Delete {
/**
* #var array of all the different post types on the site
*/
private $postTypes;
/**
* #var array of wordpress hooks we will have to assemble to delete all possible transients
*/
private $wpHooks;
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct()
{
$this->postTypes = array_values( get_post_types(array(), 'names', 'and') );
$this->wpHooks = $this->setWpHooks($this->postTypes);
add_action('publish_alert', array($this, 'deleteAlertTest'));
}
private function setWpHooks($postTypes)
{
$hooks = array_map(function($postType) {
return 'publish_' . $postType;
}, $postTypes);
return $hooks;
}
private function deleteAlertTest($post)
{
$postId = $post->ID;
echo 'test';
}
}
add_action( 'wp_loaded', array( 'Transient_Delete', 'init' ));
Another note here is that this is in the mu-plugins directory.
note: "alert" of publish_alert is a custom post type.
Ok this was my fault, it looks like the hook publish_alert works fine if I change the deleteAlertTest function to public. Any idea on why having it be a private function has that effect? Its within the same class.
When i edit a page, and then click on Add Media. In the Media Library tab, there is a loading image that never stops spinning. Then i click on the Upload file tab and try to upload an image, when i do i got this error message "An error occurred in the upload. Please try again later.".
So i started desactivating plugins to see if one of them is causins this, and indeed one custom plugin i created is causing this, and i don't know why
This plugin contains a master class, where i declared two plugins
/*
Plugin Name: blabla
Description: blabla
Version: 0.1
Author: User
*/
include_once plugin_dir_path( __FILE__ ).'/liste_emplois.php';
include_once plugin_dir_path( __FILE__ ).'/candidature.php';
class Manitou_Plugin
{
public function __construct()
{
add_action('widgets_init', function(){register_widget('Manitou_Liste_Widget');});
add_action('widgets_init', function(){register_widget('Manitou_Candidature_Widget');});
}
}
And my two plugins contain almost the same thing and same structure, here is one of them
class Manitou_Liste_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'Manitou_Liste',
__('blabla', 'wpb_widget_domain'),
array('description' => __('blabla'),)
);
}
public function widget($args, $instance) {
wp_enqueue_script('tablesorter', '/wp-content/plugins/manitou/scripts/jquery.tablesorter.min.js');
wp_enqueue_script('manitou-affichages', '/wp-content/plugins/manitou/scripts/manitou-affichages-3.0.1.js');
wp_enqueue_script('xdomainrequest', '/wp-content/plugins/manitou/scripts/jquery.xdomainrequest.min.js');
wp_enqueue_script('initmanitou', '/wp-content/plugins/manitou/scripts/initmanitou.js');
wp_enqueue_style('manitou-affichages', '/wp-content/plugins/manitou/css/manitou-affichages.css');
echo __("<div id='manitou_affichages'></div>", 'wpb_widget_domain');
}
public function form( $instance ) {
}
public function update( $new_instance, $old_instance ) {
}
}
function wpb_load_widget3() {
register_widget('Manitou_Liste_Widget');
}
add_action('widgets_init', 'wpb_load_widget3');
Do you see anithing that can cause the upload problem ?
Thanks a lot
I am very new to Wordpress and I need some help with this one.
I have this widget:
class MyWidget extends WP_Widget {
...
function widget($args, $instance) {
...
}
}
Then outside the widget class I have a javascript to which I have to pass the widget parameters:
function MyWidget_load_scripts() {
wp_enqueue_script(
'my-scripts', plugins_url('assets/js/myscript.js', __FILE__), array('jquery')
);
wp_localize_script('my-scripts', 'params', $widgetParameters);
}
add_action('wp_enqueue_scripts', 'MyWidget_load_scripts');
How can I get the widget parameters?
Thanks in advance