Saturday 25 June 2016

How to Create Custom Plugin?

Hi, Learn here to create Simple form  Plugin.
Create 3 files & 1 directory as "images" name.
1. custom_fields_variable.php
2. readme.txt
3. uninstall.php

1> Here, "images" Directory for Upoad Images like, logo.

2> "readme.txt" for description about your Plugin.

3>  Copy following code in "custom_fields_variable.php"  file.
      All details are described in comments.

<?php
/**
* Plugin Name: Custom Fields Variable
* Plugin URI: #
* Description: This plugin is create custom fields & its variable. You can print this value to anywhere using variable.
* Version: 1.0
* Author: Ankit Shah
* Author URI: http://itnewcode.blogspot.com/
* License: GPL
*/
?>
<?php
/*
we’ll do is create a menu entry in the backend where we can place our settings user interface:
add_menu_page() function arguments:
1. Page title – used in the title tag of the page (shown in the browser bar) when it is displayed.
2. Menu title – used in the menu on the left.
3. Capability – the user level allowed to access the page.
4. Menu slug – the slug used for the page in the URL.
5. Function – the name of the function you will be using to output the content of the page.
6. Icon – A url to an image or a Dashicons string.
7. Position – The position of your item within the whole menu.
*/

// create custom plugin settings menu
add_action('admin_menu', 'custom_fields_variable_plugin_create_menu');

function custom_fields_variable_plugin_create_menu() {

//create new top-level menu
add_menu_page('Custom Fields Variable Plugin Settings', 'CFV Settings', 'administrator', __FILE__, 'custom_fields_variable_plugin_settings_page' , plugins_url('/images/icon.png', __FILE__) );

//call register settings function
add_action( 'admin_init', 'register_custom_fields_variable_plugin_settings' );
}


function register_custom_fields_variable_plugin_settings() {
//register our settings
register_setting( 'cfv-plugin-settings-group', 'header_logo' );  // For file upload
register_setting( 'cfv-plugin-settings-group', 'aks_name' );
register_setting( 'cfv-plugin-settings-group', 'aks_textarea' );
register_setting( 'cfv-plugin-settings-group', 'aks_radio' );
register_setting( 'cfv-plugin-settings-group', 'aks_checkbox1' );
register_setting( 'cfv-plugin-settings-group', 'aks_checkbox2' );
register_setting( 'cfv-plugin-settings-group', 'aks_color' );
register_setting( 'cfv-plugin-settings-group', 'aks_email' );
register_setting( 'cfv-plugin-settings-group', 'aks_number' );
register_setting( 'cfv-plugin-settings-group', 'aks_tel' );
register_setting( 'cfv-plugin-settings-group', 'aks_time' );
register_setting( 'cfv-plugin-settings-group', 'aks_url' );
register_setting( 'cfv-plugin-settings-group', 'aks_week' );
register_setting( 'cfv-plugin-settings-group', 'aks_month' );
register_setting( 'cfv-plugin-settings-group', 'aks_date' );
}

function custom_fields_variable_plugin_settings_page() {
?>
<div class="wrap">
<h2>Your Plugin Name</h2>

<form method="post" action="options.php">
    <?php settings_fields( 'cfv-plugin-settings-group' ); ?>
    <?php do_settings_sections( 'cfv-plugin-settings-group' ); ?>
<?php
//Upload file Code START
?>
<?php
if(function_exists( 'wp_enqueue_media' )){
    wp_enqueue_media();
}else{
    wp_enqueue_style('thickbox');
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
}
?>
<p><strong>Header Logo Image URL:</strong><br />
                <img class="header_logo" src="<?php echo get_option('header_logo'); ?>" height="100" width="100"/>
                <input class="header_logo_url" type="text" name="header_logo" size="60" value="<?php echo get_option('header_logo'); ?>">
                <a href="#" class="header_logo_upload">Upload</a>
</p>  

<script>
    jQuery(document).ready(function($) {
        $('.header_logo_upload').click(function(e) {
            e.preventDefault();

            var custom_uploader = wp.media({
                title: 'Custom Image',
                button: {
                    text: 'Upload Image'
                },
                multiple: false  // Set this to true to allow multiple files to be selected
            })
            .on('select', function() {
                var attachment = custom_uploader.state().get('selection').first().toJSON();
                $('.header_logo').attr('src', attachment.url);
                $('.header_logo_url').val(attachment.url);

            })
            .open();
        });
    });
</script>
<?php
//Upload file Code END
?>
    <table class="form-table">
<tr valign="top">
        <th scope="row">Name:</th>
        <td><input type="text" name="aks_name" value="<?php echo esc_attr( get_option('aks_name') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Textarea:</th>
        <td><textarea name="aks_textarea" rows="3" cols="50"><?php echo esc_attr( get_option('aks_textarea') ); ?></textarea></td>
        </tr>

<tr valign="top">
        <th scope="row">Radio:</th>
        <td><input type="radio" name="aks_radio" value="Male" <?php if ( Male == esc_attr( get_option('aks_radio') ) ) echo 'checked="checked"'; ?> />Male
<input type="radio" name="aks_radio" value="Female" <?php if ( Female == esc_attr( get_option('aks_radio') ) ) echo 'checked="checked"'; ?> />Female
</td>
        </tr>

<tr valign="top">
        <th scope="row">checkbox:</th>
        <td><input type="checkbox" name="aks_checkbox1" value="checkbox1" <?php if ( checkbox1 == esc_attr( get_option('aks_checkbox1') ) ) echo 'checked="checked"'; ?> />Male
<input type="checkbox" name="aks_checkbox2" value="checkbox2" <?php if ( checkbox2 == esc_attr( get_option('aks_checkbox2') ) ) echo 'checked="checked"'; ?> />Female
</td>
        </tr>

<tr valign="top">
        <th scope="row">Color:</th>
        <td><input type="color" name="aks_color" value="<?php echo esc_attr( get_option('aks_color') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">E-mail:</th>
        <td><input type="email" name="aks_email" value="<?php echo esc_attr( get_option('aks_email') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Telephone Number:</th>
        <td><input type="number" name="aks_number" value="<?php echo esc_attr( get_option('aks_number') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Mobile Number:</th>
        <td><input type="tel" name="aks_tel" value="<?php echo esc_attr( get_option('aks_tel') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Time:</th>
        <td><input type="time" name="aks_time" value="<?php echo esc_attr( get_option('aks_time') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">URL:</th>
        <td><input type="url" name="aks_url" value="<?php echo esc_attr( get_option('aks_url') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Week:</th>
        <td><input type="week" name="aks_week" value="<?php echo esc_attr( get_option('aks_week') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Month:</th>
        <td><input type="month" name="aks_month" value="<?php echo esc_attr( get_option('aks_month') ); ?>" /></td>
        </tr>

<tr valign="top">
        <th scope="row">Date:</th>
        <td><input type="date" name="aks_date" value="<?php echo esc_attr( get_option('aks_date') ); ?>" /></td>
        </tr>

    </table>
    <?php submit_button(); ?>
</form>
</div>
<?php } ?>
<?php
/*
To Print specific value of This fields paste following code:
$aks_name = get_option( 'aks_name' );
     print_r($aks_name);


//for full data loop Example:
global $wpdb;
$table_name = $wpdb->prefix . 'options';
$results = $wpdb->get_results ("SELECT * FROM $table_name;");
//print_r($results);
echo '<select>';
foreach ( $results as $result ) {
    echo '<option>'.$result->option_name.'</option>';
}
echo '</select>';

*/
?>
<?php
add_action( 'wp_head', 'api_ids' );
/*for check this code is working or not print variable & check inspect element to <head> tag.
This variable can any where in code beacuse of it is global variable  */

function api_ids() {

global $header_logo;
$header_logo = get_option( 'header_logo' );
// print_r($header_logo);

global $aks_name;
$aks_name = get_option( 'aks_name' );
// print_r($aks_name);

global $aks_textarea;
$aks_textarea = get_option( 'aks_textarea' );
// print_r($aks_textarea);

global $aks_radio;
$aks_radio = get_option( 'aks_radio' );
// print_r($aks_radio);

global $aks_checkbox1;
$aks_checkbox1 = get_option( 'aks_checkbox1' );
// print_r($aks_checkbox1);

global $aks_checkbox2;
$aks_checkbox2 = get_option( 'aks_checkbox2' );
// print_r($aks_checkbox2);

global $aks_color;
$aks_color = get_option( 'aks_color' );
// print_r($aks_color);

global $aks_email;
$aks_email = get_option( 'aks_email' );
// print_r($aks_email);

global $aks_number;
$aks_number = get_option( 'aks_number' );
// print_r($aks_number);

global $aks_tel;
$aks_tel = get_option( 'aks_tel' );
// print_r($aks_tel);

global $aks_time;
$aks_time = get_option( 'aks_time' );
// print_r($aks_time);

global $aks_url;
$aks_url = get_option( 'aks_url' );
// print_r($aks_url);

global $aks_week;
$aks_week = get_option( 'aks_week' );
// print_r($aks_week);

global $aks_month;
$aks_month = get_option( 'aks_month' );
// print_r($aks_month);

global $aks_date;
$aks_date = get_option( 'aks_date' );
// print_r($aks_date);

/*
Now You can print this variable any where like:
echo $header_logo;
echo $aks_name;
echo $aks_textarea;
echo $aks_radio;
echo $aks_checkbox1;
echo $aks_checkbox2;
echo $aks_color;
echo $aks_email;
echo $aks_number;
echo $aks_tel;
echo $aks_time;
echo $aks_url;
echo $aks_week;
echo $aks_month;
echo $aks_date;
*/
}



/*
If plugin active then & only then code execute.
first argument is for path,
Seconf argument is function.
*/
register_activation_hook( __FILE__, 'api_ids' );



/*
If plugin deactive remove code of plugin.
first argument is for path,
Seconf argument is function.
here no function defined.

Example:
register_deactivation_hook( __FILE__, 'deactive function' );
*/


/*
You can use for uninstall register_uninstall_hook() function or "uninstall.php" page.
we created "uninstall.php" page.
If plugin uninstall all data remove.
first argument is for path,
Seconf argument is function.

Example:
register_uninstall_hook( __FILE__, 'uninstall_plugin_function' );
function uninstall_plugin_function() {
  // Uninstallation code comes here
        // If uninstall is not called from WordPress, exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit();
}

//all register_setting() function add here & delete it
$header_logo = 'header_logo';
$aks_name = 'aks_name';
$aks_textarea = 'aks_textarea';
$aks_radio = 'aks_radio';
$aks_checkbox1 = 'aks_checkbox1';
$aks_checkbox2 = 'aks_checkbox2';
$aks_color = 'aks_color';
$aks_email = 'aks_email';
$aks_number = 'aks_number';
$aks_tel = 'aks_tel';
$aks_time = 'aks_time';
$aks_url = 'aks_url';
$aks_week = 'aks_week';
$aks_month = 'aks_month';
$aks_date = 'aks_date';


delete_option($header_logo);
delete_option($aks_name);
delete_option($aks_textarea);
delete_option($aks_radio);
delete_option($aks_checkbox1);
delete_option($aks_checkbox2);
delete_option($aks_color);
delete_option($aks_email);
delete_option($aks_number);
delete_option($aks_tel);
delete_option($aks_time);
delete_option($aks_url);
delete_option($aks_week);
delete_option($aks_month);
delete_option($aks_date);
}
*/

?>


4> Copy following code in "uninstall.php" file.
     This code will execute when delete or uninstall that plugin.

<?php

// If uninstall is not called from WordPress, exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit();
}

/*
all register_setting() function add here & delete it
*/
$header_logo = 'header_logo';
$aks_name = 'aks_name';
$aks_textarea = 'aks_textarea';
$aks_radio = 'aks_radio';
$aks_checkbox1 = 'aks_checkbox1';
$aks_checkbox2 = 'aks_checkbox2';
$aks_color = 'aks_color';
$aks_email = 'aks_email';
$aks_number = 'aks_number';
$aks_tel = 'aks_tel';
$aks_time = 'aks_time';
$aks_url = 'aks_url';
$aks_week = 'aks_week';
$aks_month = 'aks_month';
$aks_date = 'aks_date';




delete_option($header_logo);
delete_option($aks_name);
delete_option($aks_textarea);
delete_option($aks_radio);
delete_option($aks_checkbox1);
delete_option($aks_checkbox2);
delete_option($aks_color);
delete_option($aks_email);
delete_option($aks_number);
delete_option($aks_tel);
delete_option($aks_time);
delete_option($aks_url);
delete_option($aks_week);
delete_option($aks_month);
delete_option($aks_date);



/*
// Drop a custom db table if you created a table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}tablename" );
*/
?>




Click here to Download This Plugin.

For more Interesting, Useful Article & codes visit IT New Code.

Ankit Shah PHP Expert

IT New Code suggest a solution for your problem related to coding, Specially .PHP, Wordpress, WooCommerce, Magento, HTML and CSS. Visit Our website for Information.

1 comments:

 

Copyright @ 2016 IT New Code | Developing Code | Designing Code.

Designed by: Ankit Shah