<?php
/*
* Plugin Name: GST Calculator
* Plugin URI: https://example.com/gst-calculator
* Description: A simple plugin that allows users to calculate GST amounts and total prices.
* Version: 1.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: gst-calculator
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Register the GST Calculator widget.
*/
function gst_calculator_register_widget() {
register_widget( 'GST_Calculator_Widget' );
}
add_action( 'widgets_init', 'gst_calculator_register_widget' );
/**
* GST Calculator widget class.
*/
class GST_Calculator_Widget extends WP_Widget {
/**
* Widget constructor.
*/
public function __construct() {
parent::__construct(
'gst_calculator_widget',
__( 'GST Calculator', 'gst-calculator' ),
array(
'classname' => 'gst-calculator',
'description' => __( 'A widget that allows users to calculate GST amounts and total prices.', 'gst-calculator' )
)
);
}
/**
* Widget form.
*
* @param array $instance
* @return void
*/
public function form( $instance ) {
$defaults = array(
'title' => __( 'GST Calculator', 'gst-calculator' )
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'gst-calculator' ); ?></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>
<?php
}
/**
* Update widget.
*
* @param array $new_instance
* @param array $old_instance
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
/**
* Widget content.
*
* @param array $args
* @param array $instance
* @return void
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
?>
<form method="post" action="<?php the_permalink(); ?>">
<p>
<label for="gst-calculator-price"><?php _e( 'Price:', 'gst-calculator' ); ?></label>
<input id="gst-calculator-price" name="gst-calculator-price" type="text">
</p>
<p>
<label for="gst-calculator-rate"><?php _e( 'GST Rate (%):', 'gst-calculator' ); ?></label>
<input id="gst-calculator-rate" name="gst-calculator-rate" type="text">
</p>
<p>
<input type="submit" value="<?php _e( 'Calculate', 'gst-calculator' ); ?>">
</p>
</form>
<?php
if ( isset( $_POST['gst-calculator-price'], $_POST['gst-calculator-rate'] ) ) {
$price = floatval( $_POST['gst-calculator-price'] );
$rate = floatval( $_POST['gst-calculator-rate'] );
$gst = $price * $rate / 100;
$total = $price + $gst;
?>
<p>
<?php printf( __( 'GST Amount: %s', 'gst-calculator' ), $gst ); ?><br>
<?php printf( __( 'Total Price: %s', 'gst-calculator' ), $total ); ?>
</p>
<?php
}
echo $args['after_widget'];
}
}
This code creates a widget that displays a form with fields for the price and GST rate, and a button to calculate the GST amount and total price. When the form is submitted, the widget displays the calculated GST amount and total price.