Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Saturday, April 12, 2014

How to add menu in Wordpress Admin without Plugin

Here we are talking about Adding a menu item in wordpress admin panel to add manual functionality for your themes and plugins etc. Let's do it You need a Wordpress Function called add_action which get 2 parameter. 
add_action Parameters Explanations:
  1.  First parameter is what do you want to add so we write admin menu.
  2. Second parameter is that we will tell wordpress add action that my other explanation will be in signal_adder function
what will be in signal_adder function

We write function signal_adder(){

==>now our add_action will complete after this function  

we are telling wordpress in next line add_posts_page it means our menu item will be in posts section just copy the code and paste in theme functions.php file and you will see there is a menu called Add Signal.

add_posts_page parameter Explanations
  1. First parameter will be the Menu Title 
  2. Second parameter will be the Page Title 
  3. Third parameter will be manage our page options (Fixed just write as it is)
  4. fourth parameter will be your choice because in our admin panel url will be yourhost.com/wp-admin/edit.php?page=signali 
  5. Fifth parameter will be a function in which our other things lie like what will be the page matter or texts etc. 


add_action('admin_menu','signal_adder');

function signal_adder(){
    add_posts_page('Add Signal','Add Signal','manage_options','signali','signal_adder_text');
}
function signal_adder_text(){
    echo "Hurrah i make a custom admin menu";
}

Beginners always feel very ambiguous while making admin menu just write i will comment and solve your problem as already i am beginner not an expert but the problems i tackle i write here. Just follow the path and you will be lead by me. Hope you will like this tutorial then you going to share and comment your review thanks for reading.

How to get Default Timezone in PHP

Here i will talk about timezone which you can't get in PHP there is no built in function to get that.... But here i am with a new technique to get timezone in PHP using Javascript. Let's do it.

First of all Download a small js file from top of this website. Put this file in js folder and replace the name here bold in code


function timeStamp(){
    echo '<script type="text/javascript" src="'js/jstie.min.js" /></script>';
    echo '<script type="text/javascript">';
    echo "var timezone = jstz.determine();";

    $timezoni = "document.write(timezone.name());";
    echo "</script>"; 

    timeStamp();
    date_default_timezone_set($timezoni);

That's all now your timezone will be save in variable $timezoni nothing difficult just call the function timeStamp();  you will get a variable timezoni and put that in date_default_timezone_set() this is the PHP function to set a specific timezone but we do it dynamically.

Hope you will like this tutorial and you going to comment and share this with your friend.

Friday, April 11, 2014

How to Show Timestamp like Seconds and Minutes Ago

Here i am talking about the timestamp which looks like Twitter or Facebook timestamp , 2 Days ago or 4 Days ago or 2 Minutes ago. Same like here is the simple tutorial which will show you how to implement same like this. Once you need to call this function timeAgo($timestamp);and you will just see this stamp one more thing change your timezone according to your taste or server as you wish. Also for the simple way just make your date like "2014-04-11 11:10:20" and take this in $timestamp variable and just call your function you will see these type of timestamp.

function timeAgo($timestamp){
date_default_timezone_set("Asia/Karachi");
    $datetime1=new DateTime("now");
    $datetime2=date_create($timestamp);
    $diff=date_diff($datetime1, $datetime2);
    $timemsg='';
    if($diff->y > 0){
        $timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');

    }
    else if($diff->m > 0){
     $timemsg = $diff->m . ' month'. ($diff->m > 1?"'s":'');
    }
    else if($diff->d > 0){
     $timemsg = $diff->d .' day'. ($diff->d > 1?"'s":'');
    }
    else if($diff->h > 0){
     $timemsg = $diff->h .' hour'.($diff->h > 1 ? "'s":'');
    }
    else if($diff->i > 0){
     $timemsg = $diff->i .' minute'. ($diff->i > 1?"'s":'');
    }
    else if($diff->s > 0){
     $timemsg = $diff->s .' second'. ($diff->s > 1?"'s":'');
    }

$timemsg = $timemsg.' ago';
echo $timemsg;
}

If you love this tutorials please share it or comment below.

Friday, July 12, 2013

PHP Reference Sheet - Quick Reference

Data Types
integer, float, boolean, string, array, object, resource, NULL
Variable Declarations
$variablename = <value>;
$anothervariable =& $variablename; (Assign by Reference)
Declare Array
$arrayname = array();
Initialize Array
$arrayname = array(<value1>, <value2>, <value3>);
$arrayname = array(<key> => <value>, <key> => <value>); (Define Keys)
$multiarray = array(<key> => array(<value1>,<value2>)); (Multi-dimensional)
Common Array Functions
sort(<array>); (Sort array assigns new keys)
asort(<array>); (Sort array maintain keys)
rsort(<array>); (Sort array in reverse, new keys)
arsort(<array>); (Sort array in reverse, maintain keys)
count(<array>); (Count elements)
count(<array>,COUNT_RECURSIVE); (Count multidimensional array)
array_push(<array>,<value>); (Push item onto end of array)
array_pop(<array>); (Pop item off end of array)
Comments
// Comment text
/* Multi-line comment text */
# Comment text
Arithmetic Operators
+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus)
Relational Operators
== (Equal), === (Equal with type comparison), != (Not equal), <> (Not equal), !== (Not Equal
with type comparison), < (Less than), > (Greater than), <= (Less than or equal to), >=
(Greater than or equal to)
Logical Operators
! (logical NOT), && (logical AND), || (logical OR), xor (logical XOR)
Assignment Operators
= (Assign), += (Addition), -= (Subtraction), *= (Multiplication), /= (Division), .=
(Concatenation), %= (Modulus), &= (And), |= (Or), ^= (Exclusive Or), <<= (Left Shift), >>=
(Right Shift)
String Concatenation
. (Period)
String Manipulation
substr(<string>,<start>,[<length>]);
strlen(<string>);
trim(<string>);
ltrim(<string>); // Trim left
rtrim(<string>); // Trim right
strtolower(<string>);
strtoupper(<string>);
str_replace(<search>,<replace>,<string>,[<count>]);
strpos(<string>, <search>);
strcmp(<string1>,<string2>); (Binary safe string comparison)
strcasecmp(<string1>,<string2>); (Binary safe case-insensitive comparison)
explode(<delim>,<string>,[<limit>]); (Break string into array)
implode(<delim>,<array>); (Join array into string separated by delim)
Cookies
setcookie (<cookiename>, [<value>],[<expire_time_in_secs_since_epoch>]);
$_COOKIE['cookiename']; (Returns value of cookie)
Sessions
session_start(); (Create session)
$_SESSION['key_name'] = value; (Set session variable)
$variablename = $_SESSION['key_name']; (Retrieve value from session variable)
session_destroy(); (Destroy session)
Error Handling
try {
<statements that may cause error>;
}
catch (<Exception Class> $exception_name)
{
<statements to execute when error is caught>;
}
Super Globals
$GLOBALS (Access all global variables in script)
$_SERVER (Access web server variables)
$_GET (Values passed to script through URL)
$_POST (Values passed to script through HTTP Post)
$_COOKIE (Values passed by user cookie)
$_FILES (Values passed by HTTP Post File Uploads)
$_ENV (Values passed to script via the environment)
$_REQUEST (Values passed by URL, HTTP Post, or user Cookies)
$_SESSION (Values passed through user's session)
If Else
if (<condition 1>)
{ <statement 1>; }
elseif (<condition 2>)
{ <statement 2>; }
else
{ <statement 3>; }
Inline If (Ternary)
<condition> ? true : false;
For Loop
for (<initialize>;<condition>;<update>)
{
<statements>;
}
For Each Loop
foreach (<array> as [<value> |<key> => <value>])
{
<statements>;
[break];
[continue];
}
While Loop
while (<condition>)
{
<statements>;
}
Do-While Loop
do
{
<statements>;
} while (<condition>);
Switch
switch (<expression>)
{
case <literal or type>:
<statements>;
[break;]
case <literal or type>:
<statements>;
[break;]
default:
<statements>;
}
Function Structure
function <function_name>([<parameters>])
{
<statements>;
[return <value>;]
}
Class Structure
class <class_name> [<extends base_class>]
{
[var | <modifiers*>] [<class member variables>];
[<modifiers*>] function <function_name>([<parameters>])
{
<statements>;
}
}
* Modifiers <public | pr ivate | static> are implemented in PHP5
Declare and Use Class
$variable = new class_name();
$variable->function_name();
class_name::function_name(); (Static call)