Wednesday, 28 May 2014
Thursday, 1 May 2014
Custom Post type
Add this following code in your function.php file in to the theme
/******************************************************/
add_action( 'init', 'register_cpt_event' );
function register_cpt_event() {
$labels = array(
'name' => _x( 'events', 'event' ),
'singular_name' => _x( 'event', 'event' ),
'add_new' => _x( 'Add New', 'event' ),
'add_new_item' => _x( 'Add New event', 'event' ),
'edit_item' => _x( 'Edit event', 'event' ),
'new_item' => _x( 'New event', 'event' ),
'view_item' => _x( 'View event', 'event' ),
'search_items' => _x( 'Search events', 'event' ),
'not_found' => _x( 'No events found', 'event' ),
'not_found_in_trash' => _x( 'No events found in Trash', 'event' ),
'parent_item_colon' => _x( 'Parent event:', 'event' ),
'menu_name' => _x( 'events', 'event' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'new event',
'supports' => array( 'title', 'editor', 'thumbnail' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'event', $args );
}
/**************************************************************/
/******************************************************/
add_action( 'init', 'register_cpt_event' );
function register_cpt_event() {
$labels = array(
'name' => _x( 'events', 'event' ),
'singular_name' => _x( 'event', 'event' ),
'add_new' => _x( 'Add New', 'event' ),
'add_new_item' => _x( 'Add New event', 'event' ),
'edit_item' => _x( 'Edit event', 'event' ),
'new_item' => _x( 'New event', 'event' ),
'view_item' => _x( 'View event', 'event' ),
'search_items' => _x( 'Search events', 'event' ),
'not_found' => _x( 'No events found', 'event' ),
'not_found_in_trash' => _x( 'No events found in Trash', 'event' ),
'parent_item_colon' => _x( 'Parent event:', 'event' ),
'menu_name' => _x( 'events', 'event' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'new event',
'supports' => array( 'title', 'editor', 'thumbnail' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'event', $args );
}
/**************************************************************/
Monday, 21 April 2014
EXPORT FORM DATA TO EXCEL (CSV) IN PHP
In this tutorial you are going to learn how to download an excel report after filling a HTML form.
MySQL is not used in this tutorial, just a simple example showing how to generate Excel file using core PHP functions.
<?php
//give a filename
$filename = "myexcel.csv";
//set headers to download the file
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
//set an array
$cells = array("Column1", "Column2", "Column3");
$file = fopen('php://output', 'w');
fputcsv($file,$cells);
fclose($file);
?>
//give a filename
$filename = "myexcel.csv";
//set headers to download the file
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
//set an array
$cells = array("Column1", "Column2", "Column3");
$file = fopen('php://output', 'w');
fputcsv($file,$cells);
fclose($file);
?>
When you run this code on a web browser, a CSV (myexcel.csv) file will immediately download with the values given in the array $cells[].
I have used php://output stream which sends the output from Apache server to the browser.
fputcsv() - this function plays the main role in the above script, it helps us to create the CSV file from the array $cells[].
Now, lets move to the dynamic part,
Now, lets move to the dynamic part,
Create a form in HTML, see the below (index.html):
<html>
<body>
<form action="excel.php" method="post">
<table>
<tr><td>Name</td><td><input type="text" name="name" /></td></tr>
<tr><td>Email</td><td><input type="text" name="email" /></td></tr>
<tr><td>Location</td><td><input type="text" name="location" /></td></tr>
<tr><td> </td><td><input type="submit" /></td></tr>
</table>
</form>
</body>
</html>
<body>
<form action="excel.php" method="post">
<table>
<tr><td>Name</td><td><input type="text" name="name" /></td></tr>
<tr><td>Email</td><td><input type="text" name="email" /></td></tr>
<tr><td>Location</td><td><input type="text" name="location" /></td></tr>
<tr><td> </td><td><input type="submit" /></td></tr>
</table>
</form>
</body>
</html>
PHP script – slightly modified from the first script (excel.php):
<?php
//give a filename
$filename = "myexcel.csv";
//set headers to download file
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$file = fopen('php://output', 'w');
//set the column names
$cells[] = array('Name', 'Email', 'Location');
//pass all the form values
$cells[] = array($_POST['name'], $_POST['email'], $_POST['location']);
foreach($cells as $cell){
fputcsv($file,$cell);
}
fclose($file);
?>
//give a filename
$filename = "myexcel.csv";
//set headers to download file
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$file = fopen('php://output', 'w');
//set the column names
$cells[] = array('Name', 'Email', 'Location');
//pass all the form values
$cells[] = array($_POST['name'], $_POST['email'], $_POST['location']);
foreach($cells as $cell){
fputcsv($file,$cell);
}
fclose($file);
?>
if you compare the first script and this one above, you can see there is a difference in $cells[] variable which became multi-dimensional array and also I’m looping through each array usingforeach()
Basically first iteration will pass the column names which is hard coded, the second iteration will pass the dynamic values from $_POST.
Monday, 14 April 2014
WordPress Blog Too Slow? 7 Ways You Can Speed It Up Again In Minutes
Your WordPress site is slow, and you’re losing page views and revenue because of it. But you’re also busy – I get it. Here’s seven speed tips you could have a crack at on your lunch break – they’re so simple and easy.
Optimize Images
This is a two-parter. The first thing is to equip yourself with the knowledge of using the best format for the job. When we talk about images used on the web, it’s generally restricted to either a JPG or PNG. PNG is a lossless format, best suited to simple images and illustrations, or screenshots. JPG is a lossy format suitable for photographs – it can retain the detail at varying levels of quality. If you’re uploading photographs in PNG format, you probably have images that are up to ten times as big as they would be if you used JPG. Just bear in mind that JPG loses a little detail and clarity each time modifications are made and it’s saved – so always export from your originals.
- Use JPG for photos, but only export once.
- Use PNG for illustrations and screenshots
Second – even if you are using the the correct image format, chances are you could still be making savings by compressing and optimizing those images. EWWW Image Optimizer is a simple plugin that will do this for automatically as you upload them to WordPress. You can also run the optimizer over all your existing images, potentially shaving up to 95% with no noticeable loss in quality; and even convert to and from PNG and JPG if you did choose the wrong format. That alone will make a huge difference to loading time for the end user. What are you waiting for?
Friday, 28 March 2014
Import/Export Widget Settings in WordPress
Sometimes when moving WordPress sites, you may need to save and reuse widget settings from one WordPress installation to another. If a site has just a few widgets, then you can probably do that manually. However, if there are many widgets inmultiple sidebars and widget areas, then you need a better solution. In this article, we will show you how to import/export widget settings in WordPress.
First thing you need to do is install and activate the Widget Settings Import/Exportplugin. Upon activation, go to Tools » Widget Settings Export and select the widgets you want to export and click on Export Widget Settings Button. The plugin will now export all your currently active widgets in
widget_data.json file.
To restore these widgets in another WordPress installation, you will need to install and activate the Widget Settings Import/Export plugin on that site as well. Once you have activated the plugin, go to Tools » Widget Settings Import and click on the Select File button.
Locate the JSON file you downloaded earlier and then click on the Show Widgets Settings button. The plugin will now load widget data from the imported file and will show you the list of widgets. Select the widgets you want to import and click onImport Widget Settings to proceed. All your widgets will be restored to appropriate sidebars.
Please note that if you have active widgets added to your site by a plugin, then you may not be able to import those widgets until you have those plugins installed on the target website as well.
Monday, 24 March 2014
Bulk Install Your Favorite Plugins in WordPress
Every WordPress user has their favorite plugins which they like to use on all their sites. We have our own set of must use WordPress plugins. In the past, you had to manually install each individual plugin for every site that you setup. In this article, we will show you how to bulk install your favorite WordPress plugins. This will come in handy if you manage multiple WordPress sites and need to setup new WordPress sites regularly.
First thing you need to do is install and activate the WP Install Profiles plugin. After activation, you need to go to Plugins » Bulk Install Profiles.
You will see a default list of plugins there at first. If you want to use a plugin from that list, then keep the ones you want to use and delete the rest. You can also add new plugins to this list as long as they’re available on WordPress.org.
To add a plugin, you need to add the name used in the plugin’s URL slug. For example, if you want to add floating social bar, then you will enter either floating-social-bar or floating social bar.
Once you have entered all the plugins that you want to install, you can give this list a profile name, e.g. MyFavoritePlugins. After that you can save the profile, or click onDownload plugins and save profile.
Thursday, 20 March 2014
Paginate Comments in WordPress
Have you noticed that your post with lot of comments load slower than your other posts that has barely any comments. Because your most commented posts are usually the most popular, you want these posts to load fast. In this article, we will show you how to paginate comments in WordPress to speed up your post load time.
First thing you need to do is login to your WordPress admin area and go to Settings » Discussions. Next look at the section where it says Break comments into pages. Simply select the number of comments you want on each page, and how you want to display them.
Once you do this, based on your theme styling, either you will see a numeric pagination or you will see Previous / Next comment links.
Several users have reported that sometimes posts with a lot comments return awhite screen of death because it exhausts their memory.
If it doesn’t cause a white screen of death, then posts with a lot of comments tend to be really really slow. We strongly recommend that you paginate your comments.
Note: you can also paginate posts by splitting a long post into multiple pages.
Subscribe to:
Posts (Atom)
