Tuesday, 25 December 2012

Hiding unnecessary menus in wordpress admin


In this post, I describe a technique for hiding unnecessary menus. 



Some time we need to hind unnecessary menus for other clients role just like (administrator, contributor, subscriber, author, editor, etc) then with the help of this function we can hide wordpress menu.
1.    
1   This code include your themes>>functions.php files.

// remove unnecessary menus
function remove_admin_menus () {
                global $menu;
                                $restrict = explode(',', 'contributor');// you can pass user role (administrator, contributor, subscriber, author, editor, etc)
                $restrict_user = explode(',', 'Posts,Comments,Media,Profile,Appearance,Plugins,Users, Tools,Settings');// you can pass menus name whose you want to hide.
               
                $f = create_function('$v,$i', 'return __($v);');
                array_walk($restrict, $f);
                if (!current_user_can('activate_plugins')) {
                                array_walk($restrict_user, $f);
                                $restrict = array_merge($restrict, $restrict_user);
                }
                // remove menus
                end($menu);
                while (prev($menu)) {
                                $k = key($menu);
                                $v = explode(' ', $menu[$k][0]);
                                if(in_array(is_null($v[0]) ? '' : $v[0] , $restrict)) unset($menu[$k]);
                }
}
add_action('admin_menu', 'remove_admin_menus');

Thursday, 13 December 2012

How to remove last character of a string(mysql Query fields)?




SELECT Concat( mid( fieldname, 1, length(fieldname) -1 )
 , REPLACE( mid(fieldname, length(fieldname) ) , ',', "" ) )
 as fieldname FROM tablename
 
OR

Select id, REPLACE(fieldname,',','') from tablename 
where fieldname REGEXP '(.*),' limit 5;

update last character from string mysql

UPDATE tablename SET fieldname = Concat( mid(fieldname,
 1, length(fieldname) -1 ) , REPLACE( mid(fieldname, 
length(fieldname) ) , ',', "" ) ) 

Wednesday, 21 November 2012

After logout browser back button shows all visited page in cake php

Some time we logout from application, then we got successfully massage you are logout , but we click back button then we see we reach also last page where we logout.

I solve this problem with given below function.


Add below code in your AppController.php

 
 

public function beforeRender() {
    $this->disableCache();
   
}

Monday, 1 October 2012

Translate date into your language

 i got a requirment wherein i user add date in US(English) format but user want to display spanish language/or Other Language.

First Method :


/* Windows con apache
setlocale(LC_TIME, 'Spanish'); */

/* Apache con linux */
/* setlocale(LC_TIME, 'es_ES'); */

// setlocale(LC_TIME, 'de_DE'); 
// echo strftime('%A, %d. %B %Y');  

/* setlocale(LC_TIME, 'es_MX'); */

setlocale(LC_TIME, 'es_ES'); //pase language code 
echo $fecha=strftime('%B %d %Y',strtotime('01-09-2012'));// Output septiembre 01 2012

Second Method 

function converttospanish($paramdate)
{
$str=$paramdate;
$month = array("","enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiempre", "diciembre");// you can change different language 
if (($timestamp = strtotime($str)) !== false)
{
$php_date = getdate($timestamp);
$dm = date_parse_from_format("Y-m-d", $paramdate);
echo $php_date["mday"].' '.$month[$dm["month"]].' '.$php_date["year"];
}
else
{
echo 'invalid timestamp!';
}
}


Tuesday, 25 September 2012

Add and remove textbox dynamically with jQuery.

Recently when working on my site i got a requirment wherein i user can add multiple input text on page in different textboxes, and these textboxes needs to be created dynamically as per user need. So i decided to do it in jQuery. it’s quite easy to add or remove a textbox dynamically. The idea is quite simple, just combine the use of ‘counter‘ variable, jQuery createElement(), html() and remove() method. See below example :


<html>
<head>
<title>Add dynamic text box</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<style type="text/css">
div{
padding:8px;
}
</style>
</head>
<body>
<h1>add dynamic text box</h1>
<script type="text/javascript">
 $(document).ready(function(){
     var counter = 2;
     $("#addButton").click(function () { //add dynamic text box
  if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
}   
  var newTextBoxDiv = $(document.createElement('div'))
    .attr("id", 'TextBoxDiv' + counter);
  newTextBoxDiv.after().html('<label>Textbox #'+counter+':</label>'+'<input type="text" name="textbox'+ counter+'" id="textbox' + counter + '" value="" >');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
     });

     $("#removeButton").click(function () { //remove dynamic text box
if(counter==1){
 alert("No more textbox to remove");
 return false;
   }   
counter--;
        $("#TextBoxDiv" + counter).remove();
     });

     $("#getButtonValue").click(function () {

var data = '';
for(i=1; i<counter; i++){
     data += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
     alert(data);
 //save dynamic data in database;
/* for(i=1; i<counter; i++) { 
        $.post(
            'process.php',// Data will be passed to this php file. 
            { 
                value: $('#textbox'+i).val() // get data values
            }
            );
  }*/
     });
  });
</script>
</head>


<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='textbox' id='textbox1' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>

</body>
</html>

Tuesday, 18 September 2012

Cropping an Image with PHP and the GD Library

Learn how to use the PHP GD library in your PHP program to generate dynamic images. Learn how to create and manipulate the GIF, PNG and JPG images.



<?php
# Note: Install GD laibreries 
$filename = '194_card.jpg';//orignal file 
$filename1 = '1.png';//save file name whoes you save 

list($current_width, $current_height) = getimagesize($filename);
$left = 0; //crop left margin
$top = 0;//crop right margin
$crop_width = 1056; 
$crop_height = 400;
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, $filename1, 100);
?>

Sunday, 16 September 2012

Saving An Image Retrieved Via cURL


How can save images without use of browser with the help of Curl.
Some time we need save url image help of code without open browser
This function save image without open link.
$url="http://google.com/imagename.jpg";                                                                          
$ch = curl_init($url);
$fp = fopen(‘folderpath/card1.jpg', 'wb');// folder path where you save image
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Note : One of the main reasons to use cURL to save or display a remote image is that some servers/hosts have allow_url_fopen turned off. This means functions like fopen can’t open files held on external servers.
I hope you have enjoyed this tutorial