Pages

Tuesday, February 23, 2016

Upload FILE in wordpress through a Template file (frontend)

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
<input type="hidden" name="post_id" id="post_id" value="12" />
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>

<?php



// Check that the nonce is valid, and the user can edit this post.
if ( 
isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
&& wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
&& current_user_can( 'edit_post', $_POST['post_id'] )
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.

// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
} else {
// The image was uploaded successfully!
//update_post_meta($pid,'upload_pdf', $attachment_id);
echo $attachment_id;

}

} else {

// The security check failed, maybe show the user an error.
}
 ?>

Explode legal name to first_name and last_name From a SQL Table

$sql = "SELECT *,SUBSTRING_INDEX(SUBSTRING_INDEX(legalName, ' ', 1), ' ', -1) AS first_name,
     TRIM( SUBSTR(legalName, LOCATE(' ', legalName)) ) AS last_name 
     FROM users WHERE isDeleted = 0 AND status = 1 AND userRole = 2 ORDER BY last_name ASC";

Query By: Muhammad Saad

Monday, February 15, 2016

Set the Pagination in wordpress query with query string URL

<?php echo paginate_links(array(
     'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
     'current' => max(1, get_query_var('paged')),
     'format' => 'page/%#%',
     'total' => $wp_query->max_num_pages,
     'add_args' => array(
         's' => get_query_var('s'),
         'post_type' => get_query_var('post_type'),
     )
 )); ?>

Sunday, February 7, 2016

Export CSV File from your database records in wordpress

 global $wpdb;
 $edit_query = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."car_bookings");

$output_filename = 'MyReport.csv';
$output_handle = fopen( 'php://output', 'w' );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: application/csv' );
header( 'Content-Disposition: attachment; filename=' . $output_filename );
header( 'Expires: 0' );
header( 'Pragma: public' );   
ob_end_clean();

$first = true;
   // Parse results to csv format
   foreach ($edit_query as $row) {
    
      // Add table headers
      if($first){
         $titles = array();
         foreach($row as $key=>$val){
            $titles[] = $key;
         }
         fputcsv($output_handle, $titles);
         $first = false;
      }
     
       $leadArray = (array) $row; // Cast the Object to an array
       // Add row to file
       fputcsv( $output_handle, $leadArray );
   }

die();