Category: Plugin

WordPress: Add plugin settings link to Plugins page

When creating a plugin that has its own settings page, it’s frequently helpful to make a connection to the settings page straight from the Plugins list – this spares clients the time it takes to discover where precisely your module shows up in the administrator menu. Here is a basic code scrap that makes the settings interface for you.

function plugin_add_settings_link( $links ) {
    $settings_link = '<a href="options-general.php?page=plugin_name">' . __( 'Settings' ) . '</a>';
    array_push( $links, $settings_link );
  	return $links;
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'plugin_add_settings_link' );

Just replace the href attribute with the link to the plugin settings page and rename the function to something slightly more unique.


Set, get and destroy cookies in WordPress

You can either retrieve and manipulate cookies on the server side using PHP or client side, using JavaScript.

In PHP, you set cookies using setcookie(). Note that this must be done before any output is sent to the browser which can be quite the challenge in WordPress. You’re pretty much limited to some of the early running hooks which you can set via a plugin or theme file

add_action('init', function() {
    // yes, this is a PHP 5.3 closure, deal with it
    if (!isset($_COOKIE['my_cookie'])) {
        setcookie('my_cookie', 'some default value', strtotime('+1 day'));

Retrieving cookies in PHP is much easier. Simply get them by name from the $_COOKIE super global’

$cookieValue = $_COOKIE['my_cookie'];

Unsetting a cookie requires setting one with an expiration date in the past, something like

setcookie('my_cookie', null, strtotime('-1 day'));

WooCommerce: Add custom Add To Cart Message & Custom “Continue Shopping Link”

Add this code in function.php




* Custom Add To Cart Messages

* Add this to your theme functions.php file


add_filter( 'wc_add_to_cart_message', 'woocommrece_custom_add_to_cart_message' );

function woocommrece_custom_add_to_cart_message() {

global $woocommerce;


// Output success messages

if (get_option('woocommerce_cart_redirect_after_add')=='yes') :


$return_to = get_permalink(woocommerce_get_page_id('shop'));// Give the url, you want to redirect


$message = sprintf('<a href="%s">%s</a> %s', $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );


else :


$message = sprintf('<a href="%s">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );




return $message;


/* Custom Add To Cart Messages */


AJAX in WordPress Plugins

This article, aimed at plugin developers, describes how to add Ajax to a plugin.

Step 1:

Create a javascript file call my_query.js to handle you request and save it on js(js/my_query.js) folder of your plugin.

// JavaScript Document
function myfunction(id)
 var ajaxval = id;
    type: 'POST',
    url:  ajaxvoteajax.ajaxurl,
    data: {
      'action': 'my_action',
      'val': ajaxval
    context: document.body,
    success: function(data, textStatus, XMLHttpRequest){
      var data = data; 
       $('#model').html(data);    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    complete: function(XMLHttpRequest, status) {

Note: Replace action parameter by your action. In this example I used “my_action” as the action.

Step 2:

With external files, we must enqueue them in PHP so they are included on the page

add_action('wp_enqueue_scripts', 'my_enqueue');
function my_enqueue()
wp_enqueue_script('myenqueue', plugins_url('/js/my_query.js',__FILE__), array('jquery'));
wp_localize_script( 'myenqueue', 'ajaxmy_enqueuejax', array( 'ajaxurl' => admin_url('admin-ajax.php',__FILE__) ) );

Step 3:

Now do the required operation.

// Same handler function...
add_action('wp_ajax_my_action', 'my_action_callback');
function my_action_callback() {
	global $wpdb;
	$whatever = intval( $_POST['whatever'] );
	$whatever += 10;
        echo $whatever;

Note: Use same action name for the function. In this example I used my_action. So just replace “my_action” from “function my_action_callback()” function.

Step 4:

So, if you want it to fire on the front-end for both visitors and logged-in users, you can do this:

add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

Note: ‘my_action_callback’ is the function name here. Just replace the”my_action” by your action from “wp_ajax_nopriv_my_action” and “wp_ajax_nopriv_my_action” hooks.

WordPress from submite 404 error(Page Not Found)

When you are going to submit a from in wordpress,  some time it will give page not found error(404). 404 error come when you use “name”,”day”, “month”, “year” as a mane of input in your form. To avoid 404 do to use “name”,”day”, “month”, “year” as input field name.