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
Related
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']);
}
}
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 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 {
//....
}
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.
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