I need your help.
I have this code
<?php
class Messenger extends WP_Widget
{
function Messenger()
{
$widget_ops = array('classname' => 'Messenger', 'description' => 'Displays messages for users' );
$this->WP_Widget('Messenger', 'Messenger', $widget_ops);
}
public function form($instance)
{
extract($instance);
?>
Title
<input class="widefat"
id="<?php echo $this->get_field_id('title') ?>"
name="<?php echo $this->get_field_name('title')?>"
value="<?php if(isset($title)) echo $title; ?>"
/>
<?php
}
public function update()
{
}
public function widget()
{
}
}
add_action( 'widgets_init', function(){return register_widget("Messenger");});?>
but unfortunately this error occurred Warning: extract() expects parameter 1 to be array, null given in C:\wamp\www\pt\wp-content\plugins\Messenger\Messenger.php on line 22
I do not know why, I do not know why the $instance keep null after I submit the form.
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 am trying to learn WordPress' Widgets API and create my own widgets.
The problem: When I try to echo $instance['platform'], nothing appears.
I want to be able to get the value.
class Practice_Widget extends WP_Widget
{
function __construct()
{
$widget_ops = array(
'classname' => 'my-widget',
'description' => 'Second Widget'
);
parent::__construct('my_little_widget', 'Practice Second', $widget_ops);
}
public function widget($args, $instance)
{
echo $args['before_widget'];
echo $instance['platform'];
echo $args['after_widget'];
}
public function form ($instance)
{
?>
<p>
<label for = "<?php echo esc_attr($this->get_field_id('title')); ?>">
Title:
</label>
<select name="<?php echo esc_attr($this->get_field_name('platform')); ?>)">
<option value="face">Facebook</option>
<option value="insta">Instagram</option>
</select>
</p>
<?php
}
public function update ($new_instance, $old_instance)
{
$instance = array();
$instance['platform'] = ! empty($new_instance['platform']) ? $new_instance['platform'] : '';
}
}
Your update() function needs to return the $instance array, otherwise WordPress won't be able to save the new values (hence the reason why you don't see anything on the front-end):
public function update ($new_instance, $old_instance)
{
$instance = array();
$instance['platform'] = ! empty($new_instance['platform']) ? $new_instance['platform'] : '';
return $instance;
}
See the Widgets API documentation for more details.
I'm trying to hide a page from the 'Most Popular Post' widget in the sidebar.
I've got the 'WP Hide Post' plugin, It helps me hide from everywhere but that doesn't hide a page from the the widget.
Use the exclude argument in the WP_Query.
<?php
$excudeID = 30;// the post_id off the excluded page
$popularpost = new WP_Query( array( 'post__not_in' => array($excludeID) ) );
//use this query in your widget
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>
Another option could be to write your own widget:
class drc_PopulairPostsWithoutThatone extends WP_Widget
{
public function __construct()
{
// Instantiate the parent object
parent::__construct(false, 'Populair Posts Title');
}
public function widget($args, $instance)
{
global $wpdb;
$excludeId = 30;
$return = "<ul>";
$query = $wpdb->prepare("SELECT * FROM wp_popularpostsdata, wp_posts WHERE wp_popularpostsdata.postid = $wpdb->posts.ID AND $wpdb->posts.post_type = 'page' AND wp_popularpostsdata.postid != %d ORDER BY pageviews DESC limit 10", $excludeId);
$tops = $wpdb->get_results($query);
foreach ($tops as $top) {
$return .= '<li>' . get_the_title($top->postid) . ' - ' . $top->pageviews . '</li>';
}
$return .= "</ul>";
return $return;
}
public function update($new_instance, $old_instance)
{
// Save widget options
}
public function form($instance)
{
// Output admin widget options form
}
}
function drc_register_widgets()
{
register_widget('drc_PopulairPostsWithoutThatone');
}
add_action('widgets_init', 'drc_register_widgets');
Query taken from: https://wordpress.org/support/topic/shortcode-get-the-least-popular-posts/
Documentation about widgets: https://codex.wordpress.org/Function_Reference/register_widget
On my plugin Return curl error message display on post page like as a
post update message.
public function __construct() {
add_action('init', array(&$this, 'init'));
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'add_page'));
add_action('admin_notices', array(&$this,'socipilot_admin_notice') );
add_action('admin_bar_menu', array(&$this, 'socipilot_adminbar_links' ), 1001 );
add_action('add_meta_boxes', array(&$this, 'add_meta_box' ) );
add_action('admin_enqueue_scripts', array(&$this,'socipilot_enqueue_scripts'));
add_action('save_post', array(&$this, 'save'));
add_filter('plugin_action_links_'.SOCI_PILOT_PLUGIN_BASENAME, array(&$this,'ts_add_plugin_action_links'));
// Listen for the activate event
register_activation_hook(SOCI_PILOT_FILE, array(&$this, 'activate'));
// Deactivation plugin
register_deactivation_hook(SOCI_PILOT_FILE, array(&$this, 'deactivate'));
}
public function save_socipost($post_id) {
$res = actionPosttest($options['public_key'],$options['private_key'],$data);
if($res->error==1){
global $my_error;
echo $post_error = $res->msg;
exit;
}
public function __construct() {
add_action('save_post', array(&$this, 'save_socipost'));
//add filter on construct
add_filter('post_updated_messages', array(&$this,'socipilot_updated_messages'));
}
public function socipilot_updated_messages($messages){
session_start();
//difine your message hear
$messages['post'][11] = sprintf( __($_SESSION['soci_error']));
return $messages;
}
public function save_socipost($post_id) {
global $post;
global $socipost_save_post_flag;
//function data
//my fix code for message hear
if($error_id==0 && isset($res)):
if ($socipost_save_post_flag == 0){
$res = actionPosttest($options['public_key'],$options['private_key'],$data);
}
$socipost_save_post_flag =1;
if($res->error==1){
global $my_error;
if($_SESSION['soci_error']){
session_destroy();
}else{
session_start();
$_SESSION['soci_error']= $res->msg;
}
wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&message=11'));
die();
}
//continew data
.
.
.
}
My developed wordpress plugin which is activated with a shortcode is breaking my admin area saying that header cannot be modified. Digging a bit deeper I got to know that if the function is getting echoed than I have this problem if I use return than is ok. But the problem with return is: that I use ajax to retrieve html and in this case no output is generated.
message Cannot modify header information - headers already sent by (output started at /var/www.... web/wordpress/wp-admin/admin-header.php
MyClass{
public function __construct()
public $data;
{
require_once(dirname(__FILE__) . '/class/class.another.php');
$this->data = new Another();
add_action( 'init', array( &$this, 'init' ) );
}
public function init()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode') );
if(isset($_POST['id'])){
$param = $this->data->output_ajax_html($_POST['id']);
echo $this->shortcode_html_extended($param);
//this part breaks the buffer without echo is working but the contertn won't show up
}
}
public function shortcode()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode_html') );
}
public function shortcode_html()
{
$html = "";
$html .="";
return $html;
}
public function shortcode_html_extended($param)
{
$html = "";
//mixed with php
$html .="";
return $html;
}
}
$test = new MyClass();