A TEXT POST

Frontend AJAX in WordPress

While working on WPOID, I realized there are time where we use shortcodes in our plugin to display forms and we want to submit the form values using ajax. But since ajax functionality is by default only available for wordpress dashboard ie. the availability of ajaxurl (admin-ajax.php) only when someone is logged in makes it really difficult. So here is a piece of code which you can put in any file ex. ‘ajax.php’ and then put the file ‘ajax.php’ in your plugin’s directory and then use it as ajax url.

A TEXT POST

Getting 403 error on HostGator when passing some parameters to the URL?

Recently I was working on a URL shortner plugin for WordPress. The Plugin worked perfectly on our local machines, DreamHost and ServerGrove default installation too. There was a strange problem with HostGator, it was throwing error when ever the URL to shorten parameter had an URL with “.com” extension. After a lot of research and coordination with HostGator Live Chat, we discovered that it was due to mod_security rules. The error log which I got was:

[Mon Jan xx xx:xx:xx xxxx] [error] [client xxx.xxx.xxx.xxxx] ModSecurity: Access denied with code 403 (phase 2). Match of "rx (/path-of-file)|(www/delivery/ajs.php)|(path-of-another-file)|(/wp-content/(themes|uploads)(?:/(\\\\w+))*/(tim)?thumb.php|/pl/download\\\\?file=http|/index\\\\.php/admin/system_config/save/section/payment/|^/b/ss/mxmacromedi ..." against "REQUEST_FILENAME" required. [file "/opt/mod_security/hg_rules.conf"] [line "69"] [id "xxxxxxx"] [msg "JITP:xxxx"] [hostname "xxxx.com"] [uri "xxxxxxx"] [unique_id "xxxxxxx"]
Which definitely is a mod_security rule. So just tell them about the same issue(otherwise they will take long time to figure out). And then you can ask them for a whitelisting.

A TEXT POST

Getting list of categories available in all wordpress NETWORK sites

I was recently writing a wordpress network site plugin which required me to fetch list of categories in all network sites. After reading the documentation I figured out the following possible way: if we can get all the blog id’s in an array $blog_id_list , then we can do:

foreach($blog_id_list as $blog_id)
{
	switch_to_blog($blog_id);
	get_categories(); //use it as required.
}
restore_current_blog();

A TEXT POST

Facebook connect localhost

Building an application that uses Facebook Connect for login? Want you test the login locally? Here is how you will be able to use facebook connect locally:

1. Go to your application page on facebook. http://www.facebook.com/developers/editapp.php?app_id=xxxxxx
2. Select “Web Site” tab from the sidebar.
3. Add local.domain-name.tld to “Site Domain” field.
4. “Save Changes” on facebook.

The next steps depends on your OS:
If you are on unix/linux type system:
5. Open the file /etc/hosts, in your favorite text editor. (You will need root access to modify the file).
6. Add the “127.0.0.1 local.domain-name.tld” at the end of the file. For example. “127.0.0.1 local.magazinify.com”

If your are on Windows:
5. The file to edit will be:
(Considering your OS is installed in C drive)
Windows 95/98/Me c:\windows\hosts
Windows NT/2000/XP Pro c:\winnt\system32\drivers\etc\hosts
Windows XP Home/Vista/7 c:\windows\system32\drivers\etc\hosts
6. Open the file with Administrative rights in your text editor.
7. Add the “127.0.0.1 local.domain-name.tld” at the end of the file. For example. “127.0.0.1 local.magazinify.com”
8. Go to command prompt(press win-key+r then type cmd and press enter)
9. Type the command “ipconfig /flushdns”

Incase you get a SSL error, something like “Fatal error: Uncaught CurlException: 60: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed thrown in ……/facebook.php on line 512”
Add the following in your curl options:
CURLOPT_SSL_VERIFYPEER => false and
CURLOPT_SSL_VERIFYHOST => 2

Now you can use the Facebook Connect login on your local machine, just access your project from http://local.domain-name.tld/path-to-project instead of http://localhost/path-to-project from your local machine.

A TEXT POST

PHPCamp 2010 - What/When/Why/Where?

What is PHPCamp?
 
PHPCamp is a ‘ad-hoc gathering’ for PHP community. It similar to barcamp, but more focused towards PHP based web application development.
It is targeting all the PHP enthusiast and starters to join this technical extravaganza this means that anyone can come to PHPCamp and participate.
  
Why attend PHPCamp?
 
Navin has the best answer: “Because learning from humans is much better than Google
  
But I am a student and I have classes…
 
Here is why you should bunk classes to attend PHPCamp / BarcampPune: projects, internships, recosBarcamp?
  
What to expect at PHPCamp?
 
Knowledge, Knowledge and Knowledge. Oh! did I miss Free T-Shirt, Adobe® Flex™ Builder 3 Pro (Licensed Copy) along with “Getting started resources” ( CD also includes lot of “Raffles” ) and of course you could be an “Expert” in 8 hours.
  
When and Where?
 
Venue: SCDL(Symbiosis Center for Distance Learning), Pune
Time: 9AM - 5PM. ( Please reach 15 mins prior )
Landmark: 3-5 mins walk from OM Super Market(Groceries Shop) / SICSR College
  
Register ASAP: http://phpcamp.eventbrite.com/ The number of allowed participants are limited, we have already increased the number twice.
  
Event Website: http://phpcamp.org
  

A TEXT POST

Password protect your directory without .htaccess

.htaccess is generally used for basic authentication of a folder/website. The data of users logging in using .htaccess is not stored or kept. It works untill the user’s browser remembers the credentials.  And it would really be very difficult to keep track of users logging in using this method. So, here is a very simple way of doing the same using HTTP authentication: <?php $myusername = “amanjain.com”; $mypassword = “amanjain”; /*The authentication technique can be different like using database or a key value pair file*/ if ($_SERVER[“PHP_AUTH_USER”] == ”” || $_SERVER[“PHP_AUTH_PW”] == ”” ||$_SERVER[“PHP_AUTH_USER”] != $myusername || $_SERVER[“PHP_AUTH_PW”] != $mypassword) { header(“HTTP/1.0 401 Unauthorized”); header(“WWW-Authenticate: Basic realm="This is my protected area"”); /*Replace  ”This is my protected area” with your personal message*/ echo “<h1>Authorization Required For Accessing Content.</h1>”;/*If user cancels the authentication*/ die(); } ?> That’s it. Simple right?

Check out PHPCamp.net a article sharing website relevant to our own PHP community
A TEXT POST

Taking screenshot of a website using PHP on windows os

I have been trying to figure out a way using I could take a screenshot of any provided URL using PHP. Finally I could find out a way of doing it and that is by using the COM object on windows os. I have written a class for taking the screenshot. For using it, all you need to do is:

Download the class file from here. Rename it and remove the .txt entention Include the class file in the required script. And then, Create a new object:

$s = new akjScreenshot(“http://amanjain.com”);

Resize if required:

$s->toWidth(100); $s->toHeight(100);

Save:

$s->save(“screen.jpg”);

You can also save it in different formats like:

$s->save(“screen.png”, “png”); $s->save(“screen.gif”, “gif”); I hope it helps :) Please leave a comment if you find it useful.

Check out PHPCamp.net a article sharing website relevant to our own PHP community

A TEXT POST

Making mail() function of PHP work under windows os

mail() is a useful function in PHP for sending mails, but many developers face problems in making mail() work properly on Windows.
In the section [mail function] in the php.ini file, you’ll find three settings: SMTP, sendmail_from, and sendmail_path.
If your server runs on a Windows machine, you’ll have to set the SMTP option to point to your SMTP server for sending mail using PHP.
So, we must understand that PHP must have the address of some SMTP server where it would deliver the mail to be sent and that is all PHP can actually do.
So, we need  to set SMTP to the SMTP server and the sendmail_from option to the required email address. Now if we want to use localhost as the SMTP server, we must have some SMTP server running on our system, simple isn’t it?  So we can download and install hMailServer from www.hmailserver.com/ for serving our purpose.
After installing the mail server make sure that you switch off  SMTP authentication for connections coming from localhost / 127.0.0.1 in hMailServer for your mail() function to work.
And this is how [mail function] part of your php.ini should look:
[mail function] ; Setup for Windows systems SMTP = localhost sendmail_from = me@mydomain.tld

A TEXT POST

About Me

Hi I am Aman Kumar Jain, a Pune based web developer. I like to experiment with anything and everything that comes my way. I love travelling, photography and swimming. I can be seen with my Laptop most of the time. Friends call me GEEK and I believe Geek is a lifestyle.

A TEXT POST

gnUnify 2009 - Ajax with PHP

I gave a talk on ajax at gnunify’09. This is the slide I used to give the talk.

View more presentations from jainaman.