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.
Related
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
I'm developing a custom wordpress widget. The widget needs some data from the user and needs to check this data server-side.
I wrote the code that checks the data inside the function 'update' of the widget. When I press the button save of the widget the function update got called correctly and my validation is executed.
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName]) strip_tags($new_instance[$fieldName]) : '' );
}
$check = validate($new_instance);
return $instance;
}
What I need is to display a message to the user based on the result of the validation. How can I do this? For what I've seen the function update is called through ajax so I can't use an admin notice.
Is it possible?How can I do that?
Try below code
add_action('admin_notices', 'misha_custom_order_status_notices');
function misha_custom_order_status_notices() {
global $pagenow, $typenow;
if( get_transient( 'fx-admin-notice-panel' )){
echo "<div class=\"updated\"><p>Custom notification comes here</p></div>";
}
}
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName])?
strip_tags($new_instance[$fieldName]) :
''
);
}
$check = validate($new_instance);
set_transient( 'fx-admin-notice-panel', true, 5 );
return $instance;
}
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.
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.
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();