Category: Wordpress

Get All Images From WordPress Media Gallery

Uploaded images are stored as posts with the type “attachment”. Use get_posts() and query for all attachments:

$args = array(
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_mime_type' =>'image',
                'post_status' => 'inherit',
                'post_parent' => null, // any parent
           );
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $post) {
         setup_postdata($post);
         the_title();
         the_attachment_link($post->ID, false);
         the_excerpt();
     }
}

Now All images are displayed.

Have any doubt, then comment here!

create an autocomplete textbox in wordpress

Just add a div under the input tag

HTML Code:

<input type="text" id="city" name="city" autocomplete="off"/> 
<div id="key"></div>

replace the div after the success on you ajax.

Ajax Code:

     var ajaxurl="<?php echo admin_url( 'admin-ajax.php' ); ?>"; 
     var data ={ action: "city_action",  city:cid    };
        $.post(ajaxurl, data, function (response){
                     $('#key').html(response);
           });

PHP Code:

       function city_action_callback() {     
       global $wpdb;
            $city=$_GET['city'];
            $result =   $mytables=$wpdb->get_results("select * from ".$wpdb->prefix . "mycity where city like '%".$city."'" );   
            $data = "";
            echo '<ul>'
            foreach($result as $dis)
            {
                         echo '<li>'.$dis->city.'</li>';
            }
            echo '</ul>'    
         die();
       }

Have any doubt, then comment here!

Get category ID by using category name in wordpress

If you want to get category id by using category name then use the following code

function get_category_id($cat_name){
	$term = get_term_by('name', $cat_name, 'category');
	return $term->term_id;
}

then Just call the function with your category name as a parameter. for example

$category_ID = get_category_id('Books');

Now you can get the category Id in variable $category_ID.

Have any doubt, then comment here!

Get all user meta data by user ID in wordpress

If you want to get all user meta data by using id then use get_user_meta() function.

For example user id is 25. Use the following code for get all user meta data.

<?php
  $all_meta_for_user = get_user_meta( 25 );
  print_r( $all_meta_for_user );
?>

Results:

Array ( 
[first_name] => Array ( [0] => Tom ) 
[last_name] => Array ( [0] => Auger) 
[nickname] => Array ( [0] => tomauger ) 
[description] => etc.... )

Have any doubt, then comment here!

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!