WordPress Insert Post from Front End

Hi, In this article I will show one example plugin for making post entry from wordpress front end.

Screenshot:

Below is the sample code for the plugin

We can insert post in wordpress using wp_insert_post which is explained Here

In this sample I have created one sample shortcode for displaying plugin on front and in short code function I have added form to display and before that if post is submitted we will insert the post into database.

 Download | Source Code

 

WordPress Admin listing Table and Paging

Almost svery wordpress plugin developer needs to make Admin side listing for any custom table. This table generally store information that is gather from website and/or end users. Listing of table help to view the data in list format. It also allow admin to add, edit and Delete the data. It also allow bulk action for delete.

During my plugin development, I always wonder that how to make admin side listing for custom table which looks same as post and page listing. Also how can I make pagination for the table. I have searched for pagination and found many class to use that you can also found in my previous post. But I want to make it looks like as below:

 

 

After Searching for this I come to know that wordpress also providing the class for listing and paging for custom table. Class Name is Wp_List_Table. Wp_List_Table class is used to create admin screens for the wordpress. We need to extends this class and overload its method to use it.

Now the question is how to use this class and create admin side listing for our created custom table.

WordPress is providing one very good example plugin to see how we can create admin side listing for the custom table. You can find the example plugin Here

This example display code using Array of data. Instead of data we can use any database table. For creating your own table listing copy paste data into your file and follow below steps

Step 1: Create a new file under your plugin directory

Step 2: Include WP_List_Table class file into your file as follow:

Step 3: Create class for your custom table and extends it to WP_List_Table.

For this you can copy whole class from example plugin and paste it in your plugin file and rename the class name from TT_Example_List_Table to something else which is preferable for your plugin.

Step 4: If you are using database table for fetching data which will always a case in real life,  then delete the example data array.

We will fetch the data from database when it will be needed. Remove the below code:

Step 5: Setting up the naming and some configs for table listing.

Step 6: Set Default common value display for the every column using.

This method is called when the parent class can’t find a method  specifically build for a given column. Generally, it’s recommended to include one method for each column you want to render, keeping your package class neat and organized. For example, if the class needs to process a column named ‘title’, it would first see if a method named $this->column_title() exists – if it does, that method will be used. If it doesn’t, this one will be used. Generally, you should try to use custom column methods as much as  possible.

Step 7: If you have any column column which need to be display diffidently from other in the view. We can create this by below syntax

In example below is given

step 8: Display Check boxes for the bulk action.

step 9: get all column. here we specify which column to get for display.

This method dictates the table’s columns and titles. This should return an array where the key is the column slug (and class) and the value  is the column’s title text. If you need a checkbox for bulk actions, refer to the $columns array below.

Step 10: Decide which column to make sortable. This Method is optional.

If you want one or more columns to be sortable (ASC/DESC toggle), you will need to register it here. This should return an array where the key is the column that needs to be sortable, and the value is db column to sort by. Often, the key and value will be the same, but this is not always the case (as the value is a column name from the database, not the list table).
This method merely defines which columns should be sortable and makes them clickable – it does not handle the actual sorting. You still need to detect the ORDERBY and ORDER query string variables within prepare_items() and sort your data accordingly (usually by modifying your query).

Step 11: Set up bulk action for selected check boxes. This method is optional

If you need to include bulk actions in your list table, this is the place to define them. Bulk actions are an associative array in the format ‘slug’=>’Visible Title’

If this method returns an empty value, no bulk action will be rendered. If you specify any bulk actions, the bulk actions box will be rendered with the table automatically on display().

Also note that list tables are not automatically wrapped in <form> elements, so you will need to create those manually in order for bulk actions to function.

Now we also need to set up what to do when any one of the bulk action has been called. This can done using below method. This method is optional.

step 12: This is most important step for the listing the table and fetching the database values.

Here find and delete the code

Below that there is function

Also delete this code this code is now no more needed as we are sorting through database query. Add The below Code here

Here first we have called global variable which is available in wordpress for the database operation. ‘$wpdb’ . Then set order by and order values and get the data.

Every things else remain as it is in this function no need to change.

step 13: Now we need to create function which will be called when page is going to display. We just need to create object of our newly created class.

And Display the data

Conclusion

This will create both listing and pagination for custom table in wordpress. I think this is really easy to use it rather creating table by self and searching for the pagination class and adding them to the code.

This helps developers to create very neat and clean code. Also, This code is fully compatible with wordpress.

Please comment your queries, Suggestion, Like or Dislike post here.

Enjoy..!!!

Note: This example which I have used which is already available in wordpress listing table plugin. I have explained using database connection which was missing in the plugin. Hope this will helps every wordpress developers.

Thank you….

Add Pagination Into WordPress Admin For Plugin

Often in many application, such as in WordPress Plugin, we will need to deal with list of records. As the records grow, more records will need to be displayed. There is a catch though, if you retrieve all the records at one go because it will slow the system down.

In order to have a better user experience for record listing, paging comes into play. It’ll not only help to organize the records by limiting the number of records you want to show per page, it also cuts the down the time of retrieving records. Therefore, pagination plays a very important part in providing a great user experience when interacting with the data.

Imaging if you have a list of 1000 or more records that you want to display. Without pagination, listing all the records will definitely take quite awhile and also increase the load for the database server when executing the query. This is not very efficient, even though retrieving records from database can be cheap.

Now let’s say we want to add in pagination into the 1000 records, by specifying the limit, it will only shows the exact amount of records you want to view. This is a huge benefit for the database server load, because we are not retrieving all the records at once.

Below examples are 2 different type of pagination and a demo on how to add pagination into WordPress Plugin.

Simple PHP Pagination Class

Copy the following code and save as pager.php, this file will be the pagination class. With this pagination classfrom Happycodings, it’d help us add pagination to the list of records easily.

Once you have the pager.php, instantiate the class as below. Change the $limit to your desired number of records to show per page and finally get the page list by echoing it out.

Digg Style Pagination

As the above simple pagination class is enough for us to display a list of page numbering for the records, but what will happen if there are a few hundreds of pages, isn’t the row of page numbering going to be very long too?

Here is another nice Digg Style Pagination class that you can consider if you think there will be a lot of pages for your records.

Download the pagination.class.php.

Go through the guide to customize the style or desired output for the paging you want. Now, to give you a clearer view on how to add this Digg Style Pagination class. Check out below to find out how to add the Digg Style Pagination to a WordPress Plugin.

How To Add Pagination To WordPress Plugin?

If you are building a WordPress plugin that require to display a list of records from database. As records get huge, you will need to have pagination so that you can organize your records well.

First, you will need to download the pagination.class.php and save it in your WordPress Plugin folder.

Next, in the PHP file that you want to list your records, insert the following code. Below are some of the methods we used from the pagination class. For the full list of methods, please refer to Digg Style Pagination Class Help Page.

Note that you will need to change the SQL query, pagination class configuration and table fields, in order to work nicely with your WordPress Plugin.

Now you know How to Add a Digg Style Pagination to a WordPress Plugin, it should make your listing of data much more efficient. Pagination saves your time and bandwidth so you can concentrate more on other implementing issues.

Still not sure about this tutorial on adding pagination? Or simply have an idea to share? Feel free to contact us or leave a comment here!

 

 


Adding New Fields to the attachment of wordpress

Recently I was developing plugin for image features. I have to add some fields to the attachment window of wordpress or I can say in Add media box.

We can achieve this by using two hooks of wordpress.
“attachment_fields_to_edit” and “attachment_fields_to_save”;

attachment_fields_to_edit

  • $form_fields is a special array which will be described in detail in a moment.
  • $post is the attachment as an object (attachments are treated as post objects in WordPress).

attachment_fields_to_save

  • $post is the attachment as an array (attachments are treated as post objects in WordPress).
  • $attachment is the attachment part of the form $_POST which will include the fields setup through the attachment_fields_to_edit hook.

Note: Be careful in your code, as $post is sent to one function as an object and to the other as an array.

We will add this fields information to Custom Fields.

How to add this Fields to Custom Fields for Working is Properly

The new fields being added will be saved as post meta, just like the custom fields section of the post/page edit screen. Fields prefixed with an underscore (_my_custom_field) will not be listed in the drop down of available custom fields on the post/page screen; all other existing post meta fields will be listed. We can use this knowledge to hide the fields we’re adding to the media form, since they aren’t relevant for posts/pages.

There is a similar rule to keep in mind when choosing the $form_fields array key to use for your new field. Here, if you use an underscore ($form_fields[‘_my_custom_field’]) your field will be skipped and will not be added to the form.

So in order to show our fields in the media form, but also not list them in the page/post custom fields drop down, we must combine both methods. This will invlove both the edit and save functions we’ll be creating. For the ‘attachment_fields_to_edit‘ hook we’ll set the $form_fields keys up to not have underscore prefixes, and for the ‘attachment_fields_to_save‘ hook we’ll prefix our fields with an underscore before saving them as post meta. This is a workaround worth doing in order to not muddy our users’ interface with unneeded info.

Hook 1: attachment_fields_to_edit

The $form_fields array has several options for including different types of inputs and custom content. I’ve compiled the various methods below with notes and screenshots of how they render in the form.

Text Input

Hook 2: attachment_fields_to_save

Saving your custom fields is a much simpler process than adding them to the form; just check if your field is set and update its value as post meta. Remeber to prefix your field with an underscore when saving to hide it on the post/page edit screen.

You can also add errors here that will automatically be displayed below your field in the form. The$post['errors'] array gets merged with the $form_fields array before being sent through theattachment_fields_to_edit hook.

Thanks to http://net.tutsplus.com

I have posted here for my future reference.

 

How to make your site listed in major Search Engine

Google

Google is the probably best engine to be listed in. It is one of the most widely searched and they offer tools to help you in designing your page.

There are two things you need to do if you want your page listed in google.

First, submit your site name to the Google add url form. After that, I highly suggest you sign up for their sitemap program. If you need help designing your sitemap to submit, you can use mine and just edit the urls to what you want. The syntax is easy enough to understand, just replace my links with the important pages of your site. Example sitemap is as below:

feel free to copy and paste and edit to your liking. Once you are done, place the sitemap.xml in your home directory and submit to Google. The sitemap program will tell you where your page is ranked, what search results show your page, and if your site has any errors, like broken links.

At this point, create a robots.txt file, and place it in your home directory, if you haven’t already made one.
Example robots.txt as below:

, when creating yours however, the only line you really need is the following:

 

Yahoo! & Lycos

Yahoo! is another great engine to have your page listed in. Submit your url again to Yahoo! here. Yahoo! does not have a sitemap program, but there is something similar you can do. If your website runs PHP, there is a script you can use to convert the sitemap.xml you used for Google into a map that you can use for Yahoo!. I read this article to make the yahoo.txt file that can be used as a yahoo sitemap. To do the same, below is the PHP Script.

Copy and upload it to your server in the same directory as your sitemap.xml. I found this script here. Run the script through your browser, and it will generate a file called yahoo.txt. Example yahoo.txt is as follow:

After you generate this yahoo.txt file, go to the yahoo submission url again, and for the url, put the location of your yahoo.txt and submit. Lycos is included in this because it uses the Yahoo! search engine to find websites.

 

MSN / Bing

MSN was the quickest to add my site to their search engine. Once again, just submit your site to the MSN add url form here. Soon, if you have AWStats setup or a similar website stat list for your page, you will notice the MSN bot has visited your page and hopefully added you to the index.

 

Ask Jeeves

Ask Jeeves the hardest search engine to be listed in. There is no free add url form for this search engine. You do have the option of paying Ask a fee to be included in the results, but I wouldn’t suggest it, since I have gotten this site in Ask for free, it just took a lot longer. Submit your site to the Open Directory Project here. You have to pick a category for your page and provide a good description on why you should be listed to be accepted. If your site does get popular enough to be listed in the other search engines, there is still a good chance the AskJeeves bot will visit your page and include you in the search directory.

 

Tips To Get Listed

Content is the most important thing to get listed. Search engines will not add your page if you have an “Under Construction” or “Coming Soon” notice on every single page.

The more websites that link to your page, the faster you will get listed. Put your website in your signature in online forums that you visit.

Search engines may take up a week to list you, be patient and make sure you have some type of web stats so you can tell if the bots have visited your site yet.

Note: This Post is for my future use only.

Email Crawler Script PHP MySQL

This script is useful for crawling the emails from the website in recursive manner. this is really very easy class to call and starting crawling emails.

Note: I found it somewhere on internet. I put it here for my future reference. It also useful to people who wants this type of script.

emailcrawler.php

database.sql

start.php

 

&npsp;

Streaming File to the Browser

Hi Guys,

I hope you all are well. Today I am going to write post on How can make to user download the file from the another website without showing them that the file is actually coming from another server?

This can help many beginners PHP User to to Hide File URL Location in PHP. Here is the example how can u fetch file from other website and give download to the user like its downloading from our website.

Here is the Nice code:

Very Easy to understand.

Ask if you Have any query.

Thank you.. 🙂

 

URL Rewriting Using htaccess file and PHP Apache

Hello friends,

From long time I was wondering how this URL rewriting is working. I have seen many website with pretty URLs. Also I found the URL Rewriting also helps in Better SEO (Search Engine Optimization).

After Reading bunch of tutorial and forums, I think this is very easy to make just few steps to follow and need knowledge of regular expression.

Steps for Implementing URL rewrite in you website.

  1. Enable mod_rewrite in Apache Server
  2. Create Htaccess file.
  3. Call the URL with New URL Defined.

Enabling mod_rewrite in Apache Server

For enabling mod_rewrite on Apache Server follow these steps:

  1. Find http.conf file in Apache server. Generally this file resides in conf folder of Apache installation PATH. XAMPP server path is C:\xampp\apache\conf on windows. Probably WAMP Path is C:\wamp\apache\conf.
  2. Find the line #LoadModule rewrite_module modules/mod_rewrite.so in the “httpd.conf” file. You can do this easily by searching the keyword “mod_rewrite” from find menu.
  3. Uncomment the line. http.conf file use # to comment the line. So remove the # from begining of the line.
  4. Restart the server. In WAMP you can restart the server by click the icon of WAMP in system tray in right bottom side of the windows. For XAMPP you can open XAMPP control and restart the server.
  5. You can check the mod_rewrite is enabled using phpinfo();

Create HTACCESS file

Lets consider one real world example for creating and understading HTACCESS file and its rules. HTACCESS file uses RewriteRule for define the rule for URL rewriting. This will show you good example for regular expression, rewrite rules & conditions.

For example, create file testhtaccess.php. Write

Create second file testhtaccess.html and Write

This is simple text written in the both the files so that we can differentiate when we rewrite the URL which file is called.

Create Third file .htaccess in same folder of the server where these two files are and Write

The first line turns RewiteEnging On. It will allow the server to rewrite the URL. Second line is the rule for URL rewriting. “^/” says that from starting any type of character and any number of character are allowed. Then there is “?testhtaccess\.html”  \ is for escaping the . in name and there is $ which implies that this is end for rule. This is first Parameter for RewriteRule. Now second parameter tells which files to call when url is matched.

So here when we call testaccess.html file is will be rewite the url and call testhtaccess.php file. This will done internally and URL remains the same.

Lets go now little complex for this. lets say that we have URL and Query String like as below:

we need to rewrite is like

Now we have to tell the apache that whenever this second rewrited url called it should internally call first URL with query string. Now for the ditails.php script to read and parse the query string, we’ll need to use regular expressions to tell mod_rewrite how to match the two URIs. For this you should have knowledge of regular expression. Even though I am going to explain some key factor of regular expression.

. (dot) – Matches any Single Character eg. c.t will match cat, cut, cot

+ (Plus) – repeates the previous match one more time

* (asterik) – repeats the previous match zero or mote times

? (question mark) – Makes the match optional

^ (anchor) – matches beginning of the string

$ (anchor) – matches end of the string

( ) – group several character into single unit

[ ] -a character class – matches one of the characters. it used for range of the character.

 [^ ] – negative character class – matches any character not specified.

Note: ! character can be used before a regular  expression to negate it.

Now for our example we can use

or

for replace Rewrite URL with Actual URL. Using ( ) we can make a variable which can be used on later. This Variable can accessed by

Value incremented automatically on the sequence of occurring of the ( ).

This will replace the Rewrite URL with Actual One.  [a-zA-Z_]+ will match any string in lowercase or Uppercase. Its in small bracket ( ) so that it store its value in $1 and when it occurs second time it will store its value in $2.

This is so simple as we can see. I have written this post for the beginners like me. I written what I learn reading other forums and blog which are seems to be very complex for understanding it.

Any Correction and advice from you friends will be appreciated.

Thank you Please comment here if any questions.. 🙂

How to Improve MySQL Large Database Performance

Hello friends,

This is my first post for the blog. Recently I have worked on two large database projects on   backend MySQL Database. During the period of the project I came across with many problems and somehow I managed them all.

Many of you also came across with the same MySQL Database problem with Performance. Query is not running well etc. During this period  I read many blogs and forums to solve these problems. I have figured out some tips or can say some points which we should taken care of.

Database Engine (MYISAM vs. Innodb)

Both MySQL Database engines have its own pros and cons that we have to decide which type of db engine will work great for us. Below are some concerns with this two engines:

MYISAM

  • MYISAM use less memory
  • It allows full text search
  • It locks the table while writing
  • It is useful for application in which reading is high and fewer write

InnoDB

  • It uses more memory
  • It does not support full text search
  • InnoDB provides faster performance
  • It lock the table at row level while writing to the table
  • It works great for the application which make extensive use of read and write both

 

Good Database Design

Good database design is a backbone for the application performance. Bad design makes application performance less. Table must be normalized. Data Structure is the main factor which must be developed carefully. Every Developer must give time for each table and fields for making good design. You should give proper data type to each field. When you done with database creation, you would like to see what is MySQL suggest you for your database table. Here is the Command which can help you to get this information:

You can find full description here: http://dev.mysql.com/doc/refman/5.5/en/analyze-table.html

 Indexes

What many of us is knowing that indexes help us to increase the speed of query. Many times indexes create confusion in mind. Creating of proper indexes for the table is necessary but do make table overhead of indexes as indexes take space on the disk. So it increases the workload on the disk. Working on my project I many time need to add and delete column from large table which takes so much time. For increasing performance we can make index ON of OFF. so before whenever I start my database operation I make the index key off and do the operation when operations completed I again makes indexes ON. So rather than making new indexes on my each operation, it will make new index when I male Index on so its only one time. for turning on and index I use below syntax:

 

Tuning MySQL

Hardware makes as important role as other things in database tuning. Our hardware needs just as much attention and tuning as our database and script does. It is also need to check MySQL Configuration file what type of changes we have make to the configuration. There is one tool available which is perl script. You can download and Install on server It tells that which type of configuration change you can make improve the performance.

 

Note: This are the simple things which I feel good to use and take care for better performance.