Create Custom Widget in Wordpress

Sangwan Pankaj Reply 18:17
we will create a simple widget,See this code and then paste it in your site-specific plugin or functions.php file to see it in 
action.  
 
 
// Creating the widget 
class wpb_widget extends WP_Widget {

function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget', 

// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'), 

// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), ) 
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
  
// Widget Backend 
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<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>
<?php 
}
 
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
 register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
 
 
 

Hashing Passwords with PHP Coding

Sangwan Pankaj Reply 02:50
With the last version of PHP, there are many actual methods included in the core language that you should use in your code files. These will help you a lot that you might have working out the easist way to creat a salt. So, if you are able to do, I suggest you to switch to using this method everywhere (if possible) you generate passwords.

Hashing Logic is as simple as you doing this:

password_hash('YouRSuperSecure&(*$p880$0w0rd!', PASSWORD_BCRYPT);

Here, you pass in the string that you want to hash, and the method of hashing that you’re looking to use. You can add the second parameter, and stick to the default that PHP chooses to the best.

By default the method will use a cost of 10, which is a good level to start at, however if you want to change this, you can pass through the cost as a third parameter as an array of options.

See below example sets the cost value to 16 (min is 4, max is 30)

Example:- 

password_hash('YouRSuperSecure&(*$p880$0w0rd!', PASSWORD_BCRYPT, ['cost' => 16]);


It’s just that easy like anything! Enjoy!. 

Please do share our blog with interested coders and those you always ready to learn. 

How to Insert data into table from excel file in php

Sangwan Pankaj Reply 00:29

How to Insert data into table from excel file in php


Step 1:- Open an excel file and save as "Excel Data" text file and in below sample code, "test.txt" is excel-data excel file.

Step 2:- Now, follow the below instruction for create code for insert data into table in mysql using php.


<?php
  include 'config.php';
// Connect to your database.

$fp = fopen("test.txt", "r");    // Open that file('Excel Data') for reading

while($line = fgets($fp))      // Loop through each line
{

     list ($name, $first) = explode("\t", $line);

     // Split the line by the Excel Data and store it in our list...

     $sql = "insert into user (name,phone) values ('$name', '$first')";

     // Generate sql string...

     mysql_query($sql) or die ( mysql_error() );        // Execute the sql

}

?>

Step 3:- Run file.
Copyright by GhostPHP. Powered by Blogger.

Search

Recent Post

Popular Posts

Follow us