Month: June 2013

Woo-commerce: Remove tab from product page

woocommrece tab

Picture Source: WooThemes

If you want to remove tabs from woo-commerce product details page, then add this code in your function.php.


** Remove tabs from product details page


add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );

function woo_remove_product_tabs( $tabs ) {

unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab

return $tabs;


WordPress: Read more not working

When you are creating a new wordpress theme, some time read more tag not working you blog. “Read more” are not showing on the front end of your blog though you are using read more tag in wordpress post.


To solve this problem you need to follow two simple step:


Add  the <?php global $more;?> before the loop.


Add this code inside loop.

<?php $more = 0;the_content(__('Read more', 'theemename')); ?>

How to Pass an Object from PHP to Javascript and Back Again Using JSON

Many time you need to pass a PHP object through javascript to PHP . JSON and a some PHP and Javascript functions make that easy!

1st let me create a php object.

<?php $cms = array('java'=>'OpenCms', 'php'=>'wordpress', 'python'=>'django', 'c++'=>'CppCMS') ?>

Now assign this object to a javascript variable.

var obj = JSON.parse('<?php echo json_encode($cms) ?>');

So. the object  is now on a javascript variable. Now I can pass the Javascript Object through Ajax or several other ways:

I am showing ajax here:

// <![CDATA[
function loadpage() {

var obj = JSON.parse('');

        type: "GET",
        url: "your ajax url",
        data: { obj: obj
        },        success: function(html){
// ]]>

In my PHP(on ajax page) script, I now need to translate the JSON to a php object again:

$obj = $_GET['obj']; 
$obj = json_decode(json_encode($obj), FALSE);

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.

How to Add a Metabox to a Custom Post Type

metabox1Most custom post types in WordPress will need (or could benefit from) a unique set metaboxes for entering information.

For example, a “photography” post type might need fields for “location of photo”, “type of camera”, etc. And an “event” post type would probably need a “location” and an “event date”.

Metaboxes aren’t the easiest to set up- so I’ve written up this tutorial which shows how to add a one line field for “location” to an “event” post type.

Hopefully, you’ll be able to use this guide to add any sort of metaboxes you need.

Set Up the Post Type

If you are unfamiliar with how to set up custom post types, check out Justin Tadlock’s excellent tutorial. For this example, I am going to use a post type called “Event”, which goes in my functions.php file:

// Registers the new post type and taxonomy

function wpt_event_posttype() {
	register_post_type( 'events',
			'labels' => array(
				'name' => __( 'Events' ),
				'singular_name' => __( 'Event' ),
				'add_new' => __( 'Add New Event' ),
				'add_new_item' => __( 'Add New Event' ),
				'edit_item' => __( 'Edit Event' ),
				'new_item' => __( 'Add New Event' ),
				'view_item' => __( 'View Event' ),
				'search_items' => __( 'Search Event' ),
				'not_found' => __( 'No events found' ),
				'not_found_in_trash' => __( 'No events found in trash' )
			'public' => true,
			'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
			'capability_type' => 'post',
			'rewrite' => array("slug" => "events"), // Permalinks format
			'menu_position' => 5,
			'register_meta_box_cb' => 'add_events_metaboxes'

add_action( 'init', 'wpt_event_posttype' );

You may have your own custom post type set up completely different, but that’s fine. The important line of code for the metaboxes is ‘register_meta_box_cb’ => ‘add_events_metaboxes’- which calls the function to build the metaboxes.

You can rename the function to whatever you like, for instance ‘register_meta_box_cb’ => ‘add_photography_metaboxes’ might be better for a photography post type.

If the post type is being registered through a plugin or is one of the native post types, you can also use:

add_action( 'add_meta_boxes', 'add_events_metaboxes' );

Add Meta Box

The following code adds a metabox to the right side of the screen under the “Publish” box:

// Add the Events Meta Boxes

function add_events_metaboxes() {
	add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'side', 'default');

You can read the full parameters for add_meta_box in the codex. I also listed them here:

<?php add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args ); ?>

For the example above:

  • $id is “wpt_events_location”- or the html id that will be applied to this metabox.
  • $title is “Event Location”. This appears at the top of the new metabox when displayed.
  • $callback is the function “wpt_events_location” which will load the html into the metabox.
  • $page is “events”, the name of our custom post type.
  • $context is “side”. If you wanted it to load below the content area, you could put “normal”.
  • $priority controls where the metabox will display in relation to the other metaboxes. You can put “high”, “low” or “default”.

If you wanted to have two sets of metaboxes, perhaps one on the side and one below the content area, you could do something like this (Note: Don’t use this if you’re following the tutorial step by step, this is just an example of how it would be done):

// Add the Events Meta Boxes

function add_events_metaboxes() {
	add_meta_box('wpt_events_date', 'Event Date', 'wpt_events_date', 'events', 'side', 'default');
	add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'normal', 'high');

You’d then have to make sure the two function wpt_events_date and wpt_events_location were defined to call the html code to go inside the metaboxes.

Generating the HTML for the Metabox

Continuing with the first example above, we’ll now have to generate the code that goes inside our “Event Location” metabox. To keep this as simple as possible, we’re just going to make one field:

// The Event Location Metabox

function wpt_events_location() {
	global $post;

	// Noncename needed to verify where the data originated
	echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

	// Get the location data if its already been entered
	$location = get_post_meta($post->ID, '_location', true);

	// Echo out the field
	echo '<input type="text" name="_location" value="' . $location  . '" />';


At this point you should have a metabox showing up in your post. If you check your “events” post type, it should load on the right side like in the screenshot I posted. This will generate any html you choose, so, you could put as many input fields in here as you like, or html descriptions.

In order to class the inputs and descriptions correctly, check out the source code for other write panels in WordPress. See how they do textareas and select boxes. You can even add icons and generated text in these spots.


If you had tried to save your metabox data before this point, it just would have disappeared on the refresh because it wasn’t being saved. Here’s the code that updates the metabox when you click “Update” (adapted from Nathan Rice in the AgentPress theme):

// Save the Metabox Data

function wpt_save_events_meta($post_id, $post) {

	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times
	if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
	return $post->ID;

	// Is the user allowed to edit the post or page?
	if ( !current_user_can( 'edit_post', $post->ID ))
		return $post->ID;

	// OK, we're authenticated: we need to find and save the data
	// We'll put it into an array to make it easier to loop though.

	$events_meta['_location'] = $_POST['_location'];

	// Add values of $events_meta as custom fields

	foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
		if( $post->post_type == 'revision' ) return; // Don't store custom data twice
		$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
		if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
			update_post_meta($post->ID, $key, $value);
		} else { // If the custom field doesn't have a value
			add_post_meta($post->ID, $key, $value);
		if(!$value) delete_post_meta($post->ID, $key); // Delete if blank


add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields

This code checks to make sure the user has privileges to update the post, then saves all the input data into the array $events_meta.


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.