I am looking to add woocommerce shipping calculator to my delivery policy page with a simple short code. I found a plugin to do that but it's hasn't been updated in 5-7 years and it's filled with bugs and conflict. Can someone look at this code and help me figure out what's wrong with it.
Thanks.
global $shortcode_times;
add_action( 'wp', 'ss_woo_shipping_calculator_ajax' );
function ss_woo_shipping_calculator_ajax() {
if( $_POST["action"] != "ss_woo_shipping_calculator")
return false;
$result = array();
try {
WC()->shipping->load_shipping_methods();
WC()->shipping->reset_shipping();
$country = wc_clean( $_POST['country'] );
$state = wc_clean( isset( $_POST['state'] ) ? $_POST['state'] : '' );
$postcode = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ? wc_clean( $_POST['postcode'] ) : '';
$city = apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ? wc_clean( $_POST['city'] ) : '';
if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) );
} elseif ( $postcode ) {
$postcode = wc_format_postcode( $postcode, $country );
}
if ( $country ) {
WC()->customer->set_location( $country, $state, $postcode, $city );
WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
} else {
WC()->customer->set_to_base();
WC()->customer->set_shipping_to_base();
}
WC()->customer->set_calculated_shipping( true );
WC()->customer->save();
do_action( 'woocommerce_calculated_shipping' );
$cu = get_woocommerce_currency_symbol();
WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
$packages = WC()->shipping->get_packages();
if(count($packages ) > 0){
$result["result"] = 1;
$message = "<ul>";
foreach ( $packages as $i => $package ) {
if(count($package['rates']) > 0){
foreach($package['rates'] as $k => $v){
$message .= "<li>".$v->label." <span> - ".$cu." ".$v->cost."</span></li>";
}
}
}
$result["message"] = $message ;
}
} catch ( Exception $e ) {
$result["result"] = 0;
$result["message"] = $e->getMessage() ;
}
echo json_encode($result);
die();
}
function ss_woo_shipping_calculator_shortcode() {
global $shortcode_times;
$shortcode_times++;
if($shortcode_times > 1){
return "";
}
wp_enqueue_script( 'wc-cart' );
ob_start();
do_action( 'woocommerce_before_shipping_calculator' );
?>
<form>
<section class="shipping-calculator-form-shortcode" >
<p class="form-row form-row-wide" id="calc_shipping_country_field">
<select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
<option value=""><?php _e( 'Select a country…', 'woocommerce' ); ?></option>
<?php
foreach ( WC()->countries->get_shipping_countries() as $key => $value )
echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
?>
</select>
</p>
<p class="form-row form-row-wide" id="calc_shipping_state_field">
<?php
$current_cc = WC()->customer->get_shipping_country();
$current_r = WC()->customer->get_shipping_state();
$states = WC()->countries->get_states( $current_cc );
// Hidden Input
if ( is_array( $states ) && empty( $states ) ) {
?><input type="hidden" name="calc_shipping_state" id="calc_shipping_state" placeholder="<?php esc_attr_e( 'State / County', 'woocommerce' ); ?>" /><?php
// Dropdown Input
} elseif ( is_array( $states ) ) {
?><span>
<select name="calc_shipping_state" id="calc_shipping_state" placeholder="<?php esc_attr_e( 'State / County', 'woocommerce' ); ?>">
<option value=""><?php esc_html_e( 'Select a state…', 'woocommerce' ); ?></option>
<?php
foreach ( $states as $ckey => $cvalue )
echo '<option value="' . esc_attr( $ckey ) . '" ' . selected( $current_r, $ckey, false ) . '>' . esc_html( $cvalue ) . '</option>';
?>
</select>
</span><?php
// Standard Input
} else {
?><input type="text" class="input-text" value="<?php echo esc_attr( $current_r ); ?>" placeholder="<?php esc_attr_e( 'State / County', 'woocommerce' ); ?>" name="calc_shipping_state" id="calc_shipping_state" /><?php
}
?>
</p>
<?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ) : ?>
<p class="form-row form-row-wide" id="calc_shipping_city_field">
<input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_city() ); ?>" placeholder="<?php esc_attr_e( 'City', 'woocommerce' ); ?>" name="calc_shipping_city" id="calc_shipping_city" />
</p>
<?php endif; ?>
<?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>
<p class="form-row form-row-wide" id="calc_shipping_postcode_field">
<input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_postcode() ); ?>" placeholder="<?php esc_attr_e( 'Postcode / ZIP', 'woocommerce' ); ?>" name="calc_shipping_postcode" id="calc_shipping_postcode" />
</p>
<?php endif; ?>
<p><button value="1" class="button ss-woo-shipping-calculator"><?php _e( 'Update totals', 'woocommerce' ); ?></button><span id="ss-woo-shipping-calculator-loading" style="display:none"><img src='<?php echo plugins_url( '/default.gif', __FILE__ ) ?>' /></span></p>
<?php wp_nonce_field( 'woocommerce-cart' ); ?>
<div id="ss-woo-shipping-result">
</div>
</section>
</form>
<script type="text/javascript">
var $s = jQuery.noConflict();
$s(document).ready(function($) {
$(".ss-woo-shipping-calculator").click(function(){
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
var country = $(this).parent().parent().find("#calc_shipping_country").val();
var state = $(this).parent().parent().find("#calc_shipping_state").val();
var city = $(this).parent().parent().find("#calc_shipping_city").val();
var postcode = $(this).parent().parent().find("#calc_shipping_postcode").val();
$("#ss-woo-shipping-calculator-loading").show();
var data = {'action': 'ss_woo_shipping_calculator','country': country,'state': state,'city': city,'postcode': postcode};
$.post("<?php echo get_home_url(); ?>", data, function(response) {
$("#ss-woo-shipping-calculator-loading").hide();
response = JSON.parse(response);
if(response.result == 1){
$("#ss-woo-shipping-result").html(response.message);
}else{
alert(response.message);
$("#ss-woo-shipping-result").html("");
}
return false;
});
return false;
});
});
</script>
<?php
do_action( 'woocommerce_after_shipping_calculator' );
$out = ob_get_contents();
ob_end_clean();
return $out;
}
add_shortcode('ss_woo_shipping_calculator', 'ss_woo_shipping_calculator_shortcode');
I need help cleaning the code out and also making it not conflict especially with Elementor.
Related
This question already has answers here:
Remove attribute values from product variation title and show them on separate rows
(1 answer)
Display variations attributes values as separate rows in Woocommerce cart / checkout
(1 answer)
Closed 2 years ago.
I noticed when I add to Cart products that have variations, Woocommerce uses 2 templates in Cart or Checkout pages.
if the product has only one or two variations, it uses this template as an example of my product:
<tr class="cart_item">
<td class="product-name">
3-in-1 Rechargeable Trimmer Grinder - CHINA, 3 IN 1 Grooming N5
<strong class="product-quantity">× 2</strong>
</td>
</tr>
If the product has more than two variations, it uses the following:
<tr class="cart_item">
<td class="product-name">
Mini Shaver Hair Trimmer Rechargeable
<strong class="product-quantity">× 1</strong>
<dl class="variation">
<dt class="variation-Size">Size:</dt>
<dd class="variation-Size"><p>Standard</p> </dd>
<dt class="variation-ShipsFrom">Ships From:</dt>
<dd class="variation-ShipsFrom"><p>CHINA</p> </dd>
<dt class="variation-Color">Color:</dt>
<dd class="variation-Color"><p>White</p> </dd>
</dl>
</td>
</tr>
As you notice when there are more than two it uses the class variations.
my question how to modify Woocommerce so that it uses only the 2nd template (more than two variations), in order to simplify code when i want customizing css or javascript.
more details :
review-order.php:
<tbody>
<?php
do_action( 'woocommerce_review_order_before_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '× %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
<td class="product-total">
<?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
</tr>
<?php
}
}
do_action( 'woocommerce_review_order_after_cart_contents' );
?>
</tbody>
cart-item-data.php:
<?php
/**
* Cart item data (when outputting non-flat)
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/cart-item-data.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce/Templates
* #version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<dl class="variation">
<?php foreach ( $item_data as $data ) : ?>
<dt class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( $data['key'] ); ?>:</dt>
<dd class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( wpautop( $data['display'] ) ); ?></dd>
<?php endforeach; ?>
</dl>
cart.php :
<tbody>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-remove">
<?php
echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'woocommerce_cart_item_remove_link',
sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
esc_html__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
),
$cart_item_key
);
?>
</td>
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</td>
<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
}
do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
// Meta data.
echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.
// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $product_id ) );
}
?>
</td>
<td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
<td class="product-quantity" data-title="<?php esc_attr_e( 'Quantity', 'woocommerce' ); ?>">
<?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input(
array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->get_max_purchase_quantity(),
'min_value' => '0',
'product_name' => $_product->get_name(),
),
$_product,
false
);
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item ); // PHPCS: XSS ok.
?>
</td>
<td class="product-subtotal" data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
</tr>
<?php
}
}
?>
<?php do_action( 'woocommerce_cart_contents' ); ?>
<tr>
<td colspan="6" class="actions">
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon">
<label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
<?php } ?>
<button type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'woocommerce' ); ?>"><?php esc_html_e( 'Update cart', 'woocommerce' ); ?></button>
<?php do_action( 'woocommerce_cart_actions' ); ?>
<?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
</td>
</tr>
<?php do_action( 'woocommerce_after_cart_contents' ); ?>
</tbody>
wc-product-functions.php :
/**
* Variation Formatting.
*
* Gets a formatted version of variation data or item meta.
*
* #param array|WC_Product_Variation $variation Variation object.
* #param bool $flat Should this be a flat list or HTML list? (default: false).
* #param bool $include_names include attribute names/labels in the list.
* #param bool $skip_attributes_in_name Do not list attributes already part of the variation name.
* #return string
*/
function wc_get_formatted_variation( $variation, $flat = false, $include_names = true, $skip_attributes_in_name = false ) {
$return = '';
if ( is_a( $variation, 'WC_Product_Variation' ) ) {
$variation_attributes = $variation->get_attributes();
$product = $variation;
$variation_name = $variation->get_name();
} else {
$product = false;
$variation_name = '';
// Remove attribute_ prefix from names.
$variation_attributes = array();
if ( is_array( $variation ) ) {
foreach ( $variation as $key => $value ) {
$variation_attributes[ str_replace( 'attribute_', '', $key ) ] = $value;
}
}
}
$list_type = $include_names ? 'dl' : 'ul';
if ( is_array( $variation_attributes ) ) {
if ( ! $flat ) {
$return = '<' . $list_type . ' class="variation">';
}
$variation_list = array();
foreach ( $variation_attributes as $name => $value ) {
// If this is a term slug, get the term's nice name.
if ( taxonomy_exists( $name ) ) {
$term = get_term_by( 'slug', $value, $name );
if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) {
$value = $term->name;
}
}
// Do not list attributes already part of the variation name.
if ( '' === $value || ( $skip_attributes_in_name && wc_is_attribute_in_product_name( $value, $variation_name ) ) ) {
continue;
}
if ( $include_names ) {
if ( $flat ) {
$variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
} else {
$variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
}
} else {
if ( $flat ) {
$variation_list[] = rawurldecode( $value );
} else {
$variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
}
}
}
if ( $flat ) {
$return .= implode( ', ', $variation_list );
} else {
$return .= implode( '', $variation_list );
}
if ( ! $flat ) {
$return .= '</' . $list_type . '>';
}
}
return $return;
}
class-wc-order-item-meta.php :
/**
* Display meta in a formatted list.
*
* #param bool $flat Flat (default: false).
* #param bool $return Return (default: false).
* #param string $hideprefix Hide prefix (default: _).
* #param string $delimiter Delimiter used to separate items when $flat is true.
* #return string|void
*/
public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
$output = '';
$formatted_meta = $this->get_formatted( $hideprefix );
if ( ! empty( $formatted_meta ) ) {
$meta_list = array();
foreach ( $formatted_meta as $meta ) {
if ( $flat ) {
$meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
} else {
$meta_list[] = '
<dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
<dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
';
}
}
if ( ! empty( $meta_list ) ) {
if ( $flat ) {
$output .= implode( $delimiter, $meta_list );
} else {
$output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
}
}
}
$output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this, $flat );
if ( $return ) {
return $output;
} else {
echo $output; // WPCS: XSS ok.
}
}
hi i need to registry two widget of two post type. the first type is movies and the second is news
i have add the widget to function but is showing just one widget of them.
i have create two classes and when i test only one of theme its working good ? but when i add it together i see only one of them
this is my code
class Movies_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
parent::__construct('wpsites-recent-posts', esc_html__('Recent Movies','artor'), $widget_ops);
$this->alt_option_name = 'Movies_Recent_Posts';
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 widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Recent Movies','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('movies',
'ignore_sticky_posts' => true
) ) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<small class="wmovie-date"><?php echo get_the_date(); ?></small>
<hr>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'movies_widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['Movies_Recent_Posts']) )
delete_option('Movies_Recent_Posts');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('movies_widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of movies to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
}
}
class News_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
parent::__construct('wpsites-recent-posts', esc_html__('Recent News','artor'), $widget_news);
$this->alt_option_name = 'News_Recent_Posts';
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 widget($args, $instance_news) {
$cache_news = array();
if ( ! $this->is_preview() ) {
$cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache_news ) ) {
$cache_news = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache_news[ $args['widget_id'] ] ) ) {
echo $cache_news[ $args['widget_id'] ];
return;
}
ob_start();
$title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );
$number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
if ( ! $number_news )
$number_news = 5;
$show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;
$r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number_news,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('news',
'ignore_sticky_posts' => true
) ) ) );
if ($r_news->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title_news ) {
echo $args['before_title'] . $title_news . $args['after_title'];
} ?>
<ul>
<?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date_news ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache_news[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'news_widget_recent_posts', $cache_news, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance_news ) {
$instance_news = $old_instance_news;
$instance_news['title'] = strip_tags($new_instance['title']);
$instance_news['number'] = (int) $new_instance['number'];
$instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions_news = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions_news['News_Recent_Posts']) )
delete_option('News_Recent_Posts');
return $instance_news;
}
public function flush_widget_cache() {
wp_cache_delete('news_widget_recent_posts', 'widget');
}
public function form( $instance_news ) {
$title_news = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
$number_news = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
$show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title_news; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of news to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number_news; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
}
}
function load_widgets() {
register_widget('Movies_Recent_Posts');
register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
Sorry i have fixed . thank you all
class Movies_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
parent::__construct('widjet-recent-movies', esc_html__('Recent Movies','artor'), $widget_ops);
$this->alt_option_name = 'Movies_Recent_Posts';
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 widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Recent Movies','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('movies',
'ignore_sticky_posts' => true
) ) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<small class="wmovie-date"><?php echo get_the_date(); ?></small>
<hr>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'movies_widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['Movies_Recent_Posts']) )
delete_option('Movies_Recent_Posts');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('movies_widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of movies to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
}
}
class News_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
parent::__construct('widjet-recent-news', esc_html__('Recent News','artor'), $widget_news);
$this->alt_option_name = 'News_Recent_Posts';
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 widget($args, $instance_news) {
$cache_news = array();
if ( ! $this->is_preview() ) {
$cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache_news ) ) {
$cache_news = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache_news[ $args['widget_id'] ] ) ) {
echo $cache_news[ $args['widget_id'] ];
return;
}
ob_start();
$title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );
$number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
if ( ! $number_news )
$number_news = 5;
$show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;
$r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number_news,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('news',
'ignore_sticky_posts' => true
) ) ) );
if ($r_news->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title_news ) {
echo $args['before_title'] . $title_news . $args['after_title'];
} ?>
<ul>
<?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date_news ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache_news[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'news_widget_recent_posts', $cache_news, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance_news ) {
$instance_news = $old_instance_news;
$instance_news['title'] = strip_tags($new_instance['title']);
$instance_news['number'] = (int) $new_instance['number'];
$instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions_news = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions_news['News_Recent_Posts']) )
delete_option('News_Recent_Posts');
return $instance_news;
}
public function flush_widget_cache() {
wp_cache_delete('news_widget_recent_posts', 'widget');
}
public function form( $instance_news ) {
$title_news = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
$number_news = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
$show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title_news; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of news to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number_news; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
}
}
function load_widgets() {
register_widget('Movies_Recent_Posts');
register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
I am trying to create a custom recent posts widget by forking the default WordPress widget. I understand you shouldn;t hack core code, but rather fork the original widget and then register it in the functions file.
The only thing is, I don't know where to fork. I've found the default widget and I know that I'd register the widget in my theme's functions.php file, I just don't know where to save the code I'm forking. For example, do I save it as a plugin in the plugins folder, etc.?
Here's the default WordPress code:
/**
* Recent_Posts widget class
*
* #since 2.8.0
*/
class WP_Widget_Recent_Posts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "Your site’s most recent Posts.") );
parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_entries';
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') );
}
function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filter the arguments for the Recent Posts widget.
*
* #since 3.4.0
*
* #see WP_Query::get_posts()
*
* #param array $args An array of arguments used to retrieve the recent posts.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true
) ) );
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
<?php
}
}
Create a folder in Plugins and save it in there.
You don't have to create a new folder, but I would advise it just to keep the project clean.
More info here : http://codex.wordpress.org/Writing_a_Plugin
I have a wordpress widget where im saving the options, Title + Copy + URL
The url is generated by listing the wordpress pages
The below code saves and works fine on the front end. But in the widget settings panel when you save it reverts back to select page, i need it to display the previously selected item that it saved.
// Sidebar CTA Widget
class SidebarCTAWidget extends WP_Widget {
function SidebarCTAWidget() {
$widget_ops = array('classname' => 'SidebarCTAWidget', 'description' => 'Editable Sidebar CTAs' );
$this->WP_Widget('SidebarCTAWidget', 'Sidebar Call to Action', $widget_ops);
}
function form($instance) {
$defaults = array( 'title' => '', 'copy' => '', 'url' => '');
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$copy = $instance['copy'];
$url = $instance['url'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('copy'); ?>">Copy:
<textarea rows="5" class="widefat" name="<?php echo $this->get_field_name( 'copy' ); ?>" / ><?php echo esc_attr( $copy ); ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">URL:
<select class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" name="page-dropdown">
<option value=""> <?php echo esc_attr( __( 'Select Page' ) ); ?></option>
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$option = '<option value="' . get_page_link( $page->ID ) . '">';
$option .= $page->post_title;
$option .= '</option>';
echo $option;
}
?>
</select>
</label>
</p>
<?php }
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['copy'] = strip_tags( $new_instance['copy'] );
$instance['url'] = strip_tags( $new_instance['url'] );
return $instance;
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
$copy = empty($instance['copy']) ? ' ' : apply_filters('widget_title', $instance['copy']);
$url = empty($instance['url']) ? ' ' : apply_filters('widget_title', $instance['url']);
if (!empty($title))
echo '<div class="clearfix sidebar-content"><h3>' . $title . '</h3>';
;
// This is the HTML
echo '<p>' . $copy . '</p></div>';
echo '<p class="sidebar-link">Find Out More</p>';
// END
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("SidebarCTAWidget");') );
Thanks to the help of #b__ i managed to work out how to compare the saved url in the database and mark it as selected
$option = '<option '. (get_page_link( $page->ID ) == attribute_escape($url) ? "selected='selected'":"").'value="' . get_page_link( $page->ID ) . '">';
Im trying to override the text widget in WordPress. My code looks like this:
class WP_Widget_Text_Custom extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
$control_ops = array('width' => 400, 'height' => 350);
parent::__construct('text', __('Text'), $widget_ops, $control_ops);
}
function WP_Widget_Calendar( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<div class="textwidget test"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if ( current_user_can('unfiltered_html') )
$instance['text'] = $new_instance['text'];
else
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
$instance['filter'] = isset($new_instance['filter']);
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$title = strip_tags($instance['title']);
$text = esc_textarea($instance['text']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
}
function custom_register_widgets() {
register_widget( 'WP_Widget_Text_Custom' );
}
add_action( 'widgets_init', 'custom_register_widgets' );
Im getting the following error message:
*function WP_Widget::widget() must be over-ridden in a sub-class.*
I have copied the WP_Widget_Text from default-widgets.php to functions.php and added _Custom in class name.
Why am I getting this error and how do I fix it?
You might try changing WP_Widget_Calendar to be widget.