Custom plugin causes image upload to fail - wordpress

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

Related

Load CSS/JS only when that plugin is called in wordpress

I have two plugin. The pseudo code is as below. I want to load CSS/JS only when that plugin is called.
Right now, all js/css from all the plugins are getting called. How do I prevent this ?
Plugin 1
class Plugin1 {
function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue')
}
function enqueue() {
wp_register_style('xxx','path');
wp_eneque_style('xxx);
}
}
Plugin 2
class Plugin2 {
function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue')
}
function enqueue() {
wp_register_style('xxx','path');
wp_eneque_style('xxx);
}
}

Calling a class function from functions.php shortcode

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

Are widget(), update() and form() pre-created functions in Wordpress?

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.

Making Simple Rest API call in Wordpress Pages and Widgets

I'm trying to make a simple api call to a site that needs to render the data in a Wordpress Page/Widget.
I created a new page and put this code in the editor box on my dashboard:
<?php
$response = wp_remote_get( 'https://jsonplaceholder.typicode.com/posts/2' );
if( is_array($response) ) {
$header = $response['headers'];
$body = $response['body'];
}
print($response);
print($header);
print($body);
?>
Nothing is rendering on my Wordpress UI.
Yes, i'm on my local environment (using MAMP).
Solution:
Create a folder in your plugin directory and create a .php file that will be containing your api calls.
Your structure will look something like this:
class Api extends WP_Widget {
function __construct() {
$options = array(
'description' => '',
'name' => ''
);
parent::__construct('Api', 'Widget', $options);
}
public function form($instance) {
extract($instance);
// Put your HTML widget form here
}
public function widget($args, $instance) {
extract($args);
extract($instance);
$data = $this->get_api_call($args);
}
public function get_api_call($args) {
$api = wp_remote_get("http://www.example.com/json/");
$json_api = json_decode(stripslashes($api['body']));
return $json_api;
}
}
This is a basic outline instance, you'll have to customize everything according to what you exactly need from here.

How to extend a custom widget in wordpress?

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.

Resources