Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, 25 July 2014

show featured image to the beginning of each post if added any or show default post image

<?php



function easy_the_content_filter( $content ) {


        // Add image to the beginning of each post. if any featured image is added by  author than show the  featured  image otherwise  show  the  common image for all post  if no featured image is set by author.
       if ( is_singular('post') && has_post_thumbnail()) {
        $thumbnail = get_the_post_thumbnail();

        $content = $thumbnail . $content;
       
        }
        else
        {
            $content = sprintf('<img class="post-icon" src="%s/images/php.jpg" alt="Post icon" title="" width="100"/>%s',get_bloginfo( 'stylesheet_directory' ),$content );
        }

    // Returns the content.
    return $content;
}


?>

Add image to the beginning of each post in wordpress

<?php



function easy_the_content_filter( $content ) {


        // Add image to the beginning of each page

            $content = sprintf('<img class="post-icon" src="%s/images/php.jpg" alt="Post image" title="" width="100"/>%s',get_bloginfo( 'stylesheet_directory' ),$content );
     

    // Returns the content.
    return $content;
}


?>

Add Word Limit to Wordpress Posts

<?php


//Add Word Limit to Posts

add_filter("the_content", "easy_custom_filter");

  function easy_custom_filter($content)
  {

    return substr($content, 0, 100);
   // return a subset of it
  }


?>

Show category images on single product page

<?php
//Show category images on single product page

$terms = get_the_terms( $post->ID, 'product_cat' );// get the post categories ids

  foreach ( $terms as $term ) // traverse all post category deails
{
        $category_name = $term->name;
        $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
        $image = wp_get_attachment_url($category_thumbnail);
        echo '<img src="'.$image.'">';
    }
   

?>

How to enable menu support in wordpress

<?php
//How to enable menu support in WordPress theme

function easy_enable_menu()
{
    add_theme_support( 'menus' );   
}

add_action( 'init', 'easy_enable_menu', 10 );

?>

How to remove bulk edit options

<?php


    //How to remove bulk edit options

    add_filter( 'bulk_actions-' . 'edit-post', '__return_empty_array' );
?>

Thursday, 17 July 2014

how to create a custom post type in wordpress

<?php

/// put this code into your theme functions.php file 
add_action('init', 'easy_register_post_type');  //  hook  our function with Wordpress  process/stage

function easy_register_post_type() {

register_post_type(
    'products', array(
        'labels' => array(
            'name' => 'Products',
            'singular_name' => 'Product',
            'add_new' => 'Add new product',
            'edit_item' => 'Edit product',
            'new_item' => 'New product',
            'view_item' => 'View product',
            'search_items' => 'Search product',
            'not_found' => 'No products found',
            'not_found_in_trash' => 'No products found in Trash'
        ),
        'public' => true,
        'supports' => array('title',
           'title',
            'editor',
            'excerpt',
            'custom-fields',
            'revisions',
            'thumbnail'
        ),
        'has_archive' => true,
        'taxonomies' => array()
    )
);
}

?>

how to set new post thumbnail size in wordpress

<?php
//how to  set new post thumbnail size in wordpress

// set any size with your requirement  for the  thumbnail image

// add this code into your functions.php file

set_post_thumbnail_size( 210, 210, true );

?>

How to display custom post meta in wordpress

<?php
// To get the price of a random product we will need to know the ID
   $product_id=101;
    $price = get_post_meta( $product_id, 'product_price', true );

echo $price

?>   

Friday, 16 May 2014

How to upload & Save Files with new name




<?php

 /* 
   //files array details
    $_FILES['easyfile']['name'] - the actual name of the uploaded file.

    $_FILES['easyfile']['size'] - the size in bytes of the uploaded file.

    $_FILES['easyfile']['tmp_name']- the uploaded file in the temporary directory on the web server.
   
    $_FILES['easyfile']['type'] - the MIME type of the uploaded file. file type like image,documnet,excel file

    $_FILES['easyfile']['error'] - the error code associated with this file upload.

*/
$msg="";
$upload_limit=1024000;
$valid_file=false;
echo $dirpath = dirname(__FILE__);
if (isset($_POST['submit']))
{

if($_FILES['easyfile']['name'])
{
    //if no errors...
    if(!$_FILES['easyfile']['error'])
    {
   
    //here is file destils
   
    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
   
    if($_FILES['easyfile']['size'] < $upload_limit ) //can't be larger than  upload limit
        {
            $valid_file = true;
           
        }else
        {

            $msg = 'Your file size is to big.';
        }
       
        //if the file has passed the test
        if($valid_file)
        {
            echo $file_new_name =  time()."_".strtolower($_FILES['easyfile']['name']); //rename your file

            //move/copy it to where we want it to be

            move_uploaded_file($_FILES['easyfile']['tmp_name'], $dirpath.'/uploads/'.$file_new_name);

            $msg = 'Congratulations!  Your file was upload successfully!.';
        }
   
   
    }
}

}// if submit button is hit

if ($msg!="")
echo $msg;
?>


<form action="" method="post" enctype="multipart/form-data">
    Your file: <input type="file" name="easyfile" size="40" />
    <input type="submit" name="submit" value="Submit" />
</form>

File Uploading in PHP

<?php

if (isset($_POST['submit']))
{


if($_FILES['easyfile']['name'])
{
    //if no errors...
    if(!$_FILES['easyfile']['error'])
    {
   
    //here is file destils
   
    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
   
   
    }
}

}// if submit button is hit

?>


<form action="" method="post" enctype="multipart/form-data">
    Your file: <input type="file" name="easyfile" size="40" />
    <input type="submit" name="submit" value="Submit" />
</form>

List files in directory using PHP

<?php

//List files in directory using PHP

$dirpath = dirname(__FILE__);// path to directory
if(is_dir($$dirpath))
{
    if($handle = opendir($$dirpath))
    {
        while(($file = readdir($handle)) !== false)
        {
            if($file != "." && $file != ".." && !is_dir($file))
            {
                echo $file.'<br/>';
            }
        }
        closedir($handle);
    }
}

?>

Wednesday, 7 May 2014

Get country information by ip address in php

<?php
function get_easy_visitor_country($ip_address)
{
    if (!$ip_address)
     $ip_address=$_SERVER['REMOTE_ADDR'];
   
     $geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
     $addrDetailsArr =@unserialize(file_get_contents($geopluginURL));
      easy_debug($addrDetailsArr);
     $result=$addrDetailsArr['geoplugin_countryCode'];
    return $result;
}



function easy_debug($obj)
{

echo "<pre>";
     print_r(  $obj);
     echo "</pre>";
 
}


echo "country code ".visitor_country('27.255.252.191');
?>

Get Current Dollar Rate with INR

<?php
function current_rate($price=1)
{
  
        $url  = "http://www.google.com/finance/converter?a=$price&from=USD&to=INR";
       $data = file_get_contents($url);
         preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
            
     $new_price = preg_replace("/[^0-9.]/", "", $converted[1]);
    

return    $new_price;
}




echo "1$==".$current_dolar_rate=current_rate(1) ." INR";
?>

Show all images as array from a folder or directory in php

<?php
$myfull_path="/uploads";
$images=array();

if ($dir = opendir( $myfull_path)) {
    $images = array();
    while (false !== ($file = readdir($dir))) {
        if ($file != "." && $file != ".." )
        {
        $ext=strtolower(getExtension($file));
       
        if ($ext=="jpg" || $ext=="jepg" || $ext=="png" || $ext=="gif" || $ext=="bmp" )   
        {

            $images[] = $file;
        }
       
        }
    }
    closedir($dir);
}



print_r($images);

function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

?>

get all images as array from a folder or directory in php

<?php
$myfull_path="/uploads";
$images=array();

if ($dir = opendir( $myfull_path)) {
    $images = array();
    while (false !== ($file = readdir($dir))) {
        if ($file != "." && $file != ".." )
        {
        $ext=strtolower(getExtension($file));
       
        if ($ext=="jpg" || $ext=="jepg" || $ext=="png" || $ext=="gif" || $ext=="bmp" )   
        {

            $images[] = $file;
        }
       
        }
    }
    closedir($dir);
}



print_r($images);

function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

?>

Get Dollar current rate with INR



<?php
function current_rate($price=1)
{
  
        $url  = "http://www.google.com/finance/converter?a=$price&from=USD&to=INR";
       $data = file_get_contents($url);
         preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
            
     $new_price = preg_replace("/[^0-9.]/", "", $converted[1]);
    

return    $new_price;
}




echo "1$==".$current_dolar_rate=current_rate(1) ." INR";
?>

check a valid file extension and size in php

<?php
    $allowed_ext = array(
             'jpg'    =>    true,
            'gif'    =>    true,
            'png'    =>    true
     );




$allowed_imagesize=1024000;

    function easy_checkvalid_ext($ext,$allowed_ext)
    {
     
     
       if($allowed_ext[$ext])
        {
            return true;
        }
       else
        {
           return false;
       }  
    
       }
   
    function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}   
 
     


      
    function easy_checkfile_size($size,$allowed_imagesize) {
    
       if($size < $allowed_imagesize)
        {    return true;}
       
       else
        {
             return false;
    
       }
    }

       $ext=getExtension($_FILES["files"]["name"]);    
      $validfile = easy_checkvalid_ext($ext,$allowed_image_format);
      $validfilesize = easy_checkfile_size($_FILES["files"]["size"],$allowed_imagesize);

if (!$validfilesize or  !$validfile)
{
echo "not a valid file format or too big size";   
}

?>

check valid Extension in php

<?php
$allowed_ext = array(
             'jpg'    =>    true,
            'gif'    =>    true,
            'png'    =>    true
     );

    function easy_checkvalid_ext($ext,$allowed_ext)
    {
     
     if($allowed_ext[$ext])
        {
            return true;
        }
       else
        {
           return false;
       }  
    
       }

 $validfile = easy_checkvalid_ext('jpg',$allowed_image_format);

if ( !$validfile)
echo "not a valid file type";
?>

Get country code by ip address in php

<?php

function get_easy_visitor_country($ip_address)
{
    if (!$ip_address)
     $ip_address=$_SERVER['REMOTE_ADDR'];
   
     $geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
     $addrDetailsArr =@unserialize(file_get_contents($geopluginURL));
      easy_debug($addrDetailsArr);
     $result=$addrDetailsArr['geoplugin_countryCode'];
    return $result;
}


function easy_debug($obj)
{

echo "<pre>";
     print_r(  $obj);
     echo "</pre>";
 
}

?>
Freelance Jobs