Monday, October 28, 2013

Some Important and Useful Wordpress Functions

Hiiiii, We all know that using wordpress is easy but sometimes it is tricky to use if you are not completely familiar with functionality of wordpress. Sometimes you have to add some custom functionality in your wordpress in the same manner as wordpress works rather than just writing core php codes,  but to do so you face many difficulties (like hoe to get current page/post id, how to fetch data from database tables, getting post meta data from  database tables, showing thumbnails images , getting uploads directory path etc ). So, in this post I'll be telling you many important functions of Wordpress for developing purpose :

wordpress functions


Wordpress Functions :


1. get_the_ID() :

It returns the ID of current page/post.
Usage :  $post_id = get_the_ID() ;

2. get_post($post_id) :

It returns all the data from wp_posts table for the post_id you have sent as a parameter int this function. This function the data in Std Object form.
Usage : $post_details = get_post($post_id); 
To access data in this object, you have to write $post_details->post_title;

3. get_post_meta($post_id) :

It returns all the post meta data from wp_postmeta table in the array form for the post_id you have sent as a parameter in this function.
Usage : $post_meta = get_post_meta($post_id); 
To access data in this array, you have to write $post_meta['your_meta_details'];

4. have_posts() :

It checks if there are posts in post table or not or the query to fetch posts from posts table returned some result or not.
Usage : It is used in loops - while ( have_posts() ) {}

5. get_template_directory_uri() :

It returns the path to currently working theme directory.
Usage : $theme_ path=get_template_directory_uri(); 
If you are in Twenty Twelve theme of your wordpress, then this function will return - " http://localhoat/wp-content/themes/twentytwelve"

6. plugins_url() :

It returns path to your wordpress plugin directory.
Usage : $plugins_url = plugins_url();
If you want to access Plugin for eg. akismet ,then you have to access it like - $plugins_url."/akismet";

7. wp_upload_dir();

It returns a compelete array of path and urls to your upload directory like :
Array ( 
[path] => C:\path\to\wordpress\wp-content\uploads\2013\10 
[url] => http://example.com/wp-content/uploads/2013/10 
[subdir] => /2013/10 
[basedir] => C:\path\to\wordpress\wp-content\uploads 
[baseurl] => http://example.com/wp-content/uploads 
[error] => 
Usage : $uploads = wp_upload_dir();
You can access the data in this array by this way - $path= $uploads['path'];

8. get_the_post_thumbnail( $post_id, $size, $attr ) :

It returns the featured image if there is any in the post and to view image , you just have to echo the result fetched bye this function.In this function on first parameter i.e. post_id is optional but I find it compulsory and other 2 are optional.
$size is the size of image at which you wanna display.
$attr is the additional css attributes which u wanna add to that image
Usage : <?php echo get_the_post_thumbnail( $post_id,array(100,100) ,array('class' => 'alignleft')); ?> 
Here 100, 100 in second parameter is the size of the displaying image in pixels.


Hope you like this post……..Please Comment…………!!!!!!!!!


Tags:

0 Responses to “Some Important and Useful Wordpress Functions”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks