Do something after WooCommerce order completed

If you want to do something after WooCommerce order completed then use this code.

woocommerce_order_status_completed action hook is a built-in hook of WooCommerce. It allows you to execute a function as when the order is completed. Add your stuff in inside of my_function().

add_action( 'woocommerce_order_status_completed', 'my_function' );
/*
 * Do something after WooCommerce sets an order on completed
 */
function my_function($order_id) {
	
	// order object (optional but handy)
	$order = new WC_Order( $order_id );
	// do some stuff here
	
}

Have any doubt, then comment here!

Create a Custom WordPress Plugin

To start, create a new folder in your “wp-content/plugins” folder called “my-plugin”. Then create a new file within this folder called “myplugin.php” and place this code:

<?php
/*
Plugin Name: my plugin
Plugin URL: http://www.boopathirajan.com
Description: A simple WordPress plugin
Version: 1.0
Author: Boopathi Rajan
Author URI: http://www.boopathirajan.com
*/
?>

If you refresh your administration panel’s plugin page, you’ll now see our plugin listed along with the other ones.Let’s go and activate our plugin by clicking Activate to the right of the plugin entry.

Have any doubt, then comment here!

How to add wordpress color picker in custom plugin development

WordPress has added a color picker in the core code from WordPress v3.5.Plugin developers can now take advantage of this and add a color picker easily in their plugin

How to Add Color Picker

Step 1) Create a java script named as color-script.js and place this code:

jQuery(document).ready(function($){
var myOptions = {
// you can declare a default color here,
// or in the data-default-color attribute on the input
defaultColor: false,
// a callback to fire whenever the color changes to a valid color
change: function(event, ui){},
// a callback to fire when the input is emptied or an invalid color
clear: function() {},
// hide the color picker controls on load
hide: true,
// show a group of common colors beneath the square
// or, supply an array of colors to customize further
palettes: true
};
$('.my-color-field').wpColorPicker(myOptions);
});

Step 2) Enqueue the “wp-color-picker” jquery script


add_action( 'admin_enqueue_scripts', 'mw_enqueue_color_picker' );
function mw_enqueue_color_picker( $hook_suffix ) {
// first check that $hook_suffix is appropriate for your admin page
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'my-script-handle', plugins_url('js/color-script.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}

My plugin now has a color-script.js file that has declared wp-color-picker as a dependency. We can now use the wpColorPicker jQuery method inside it.

Step 3) Add an input (example: text input) to the interface where you want the color picker

<input type="text" value="#abc" class="my-color-field"/>

You can also specify a default color for the field (this will be selected by default):

<input type="text" value="#bada55" data-default-color="#abc" class="my-color-field"/>

That’s it. Your users will now be able to choose a color from the color picker

Have any doubt, then comment here!

Add scripts to the wordpress header using custom plugin

If you would like to add scripts between <head></head> tag then you have to use wp_head() function.for example

Add the following code to your plugin

// Add scripts to wp_head()
function child_theme_head_script() {
    // Your code here
}
add_action( 'wp_head', 'child_theme_head_script' );

That’s it.Code added to wp_head() will be run just before the closing </head> tag.

Have any doubt, then comment here!

wordpress admin – create a page without it appearing in the side menu

Set the parent_slug property to null, example;

add_submenu_page( 
           null,            // -> Set to null - will hide menu link
          'Page Title',    // -> Page Title
          'Menu Title',    // -> Title that would otherwise appear in the menu
          'administrator', // -> Capability level
          'menu_handle',   // -> Still accessible via admin.php?page=menu_handle
          'page_callback' // -> To render the page
    );

This will hide the sub menu page from your parent (top level) menu link.

That’s it. If you have any query then comment here.

Add new menu items to wordpress admin by using custom plugin

To add an menu items to administration menu then you must do three things:

1. create function for building menu

function my_plugin_menu() {
	add_options_page( 'My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options' );
}

2. Register the above function using the admin_menu action hook.

add_action( 'admin_menu', 'my_plugin_menu' );

3. Create the HTML output for the page displayed when the menu item is clicked

function my_plugin_options() {
	if ( !current_user_can( 'manage_options' ) )  {
		wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
	}
	echo '<div class="wrap">';
	echo '<p>Here you can put your own HTML code</p>';
	echo '</div>';
}

That’s it. If you have any query then comment here.

Create table while activating the custom wordpress plugin

If you want create a database table while a activating the plugin then you have to register_activation_hook() function. The following code can be placed in your main plugin file. The function will create tables in your Database.


function myplugin_install() {
    global $wpdb; 
    $table_name = $wpdb->prefix . "mytable";
    $db_version = '1.0';
    /**Execute the sql statement to create table**/
    $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
        `ID` int(11) NOT NULL AUTO_INCREMENT,
        `field1` varchar(300) DEFAULT NULL,
		`field2` varchar(300) DEFAULT NULL,
		`field3` varchar(300) DEFAULT NULL,
		`field4` varchar(300) DEFAULT NULL,
		`field5` varchar(300) DEFAULT NULL,
		 PRIMARY KEY (`ID`));"; 
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    add_option( "db_version", '1.0' );  
}
// plugin activation hook
register_activation_hook(__FILE__,'myplugin_install');

Then activate your plugin.If already activated then reactivate the plugin. It automatically create table in your database. That’s it.

Have any doubt, then comment here!

Show welcome message only once per day

If you want to display a welcome message or alert box then you have to add the following JavaScript in your file.

function setCookie(key, value) {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
            document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
        }

function getCookie(key) {
           var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
           return keyValue ? keyValue[2] : null;
        }
if(getCookie('pop')==null)
{
	setCookie('pop',1);
	alert("WELCOME");
}

That’s it. In this code alert box shows only once per day.

Have any doubt, then comment here!