I have been having issue with the implementation of my sidebar Widget, I have tried all the possible techniques but to no avail, including trying the use of a plugin. Included is the link to the backend, if there is solution to this, please kindly help out. Thanks for your assistance in anticipation.
Enahoro O
I am running into a problem with WP_Widget that I do not understand. When I use WP_Widget to register a classic (non-block) widget, WordPress does not display a Save button on the Appearance → Widgets backend page. However, when I use wp_register_sidebar_widget() and wp_register_widget_control(), the Save button appears as expected.
Is this the intended behavior? Should WP_Widget be compatible with classic (non-block) widgets, or am I missing something in my implementation?
If this is not expected behavior here are two minimal plugin examples (The plugin’s GUI is a text input. The backend is designed to save a default value). [While I don’t expect someone to debug my code, I thought a pro might be able to scan my “WP_Widget” implementation below and tell me if I have done something wrong in my implementation.]
Using “wp_register_sidebar_widget()” and “wp_register_widget_control()”:
<?php
/*
Plugin Name: AC Your Name Two (Without WP_Widget)
Description: A simple widget plugin that displays a text prompt and input field for "Your name".
Version: 1.0
Author: Your Name
*/
/**
* Function to display the front-end of the widget
*/
function ac_your_name_two_widget_display($args) {
// Retrieve the saved options
$options = get_option('ac_your_name_two_options');
$default_name = !empty($options['default_name']) ? $options['default_name'] : __('Your name', 'text_domain');
echo $args['before_widget'];
?>
<div class="ac-your-name-two-widget">
<p><?php echo esc_html__('Your name:', 'text_domain'); ?></p>
<input type="text" value="<?php echo esc_attr($default_name); ?>" readonly />
</div>
<?php
echo $args['after_widget'];
}
/**
* Function to render the widget control form in the WordPress admin
*/
function ac_your_name_two_widget_control() {
// Retrieve existing options, or set defaults
$options = get_option('ac_your_name_two_options');
if (!$options) {
$options = array('default_name' => '');
}
// Process the form when it's submitted
if ($_POST['ac_your_name_two_submit']) {
$options['default_name'] = sanitize_text_field($_POST['ac_your_name_two_default_name']);
update_option('ac_your_name_two_options', $options);
}
// Render the form
?>
<p>
<label for="ac_your_name_two_default_name"><?php _e('Default Name:', 'text_domain'); ?></label>
<input class="widefat" id="ac_your_name_two_default_name" name="ac_your_name_two_default_name" type="text" value="<?php echo esc_attr($options['default_name']); ?>" />
</p>
<input type="hidden" id="ac_your_name_two_submit" name="ac_your_name_two_submit" value="1" />
<?php
}
/**
* Registers the widget and its control
*/
function ac_your_name_two_register() {
// Define widget options for front-end and back-end
$widget_ops = array('classname' => 'ac_your_name_two_widget', 'description' => __('Displays a text input for "Your name".', 'text_domain'));
$control_ops = array('width' => 400, 'height' => 300);
// Register the widget using the older method
wp_register_sidebar_widget('ac_your_name_two_widget', __('AC Your Name Two', 'text_domain'), 'ac_your_name_two_widget_display', $widget_ops);
wp_register_widget_control('ac_your_name_two_widget', __('AC Your Name Two', 'text_domain'), 'ac_your_name_two_widget_control', $control_ops);
}
add_action('widgets_init', 'ac_your_name_two_register');
/**
* Clean up options upon plugin deactivation
*/
function ac_your_name_two_deactivate() {
delete_option('ac_your_name_two_options');
}
register_deactivation_hook(__FILE__, 'ac_your_name_two_deactivate');
Using “WP_Widget (no Save button):
<?php
/*
Plugin Name: AC Your Name Two (WP_Widget)
Description: A widget plugin that displays a text prompt and input field for "Your name" with isolated functions for display and backend.
Version: 1.0
Author: Your Name
*/
/**
* Function to handle front-end display of the widget.
*/
function ac_your_name_two_display_widget($args, $instance) {
$default_name = !empty($instance['default_name']) ? $instance['default_name'] : __('Your name', 'text_domain');
echo $args['before_widget'];
?>
<div class="ac-your-name-two-widget">
<p><?php echo esc_html__('Your name:', 'text_domain'); ?></p>
<input type="text" value="<?php echo esc_attr($default_name); ?>" readonly />
</div>
<?php
echo $args['after_widget'];
}
/**
* Function to handle the back-end form in the widget settings.
*/
function ac_your_name_two_display_form($instance, $widget) {
$default_name = !empty($instance['default_name']) ? $instance['default_name'] : '';
?>
<p>
<label for="<?php echo esc_attr($widget->get_field_id('default_name')); ?>"><?php _e('Default Name:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($widget->get_field_id('default_name')); ?>" name="<?php echo esc_attr($widget->get_field_name('default_name')); ?>" type="text" value="<?php echo esc_attr($default_name); ?>" />
</p>
<?php
}
/**
* AC Your Name Two Widget Class extending WP_Widget
*/
class AC_Your_Name_Two_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'ac_your_name_two_widget', // Base ID
__('AC Your Name Two', 'text_domain'), // Widget name
array('description' => __('Displays a text input for "Your name".')) // Options
);
}
// Call the isolated front-end display function
public function widget($args, $instance) {
ac_your_name_two_display_widget($args, $instance);
}
// Call the isolated back-end form function
public function form($instance) {
ac_your_name_two_display_form($instance, $this);
}
// Update widget settings
public function update($new_instance, $old_instance) {
$instance = array();
$instance['default_name'] = (!empty($new_instance['default_name'])) ? sanitize_text_field($new_instance['default_name']) : '';
return $instance;
}
}
// Register the widget
function register_ac_your_name_two_widget() {
register_widget('AC_Your_Name_Two_Widget');
}
add_action('widgets_init', 'register_ac_your_name_two_widget');
// Clean up widget data when plugin is deactivated
function ac_your_name_two_deactivate() {
unregister_widget('AC_Your_Name_Two_Widget');
delete_option('widget_ac_your_name_two_widget');
}
register_deactivation_hook(__FILE__, 'ac_your_name_two_deactivate');
]]>I have a Posts page and a ACF Post type. I want the Recent Posts Widget ONLY on the Posts page.
I have the Recent Posts Widget on the Posts page – great! It’s also appearing on my ACF Post Type. If I delete the widget in the Colibri theme, it deletes it on both the Posts page and my ACF Post Type. If I unlink all the styles before removing the widget, it still deletes it off of both pages.
Is there a way to customize the removal of this widget only on my ACF page, and not my Posts page?
]]>Please help us with this.
]]>I am using the code below to register my sidebars and call them but it's not working.
/* Add Multiple sidebar
*/
if ( function_exists(‘register_sidebar’) ) {
$sidebar1 = array(
‘before_widget’ => ‘<div class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2 class=”widgettitle”>’,
‘after_title’ => ‘</h2>’,
‘name’=>__( ‘My sidebar 1’, ‘textdomain’ ),
);
$sidebar2 = array(
‘before_widget’ => ‘<div class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2 class=”widgettitle”>’,
‘after_title’ => ‘</h2>’,
‘name’=>__( ‘My sidebar 2’, ‘textdomain’ ),
);
$sidebar3 = array(
‘before_widget’ => ‘<div class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2 class=”widgettitle”>’,
‘after_title’ => ‘</h2>’,
‘name’=>__( ‘My sidebar 3’, ‘textdomain’ ),
);
$sidebar4 = array(
‘before_widget’ => ‘<div class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2 class=”widgettitle”>’,
‘after_title’ => ‘</h2>’,
‘name’=>__( ‘My sidebar 4’, ‘textdomain’ ),
);
register_sidebar($sidebar1);
register_sidebar($sidebar2);
register_sidebar($sidebar3);
register_sidebar($sidebar4);
}`
<?php if ( is_active_sidebar( 'My sidebar 1' ) ) { ?>
<ul id="sidebar">
<?php dynamic_sidebar('My sidebar 1'); ?>
</ul>
<?php } ?>
]]>There is a problem with the last version(3.2) with the plugin’s widget on the sidebar, I want it to include only the titles of the related posts, as a catalog.
This module needs some other styling than the main bottom crp module because the titles look like boxes with border and they have distance between, also are smaller than the full width of the sidebar. I want them the way they looked with version 3.1
This is because in the last version the main div will always include crp_related class. So any change to the sidebar crp widget will have affect on the bottom crp module and will brake it, and this is a problem!
We didn’t have this problem with version 3.1, and that is why 3.1.1 is the version I use now.
Please, show us a way to fix this issue.
]]>1. Sidebar widget display issues
2. No widgets on the homepage
3. No pagination
4. Thumbs not populating