Simple pagination system in your WordPress plugins

May be you are developing a plugin or a theme and it has something to do with handing some pagination of your custom tables data. Here’s how we can simply manage it, I am not going to build all the admin panel stuffs, but just showing you the process.

Step 1:

Lets get the page number from the url query string. If we don’t find anything, we’ll set the page number to 1.

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

Step 2:

Now we need to set per page entry limit and the page offset. We will use the limit and offset to get data from our MySQL query. If you are confused about the offset, may be you’ve seen it on PHPMyAdmin like this: "SELECT * FROM `table_name` LIMIT 0, 10". Here, we are getting the first 10 entries from our database. If we want get the next 10 entries, our limit will be "LIMIT 10, 10". So the first digit for the limit is the offset. It tells us from where we’ll get our next entries.

$limit = 10;
$offset = ( $pagenum - 1 ) * $limit;

So, by this two lines we are setting the limit and our offset dynamically. Read the rest of this entry »

Tags:

Add your custom columns to WordPress admin panel tables

What? What did you said on the title? Well, I am not sure this is an appropriate title. But I am going to explain it to you.

Lets say, you are making a theme or a WordPress plugin and you want to show something in the admin panel like this:Custom Post Column

We will see how can you add those columns to the admin panel tables and manipulate data on the table rows. Well, this is very easy to do, because WP has a really nice API to work with.

Now we will add those two columns to our admin panel’s post listing table. Here is the code:

<?php
function test_modify_post_table( $column ) {
    $column['test_budget'] = 'Budget';
    $column['test_expires'] = 'Expires';

    return $column;
}
add_filter( 'manage_posts_columns', 'test_modify_post_table' );

Read the rest of this entry »

Tags:

Working with IDE One API

I am very lazy to write any post. To be frank, I don’t find anything to write :roll:

Today I found something to write about. It’s nothing, but some bunch of codes. I’ll describe them, they are self explanatory. The idea is to write code and compile them On-The-Cloud. It’s actually using “IDE One” api to compile any programming source code, and give the result back. It’s useful in the devices where you can’t use any compiler, like mobile phones. There are already many applications for mobile that works the same way. Today I just wrote it in PHP. It uses SOAP protocol to communicate to the server. Enough talks :P

<?php

$user = 'tareq'; //--> API username
$pass = '********'; //--> API password

$lang = 1; //--> Source Language Code; Here is 1 => C++

$code = '#include<stdio.h>
int main() {
	printf("hello");
	return 0;
}
'; //--> Source Code

$input = '';
$run = true;
$private = false;

//create new SoapClient
$client = new SoapClient( "http://ideone.com/api/1/service.wsdl" );

//create new submission
$result = $client->createSubmission( $user, $pass, $code, $lang, $input, $run, $private );

//if submission is OK, get the status
if ( $result['error'] == 'OK' ) {

    $status = $client->getSubmissionStatus( $user, $pass, $result['link'] );

    if ( $status['error'] == 'OK' ) {

        //check if the status is 0, otherwise getSubmissionStatus again
        while ( $status['status'] != 0 ) {
            sleep( 3 ); //sleep 3 seconds
            $status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
        }

        //finally get the submission results
        $details = $client->getSubmissionDetails( $user, $pass, $result['link'], true, true, true, true, true );
        if ( $details['error'] == 'OK' ) {
            var_dump( $details );
        } else {
            //we got some error
            var_dump( $details );
        }
    } else {
        //we got some error
        var_dump( $status );
    }
} else {
    //we got some error
    var_dump( $result );
}

Here is a working demo. It might be buggy, may be you could help me to find those bugs :)

Debugging Zend Framework using firebug

You can use firebug to debug your Zend Framework application. May be you are using print_r(), var_dump() or Zend_Debug::dump($var), they prints the information in your application. But, with the help of FirePHP add-on for Firebug, we will be able to dump our variable on firebug console :D

First install the FirePHP add-on for your Firebug. Then you can use the following snippet anywhere to dump your variables to firebug console -

$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log( $writer );
$logger->log('My Sample Log', Zend_Log::DEBUG);

The second parameter of the Zend Logger is a constant. You can use 0-7 (zero to seven) as a parameter. Here is the details – Read the rest of this entry »

New plugin: WordPress User Frontend

Today I am coming with a new wordpress plugin, named “WordPress User Frontend“. Some of us want something like that the subscriber/contributor will not be able to go in the wordpress backend and everything these user can control will be done from wordpress frontend.

Features:

So here is my plugin that solves your problem. This features of this plugin in it’s version 0.2 are follows:

  • User can create a new post and edit from frontend
  • They can view their page in the custom dashboard
  • Users can edit their profile
  • Administrator can restrict any user level to access the wordpress backend (/wp-admin)
  • New posts status, submitted by users are configurable via admin panel. i.e. Published, Draft, Pending
  • Admin can configure to receive notification mail when the users creates a new post.
  • Configurable options if the user can edit or delete their posts.
  • Users can upload attachments from the frontend
  • Admins can manage their users from frontend
  • Pay per post or subscription on posting is possible

Read the rest of this entry »

Simple server to server file downloader script

About a month ago I wrote a script for my own need. It can download files from another server to your server. I just wrote it for my own purpose and it’s pretty simple and stupid enough. But hey, it works ;)

<?php
if (isset($_POST['sub']))
{
    if (filter_var($url = $_POST['url'], FILTER_VALIDATE_URL))
    {
        define('UPLOAD_DIR', dirname(__FILE__) . '/files/');
        $length = 5120;

        $handle = fopen($url, 'rb');
        $filename = UPLOAD_DIR . substr(strrchr($url, '/'), 1);
        $write = fopen($filename, 'w');

        while (!feof($handle))
        {
            $buffer = fread($handle, $length);
            fwrite($write, $buffer);
        }

        fclose($handle);
        fclose($write);
        echo "<h1>File download complete</h1>";
    }
    else
    {
        echo "<h2>Invalid url</h2>";
    }
}
?>
<form action="" method="POST">
    <p>
        <label for="url">Url:</label>
        <input type="text" size="50" name="url" id="url" />
    </p>
    <p>
        <input type="submit" name="sub" value="download" />
    </p>
</form>

This script has a form input field to make the operation simpler. The downloaded files will be saved on a directory named /files. I am just sharing it, may be it will help you in anyways :)

Locate phone location with GP Aloshbei location API and visualize with google maps API

We have built our main Aloashbei class in the previous post. Now we will extend it to locate any GrameenPhone number. But currently, as a developer you can only track your number for testing purpose.

File Structure:

  • class.Aloashbei.php
  • class.AloashbeiLBS.php
  • location.php
  • includes/WebService_Aloashbei_LBS_WS.wsdl

Read the rest of this entry »

PHP class for sending, receiving and checking SMS status with GrameenPhone’s Aloashbei API

I saw everyone is participating in the GrameenPhone Aloashbei API contest and it’s began and I am late :P . So why not doing something that makes other’s life easy? So I decided to make a PHP class to do that job for you. You don’t need to know how to work with SOAP. Just instantiate a class, set the parameter and send, VOILA!!!

With the Aloashbei SMS API, you can send SMS, Check the SMS status and receive SMS. You can track location of any GP mobile number with the Location Locator API. It will provide you the longitude and latitude of the phone numbers current position. You can send MMS with the MMS API. You can charge any user who buys any content through the Charging API.

So we are going to build php class that will do these thing easily. Here I will built the SMS class. We can send SMS to any number and if it’s successful, it will return us a message ID. With this message ID, we can check the status of that sent message. To make a complete Aloashbei API class, we’ll first make a basic class and will extend it for other purposes. So here is the basic class that we will extend. It’ll hold the values for setting up the variables and will set user/password initially.

The file Structure:

  • class.Aloashbei.php
  • class.AloashbeiSMS.php
  • sms.php
  • includes/WebService_GP_ADP_BizTalk_SMS_Orchestrations.wsdl

Read the rest of this entry »

Translate or convert wordpress date/time/comment number to Bangla digit

Today I worked with a wordpress site Bangladesh Linux Users Alliance, which is a complete Bangla language based site. You can translate with the Bangla language pack, but you can’t convert the English digits to it’s corresponding Bangla digit. So I visited the wordpress codex and found some filters that can do that job very easily.

Here is your function that will convert the digit

/**
 * This function converts all english number's to bangla number
 */
function make_bangla_number($str)
{
	$engNumber = array(1,2,3,4,5,6,7,8,9,0);
	$bangNumber = array('১','২','৩','৪','৫','৬','৭','৮','৯','০');
	$converted = str_replace($engNumber, $bangNumber, $str);

	return $converted;
}

Now we need to add some filters that will hook to this places and convert the digit’s to Bangla.

add_filter( 'get_the_time', 'make_bangla_number' );
add_filter( 'the_date', 'make_bangla_number' );
add_filter( 'get_the_date', 'make_bangla_number' );
add_filter( 'comments_number', 'make_bangla_number' );
add_filter( 'get_comment_date', 'make_bangla_number' );
add_filter( 'get_comment_time', 'make_bangla_number' );

Read the rest of this entry »

Print array elements recursively

Few minutes ago just got a tweet from phpfour (Emran Hasan) that his company is looking for PHP developers. I just visited the announcement page of their site and clicked the apply online button just for testing. There I saw an array written in PHP to solve it. It looked interesting to me and just wrote code for it. Here it is:

<?php
$array = array(
			array(141,151,161),
			2,
			3,
			array(101, 202, 303),
			"php",
			"5"
		);

/**
 * print array elements recursively
 *
 * @param array
 */
function recurse ($array)
{
	//statements
	foreach ($array as $key => $value)
	{
		# code...
		if( is_array( $value ) )
			recurse( $value );
		else
			echo $key . ' => ' . $value . '<br>';
	}
} // End recurse

recurse($array);

/*
output
0 => 141
1 => 151
2 => 161
1 => 2
2 => 3
0 => 101
1 => 202
2 => 303
4 => php
5 => 5
*/

It’s Fun :D

Tags: ,