How to create simple plugin in WORDPRESS
Create a new folder in yourSite/wp-content/plugins/
Create a new php file in this folder (ex- products.php)
Open products.php file in editor and add these lines into your page at the top
<?php
/*
Plugin Name: Your Plugin Name
Plugin URI: Your Plugin URI
Description: A very simple Plugin
Author: Your Name
Version: 1.0
Author URI: Custom
*/ ?>
Now Create functions into this file
add_action('admin_menu', 'admin_menu_item'); // add a new menu item in admin panel
add_action('init', 'import_csv_data'); //initialize a function on the front page of plugin
function admin_menu_item(){
// see http://codex.wordpress.org/Adding_Administration_Menus
add_menu_page('Shareasale Product Import','Shareasale Feed Import','administrator','shareasale_import','shareasale_import');
}
function shareasale_import(){
echo '<div class="wrap"><h2>Import Shareasale.com data feed:</h2>';
echo '<form enctype="multipart/form-data" name="import_form" method="post" action="#"><table>';
echo '<tr><td>CSV:</td><td><input type="file" name="datafeed" /></td></tr>';
echo '<tr><td>Shareasale affiliate ID:</td><td><input type="text" name="affiliateid" /></td></tr>';
echo '<tr><td></td><td><input type="submit" name="importfeed" value="Start import" /></td></tr>';
echo '</table></form>';
echo '</div>';
}
function import_csv_data() {
global $wpdb;
if(isset($_POST['importfeed']))
{
if($_POST['affiliateid']!="" && $_FILES['datafeed']!="")
{
$affid = $_POST['affiliateid'];
$getData = file_get_contents($_FILES['datafeed']['tmp_name']); //file_get_contents("29482.txt");
$getdata_row = explode(PHP_EOL, $getData);
foreach($getdata_row as $line){
if(trim($line) != ''){
$bits[] = explode('|',$line);
}
}
foreach ($bits as $key => $value) {
$product_id = trim($value[0]);
$query = $wpdb->query("SELECT wp_postmeta.meta_id FROM wp_postmeta WHERE 1 AND wp_postmeta.meta_key = 'product_id' AND wp_postmeta.meta_value = '$product_id'");
if(!$query)
{
$title = trim($value[1]);
$description = $value[11];
$stock_status = $value[18];
$manufacturer = $value[19];
if($value[12] != ''){ $description .= "<br /><br />".$value[12]; }
if($value[13] != ''){ $description .= "<br /><br />".$value[13]; }
if($value[14] != ''){ $description .= "<br /><br />".$value[14]; }
if($value[15] != ''){ $description .= "<br /><br />".$value[15]; }
$description = trim($description);
$postcategories = $value[9];
$merchentid = $value[2];
if($value[5] != ''){ $image_thumb = $value[5]; $image_thumb = trim($image_thumb); }
if($value[6] != '') { $image_large = $value[6]; $image_large = trim($image_large); } // 5-thumb / 6-big image
if($value[7] != ''){ $price = $value[7]; }else{ $price = $value[8]; } // 7-price / 8-retail price
$affiliateurl = trim(str_replace('YOURUSERID',$affid,$value[4]));
// Add Featured Image to Post
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = @file_get_contents($image_thumb); // Get image data
$filename = basename($image_thumb); // Create image file name
if($image_data!==false)
{
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
}
$postid = wp_insert_post(array( 'post_title'=>$title,'post_type'=>'product', 'post_content'=>$description, 'post_author'=>1, 'post_status'=>'publish' ));
add_post_meta($postid,'product_id', $product_id);
add_post_meta($postid, 'image_thumb', $image_thumb);
add_post_meta($postid,'image_large', $image_large);
add_post_meta($postid, 'price', $price);
add_post_meta($postid, 'affid', $affiliateurl);
add_post_meta($postid,'merchentid', $merchentid);
add_post_meta($postid,'catagory', $postcategories);
add_post_meta($postid,'stock_status', $stock_status);
add_post_meta($postid,'manufacturer', $manufacturer);
if($image_data!==false)
{
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $postid );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $postid, $attach_id );
}
}
}
echo '<script>alert("CSV IMPORTED SUCCESSFULLY!");</script>';
}else {
echo '<script>alert("All fields are required!");</script>';
}
}
}
add_action( 'init', 'create_post_type' );
function create_post_type() {
add_theme_support('post-thumbnails');
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' )
)
);
}
function getallproducts()
{
$type = 'product';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1);
?>
<div class="search_product_wrapper">
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" class="home_searchbox" />
<input type="submit" id="searchsubmit" value="Search" class="btn btn_home_search" />
</div>
</form>
</div>
<div style="clear:both"></div>
<?php
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$post_id = get_the_ID();
$meta_values = get_post_meta($post_id, "price", true);
$meta_manufacturer = get_post_meta($post_id, "manufacturer" , true);
if (strlen(get_the_title()) > 35) {
$product_title = substr(get_the_title(), 0, 40);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
} else {
$product_title = get_the_title();
}
?>
<div class="col-sm-3 product_wrapper">
<p>
<?php if (has_post_thumbnail()) { the_post_thumbnail(); } else { ?> <img src="<?php bloginfo('template_directory'); ?>/not-available.jpg" alt="<?php the_title(); ?>" height="150px" width="150px" /> <?php } ?>
</p>
<div class="title_wrapper">
<a class="nodecor" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
</div>
<p class="price_wrapper"><?php echo $meta_values; ?></p>
</div>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
add_shortcode('getallproducts', 'getallproducts');
-------------------------------------------------------
UPDATED CODE
---------------------------------------------------------
<?php
/*
Plugin Name: Shareasale Feed Importer
Plugin URI:
Description: A very simple Shareasale data importer
Author: webplanetsoft
Version: 1.0
Author URI: http://webplanetsoft.com
*/
add_action('admin_menu', 'admin_menu_item');
add_action('init', 'import_csv_data');
function admin_menu_item(){
// see http://codex.wordpress.org/Adding_Administration_Menus
add_menu_page('Shareasale Product Import','Shareasale Feed Import','administrator','shareasale_import','shareasale_import');
}
function shareasale_import(){
echo '<div class="wrap"><h2>Import Shareasale.com data feed:</h2>';
echo '<form enctype="multipart/form-data" name="import_form" method="post" action="#"><table>';
echo '<tr><td>CSV:</td><td><input type="file" name="datafeed" /></td></tr>';
echo '<tr><td>Shareasale affiliate ID:</td><td><input type="text" name="affiliateid" /></td></tr>';
echo '<tr><td></td><td><input type="submit" name="importfeed" value="Start import" /></td></tr>';
echo '</table></form>';
echo '</div>';
}
function import_csv_data() {
global $wpdb;
if(isset($_POST['importfeed']))
{
if($_POST['affiliateid']!="" && $_FILES['datafeed']!="")
{
$affid = $_POST['affiliateid'];
$getData = file_get_contents($_FILES['datafeed']['tmp_name']);
$getdata_row = explode(PHP_EOL, $getData);
foreach($getdata_row as $line){
if(trim($line) != ''){
$bits[] = explode('|',$line);
}
}
foreach ($bits as $key => $value) {
$product_id = trim($value[0]);
$query = $wpdb->get_results("SELECT wplc_postmeta.meta_id FROM wplc_postmeta WHERE 1 AND wplc_postmeta.meta_key = 'product_id' AND wplc_postmeta.meta_value = '$product_id'");
$total_result_rows = $wpdb->num_rows;
if($total_result_rows == 0)
{
$title = trim($value[1]);
$description = $value[11];
$stock_status = $value[18];
$manufacturer = $value[19];
if($value[12] != ''){ $description .= "<br /><br />".$value[12]; }
if($value[13] != ''){ $description .= "<br /><br />".$value[13]; }
if($value[14] != ''){ $description .= "<br /><br />".$value[14]; }
if($value[15] != ''){ $description .= "<br /><br />".$value[15]; }
$description = trim($description);
$postcategories = $value[9];
$merchentid = $value[2];
if($value[5] != ''){ $image_thumb = $value[5]; $image_thumb = trim($image_thumb); }
if($value[6] != '') { $image_large = $value[6]; $image_large = trim($image_large); } // 5-thumb / 6-big image
if($value[7] != ''){ $price = $value[7]; }else{ $price = $value[8]; } // 7-price / 8-retail price
$affiliateurl = trim(str_replace('YOURUSERID',$affid,$value[4]));
// Add Featured Image to Post
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = @file_get_contents($image_thumb); // Get image data
$filename = basename($image_thumb); // Create image file name
if($image_data!==false)
{
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
}
$postid = wp_insert_post(array( 'post_title'=>$title,'post_type'=>'product', 'post_content'=>$description, 'post_author'=>1, 'post_status'=>'publish' ));
add_post_meta($postid,'product_id', $product_id);
add_post_meta($postid, 'image_thumb', $image_thumb);
add_post_meta($postid,'image_large', $image_large);
add_post_meta($postid, 'price', $price);
add_post_meta($postid, 'affid', $affiliateurl);
add_post_meta($postid,'merchentid', $merchentid);
add_post_meta($postid,'catagory', $postcategories);
add_post_meta($postid,'stock_status', $stock_status);
add_post_meta($postid,'manufacturer', $manufacturer);
if($image_data!==false)
{
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $postid );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $postid, $attach_id );
}
}
}
echo '<script>alert("CSV IMPORTED SUCCESSFULLY!");</script>';
}else {
echo '<script>alert("All fields are required!");</script>';
}
}
}
add_action( 'init', 'create_post_type' );
function create_post_type() {
add_theme_support('post-thumbnails');
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' )
)
);
}
function getallproducts()
{
?>
<div class="search_product_wrapper">
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" class="home_searchbox" />
<input type="submit" id="searchsubmit" value="Search" class="btn btn_home_search" />
</div>
</form>
</div>
<div style="clear:both"></div>
<?php
$type = 'product';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 5,
'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { //<?php query_posts('showposts=5'); if (have_posts()) : while (have_posts()) : the_post();
while ($my_query->have_posts()) : $my_query->the_post();
$post_id = get_the_ID();
$meta_values = get_post_meta($post_id, "price", true);
$meta_manufacturer = get_post_meta($post_id, "manufacturer" , true);
if (strlen(get_the_title()) > 35) {
$product_title = substr(get_the_title(), 0, 35);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
} else {
$product_title = get_the_title();
}
?>
<div class="col-sm-3 product_wrapper">
<div>
<a href="<?php the_permalink() ?>"><?php if (has_post_thumbnail()) { the_post_thumbnail(); } else { ?> <img class="default-img-class" src="<?php bloginfo('template_directory'); ?>/not-available.jpg" alt="<?php the_title(); ?>" height="150px" width="150px" /> <?php } ?></a>
</div>
<div class="title_wrapper">
<a class="nodecor" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?> "><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
</div>
<p class="price_wrapper">$<?php echo $meta_values; ?></p>
</div>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<div class="all_stores_avail">
<div class="titleof_block"></div>
<div class="store-data-container"><?php echo do_shortcode('[jw_easy_logo slider_name="stores"]'); ?></div>
</div>
<?php
}
add_shortcode('getallproducts', 'getallproducts');
function get_post_data_with_details()
{
$meta_catagory = get_post_meta(get_the_ID(), "catagory" , true);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'catagory',
'meta_value' => $meta_catagory,
'showposts' => 5,
'orderby' => 'rand'
);
$posts = get_posts( $args );
// DO NOTHING
if( !$posts )
return;
foreach( $posts as $post )
{
$catagroy_title = $product_details['catagory'];
$meta = get_post_meta( $post->ID );
$product_meta_details="";
foreach ($meta as $key => $metavalue) {
$product_meta_details[$key] = $metavalue[0];
}
$product_title = $post->post_title;
if (strlen($product_title) > 35) {
$product_title = substr($product_title, 0, 35);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
}
?>
<div class="col-sm-3 product_wrapper bottom_pro_wrap">
<div class="img_pro_wrap">
<a href="<?php echo $post->guid; ?>"><img class="default-img-class" src="<?php echo $product_meta_details['image_large'] ?>" alt="<?php the_title(); ?>" height="150px" width="100%" /></a>
</div>
<div class="title_wrapper bottom_title_wrap">
<a class="nodecor" href="<?php echo $post->guid; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?> "><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($product_meta_details['manufacturer'])) { echo $product_meta_details['manufacturer']; } ?></div>
</div>
<p class="price_wrapper">$<?php echo $product_meta_details['price']; ?></p>
</div>
<?php
}
}
add_shortcode('getrelatedproducts', 'get_post_data_with_details');
function add_css() {
?>
<style>
.store-data-container { margin-top:50px; }
.store-data-container a { text-decoration:none!important; }
.title_wrapper a { text-decoration:none!important; color: #fb7203!important; }
.title_wrapper a:hover { text-decoration:none!important; color: black!important; }
.product_wrapper {
border: 1px solid #dedede;
display:inline-block;
margin-bottom: 10px;
margin-right: 10px;
padding: 15px;
width: 160px;
vertical-align:top;
height:255px;
background-color: #fff;
}
.img_pro_wrap { height:190px; }
.bottom_pro_wrap { height:270px; padding-top:0px; }
.price_wrapper { float: right; font-weight: bold;}
.title_wrapper { border-bottom:1px solid #dedede; margin-bottom:0px; padding-bottom:10px; height:65px; }
.bottom_title_wrap { height: 60px;
margin-bottom: 0;
padding-bottom: 0; }
.nodecor { text-decoration:none; }
.default-img-class { padding-top:10px; }
.product_wrapper:hover {
-moz-box-shadow: inset 0 0 10px #000000;
-webkit-box-shadow: inset 0 0 10px #000000;
box-shadow: inset 0 0 10px #000000;
}
.search_product_wrapper {
text-align:center;
margin:50px;
}
.home_searchbox {
height: 30px;
width: 590px;
}
.btn_home_search {
height: 37px;
}
.gallery-holder {
width: 37%;
float:left;
}
.title_product a {
color: #3d3d3d;
font-size: 30px;
font-weight: 500;
letter-spacing: -1px;
line-height: 36px;
}
.right_body.availability {
display: inline-block;
line-height: 20px;
margin: 0 0 0 21px;
vertical-align: top;
}
.price { padding-top:30px; }
.price-current {
color: #28124a;
display: inline-block;
font-size: 40px;
font-weight: 700;
vertical-align: top;
}
.qnt-holder {
display: inline-block;
margin: 24px 0;
vertical-align: top;
}
.right_body #addto-cart {
margin: 0 0 0 18px;
}
.le-button.huge {
padding: 18px 52px;
}
.le-button {
background-color: #f27a24;
}
.le-button {
border: medium none;
border-radius: 4px;
color: #fff;
display: inline-block;
font-size: 15px;
font-weight: bold;
line-height: 20px;
padding: 10px 20px;
text-transform: capitalize;
transition: all 0.3s ease 0s;
}
.brand {
border-bottom: 2px solid #dedede;
padding-bottom: 40px;
}
.availability label {
display: inline-block;
font-weight: 700;
margin-bottom: 5px;
margin-top:5px;
color:grey;
}
.availability .available {
color: #59b210;
font-weight: 700 !important;
text-transform: capitalize;
}
.product_description_tab { padding-top:30px;}
.tab_button {
background-color: #f27a24;
border-radius: 10px 10px 0 0;
color: #fff;
font-size: 18px;
font-weight: bold;
padding: 20px 20px 20px 25px;
width: 115px;
}
.tab_details {
background-color: #fff;
border: 1px solid #dedede;
margin-right: 20px;
padding: 20px;
margin-bottom:40px;
}
.desc_tab1 { font-weight:bold; padding:5px; color:grey; font-size:17px; }
.desc_tab1_content { padding:5px; color:grey; }
.price_wrapper{
float: right;
font-weight: bold!important;
text-align:right;
width:100%;
}
</style>
<?php }
add_action('wp_footer', 'add_css');
?>
Create a new folder in yourSite/wp-content/plugins/
Create a new php file in this folder (ex- products.php)
Open products.php file in editor and add these lines into your page at the top
<?php
/*
Plugin Name: Your Plugin Name
Plugin URI: Your Plugin URI
Description: A very simple Plugin
Author: Your Name
Version: 1.0
Author URI: Custom
*/ ?>
Now Create functions into this file
add_action('admin_menu', 'admin_menu_item'); // add a new menu item in admin panel
add_action('init', 'import_csv_data'); //initialize a function on the front page of plugin
function admin_menu_item(){
// see http://codex.wordpress.org/Adding_Administration_Menus
add_menu_page('Shareasale Product Import','Shareasale Feed Import','administrator','shareasale_import','shareasale_import');
}
function shareasale_import(){
echo '<div class="wrap"><h2>Import Shareasale.com data feed:</h2>';
echo '<form enctype="multipart/form-data" name="import_form" method="post" action="#"><table>';
echo '<tr><td>CSV:</td><td><input type="file" name="datafeed" /></td></tr>';
echo '<tr><td>Shareasale affiliate ID:</td><td><input type="text" name="affiliateid" /></td></tr>';
echo '<tr><td></td><td><input type="submit" name="importfeed" value="Start import" /></td></tr>';
echo '</table></form>';
echo '</div>';
}
function import_csv_data() {
global $wpdb;
if(isset($_POST['importfeed']))
{
if($_POST['affiliateid']!="" && $_FILES['datafeed']!="")
{
$affid = $_POST['affiliateid'];
$getData = file_get_contents($_FILES['datafeed']['tmp_name']); //file_get_contents("29482.txt");
$getdata_row = explode(PHP_EOL, $getData);
foreach($getdata_row as $line){
if(trim($line) != ''){
$bits[] = explode('|',$line);
}
}
foreach ($bits as $key => $value) {
$product_id = trim($value[0]);
$query = $wpdb->query("SELECT wp_postmeta.meta_id FROM wp_postmeta WHERE 1 AND wp_postmeta.meta_key = 'product_id' AND wp_postmeta.meta_value = '$product_id'");
if(!$query)
{
$title = trim($value[1]);
$description = $value[11];
$stock_status = $value[18];
$manufacturer = $value[19];
if($value[12] != ''){ $description .= "<br /><br />".$value[12]; }
if($value[13] != ''){ $description .= "<br /><br />".$value[13]; }
if($value[14] != ''){ $description .= "<br /><br />".$value[14]; }
if($value[15] != ''){ $description .= "<br /><br />".$value[15]; }
$description = trim($description);
$postcategories = $value[9];
$merchentid = $value[2];
if($value[5] != ''){ $image_thumb = $value[5]; $image_thumb = trim($image_thumb); }
if($value[6] != '') { $image_large = $value[6]; $image_large = trim($image_large); } // 5-thumb / 6-big image
if($value[7] != ''){ $price = $value[7]; }else{ $price = $value[8]; } // 7-price / 8-retail price
$affiliateurl = trim(str_replace('YOURUSERID',$affid,$value[4]));
// Add Featured Image to Post
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = @file_get_contents($image_thumb); // Get image data
$filename = basename($image_thumb); // Create image file name
if($image_data!==false)
{
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
}
$postid = wp_insert_post(array( 'post_title'=>$title,'post_type'=>'product', 'post_content'=>$description, 'post_author'=>1, 'post_status'=>'publish' ));
add_post_meta($postid,'product_id', $product_id);
add_post_meta($postid, 'image_thumb', $image_thumb);
add_post_meta($postid,'image_large', $image_large);
add_post_meta($postid, 'price', $price);
add_post_meta($postid, 'affid', $affiliateurl);
add_post_meta($postid,'merchentid', $merchentid);
add_post_meta($postid,'catagory', $postcategories);
add_post_meta($postid,'stock_status', $stock_status);
add_post_meta($postid,'manufacturer', $manufacturer);
if($image_data!==false)
{
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $postid );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $postid, $attach_id );
}
}
}
echo '<script>alert("CSV IMPORTED SUCCESSFULLY!");</script>';
}else {
echo '<script>alert("All fields are required!");</script>';
}
}
}
add_action( 'init', 'create_post_type' );
function create_post_type() {
add_theme_support('post-thumbnails');
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' )
)
);
}
function getallproducts()
{
$type = 'product';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1);
?>
<div class="search_product_wrapper">
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" class="home_searchbox" />
<input type="submit" id="searchsubmit" value="Search" class="btn btn_home_search" />
</div>
</form>
</div>
<div style="clear:both"></div>
<?php
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$post_id = get_the_ID();
$meta_values = get_post_meta($post_id, "price", true);
$meta_manufacturer = get_post_meta($post_id, "manufacturer" , true);
if (strlen(get_the_title()) > 35) {
$product_title = substr(get_the_title(), 0, 40);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
} else {
$product_title = get_the_title();
}
?>
<div class="col-sm-3 product_wrapper">
<p>
<?php if (has_post_thumbnail()) { the_post_thumbnail(); } else { ?> <img src="<?php bloginfo('template_directory'); ?>/not-available.jpg" alt="<?php the_title(); ?>" height="150px" width="150px" /> <?php } ?>
</p>
<div class="title_wrapper">
<a class="nodecor" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
</div>
<p class="price_wrapper"><?php echo $meta_values; ?></p>
</div>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
add_shortcode('getallproducts', 'getallproducts');
-------------------------------------------------------
UPDATED CODE
---------------------------------------------------------
<?php
/*
Plugin Name: Shareasale Feed Importer
Plugin URI:
Description: A very simple Shareasale data importer
Author: webplanetsoft
Version: 1.0
Author URI: http://webplanetsoft.com
*/
add_action('admin_menu', 'admin_menu_item');
add_action('init', 'import_csv_data');
function admin_menu_item(){
// see http://codex.wordpress.org/Adding_Administration_Menus
add_menu_page('Shareasale Product Import','Shareasale Feed Import','administrator','shareasale_import','shareasale_import');
}
function shareasale_import(){
echo '<div class="wrap"><h2>Import Shareasale.com data feed:</h2>';
echo '<form enctype="multipart/form-data" name="import_form" method="post" action="#"><table>';
echo '<tr><td>CSV:</td><td><input type="file" name="datafeed" /></td></tr>';
echo '<tr><td>Shareasale affiliate ID:</td><td><input type="text" name="affiliateid" /></td></tr>';
echo '<tr><td></td><td><input type="submit" name="importfeed" value="Start import" /></td></tr>';
echo '</table></form>';
echo '</div>';
}
function import_csv_data() {
global $wpdb;
if(isset($_POST['importfeed']))
{
if($_POST['affiliateid']!="" && $_FILES['datafeed']!="")
{
$affid = $_POST['affiliateid'];
$getData = file_get_contents($_FILES['datafeed']['tmp_name']);
$getdata_row = explode(PHP_EOL, $getData);
foreach($getdata_row as $line){
if(trim($line) != ''){
$bits[] = explode('|',$line);
}
}
foreach ($bits as $key => $value) {
$product_id = trim($value[0]);
$query = $wpdb->get_results("SELECT wplc_postmeta.meta_id FROM wplc_postmeta WHERE 1 AND wplc_postmeta.meta_key = 'product_id' AND wplc_postmeta.meta_value = '$product_id'");
$total_result_rows = $wpdb->num_rows;
if($total_result_rows == 0)
{
$title = trim($value[1]);
$description = $value[11];
$stock_status = $value[18];
$manufacturer = $value[19];
if($value[12] != ''){ $description .= "<br /><br />".$value[12]; }
if($value[13] != ''){ $description .= "<br /><br />".$value[13]; }
if($value[14] != ''){ $description .= "<br /><br />".$value[14]; }
if($value[15] != ''){ $description .= "<br /><br />".$value[15]; }
$description = trim($description);
$postcategories = $value[9];
$merchentid = $value[2];
if($value[5] != ''){ $image_thumb = $value[5]; $image_thumb = trim($image_thumb); }
if($value[6] != '') { $image_large = $value[6]; $image_large = trim($image_large); } // 5-thumb / 6-big image
if($value[7] != ''){ $price = $value[7]; }else{ $price = $value[8]; } // 7-price / 8-retail price
$affiliateurl = trim(str_replace('YOURUSERID',$affid,$value[4]));
// Add Featured Image to Post
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = @file_get_contents($image_thumb); // Get image data
$filename = basename($image_thumb); // Create image file name
if($image_data!==false)
{
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
}
$postid = wp_insert_post(array( 'post_title'=>$title,'post_type'=>'product', 'post_content'=>$description, 'post_author'=>1, 'post_status'=>'publish' ));
add_post_meta($postid,'product_id', $product_id);
add_post_meta($postid, 'image_thumb', $image_thumb);
add_post_meta($postid,'image_large', $image_large);
add_post_meta($postid, 'price', $price);
add_post_meta($postid, 'affid', $affiliateurl);
add_post_meta($postid,'merchentid', $merchentid);
add_post_meta($postid,'catagory', $postcategories);
add_post_meta($postid,'stock_status', $stock_status);
add_post_meta($postid,'manufacturer', $manufacturer);
if($image_data!==false)
{
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $postid );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $postid, $attach_id );
}
}
}
echo '<script>alert("CSV IMPORTED SUCCESSFULLY!");</script>';
}else {
echo '<script>alert("All fields are required!");</script>';
}
}
}
add_action( 'init', 'create_post_type' );
function create_post_type() {
add_theme_support('post-thumbnails');
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' )
)
);
}
function getallproducts()
{
?>
<div class="search_product_wrapper">
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" class="home_searchbox" />
<input type="submit" id="searchsubmit" value="Search" class="btn btn_home_search" />
</div>
</form>
</div>
<div style="clear:both"></div>
<?php
$type = 'product';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 5,
'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { //<?php query_posts('showposts=5'); if (have_posts()) : while (have_posts()) : the_post();
while ($my_query->have_posts()) : $my_query->the_post();
$post_id = get_the_ID();
$meta_values = get_post_meta($post_id, "price", true);
$meta_manufacturer = get_post_meta($post_id, "manufacturer" , true);
if (strlen(get_the_title()) > 35) {
$product_title = substr(get_the_title(), 0, 35);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
} else {
$product_title = get_the_title();
}
?>
<div class="col-sm-3 product_wrapper">
<div>
<a href="<?php the_permalink() ?>"><?php if (has_post_thumbnail()) { the_post_thumbnail(); } else { ?> <img class="default-img-class" src="<?php bloginfo('template_directory'); ?>/not-available.jpg" alt="<?php the_title(); ?>" height="150px" width="150px" /> <?php } ?></a>
</div>
<div class="title_wrapper">
<a class="nodecor" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?> "><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
</div>
<p class="price_wrapper">$<?php echo $meta_values; ?></p>
</div>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<div class="all_stores_avail">
<div class="titleof_block"></div>
<div class="store-data-container"><?php echo do_shortcode('[jw_easy_logo slider_name="stores"]'); ?></div>
</div>
<?php
}
add_shortcode('getallproducts', 'getallproducts');
function get_post_data_with_details()
{
$meta_catagory = get_post_meta(get_the_ID(), "catagory" , true);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'catagory',
'meta_value' => $meta_catagory,
'showposts' => 5,
'orderby' => 'rand'
);
$posts = get_posts( $args );
// DO NOTHING
if( !$posts )
return;
foreach( $posts as $post )
{
$catagroy_title = $product_details['catagory'];
$meta = get_post_meta( $post->ID );
$product_meta_details="";
foreach ($meta as $key => $metavalue) {
$product_meta_details[$key] = $metavalue[0];
}
$product_title = $post->post_title;
if (strlen($product_title) > 35) {
$product_title = substr($product_title, 0, 35);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
}
?>
<div class="col-sm-3 product_wrapper bottom_pro_wrap">
<div class="img_pro_wrap">
<a href="<?php echo $post->guid; ?>"><img class="default-img-class" src="<?php echo $product_meta_details['image_large'] ?>" alt="<?php the_title(); ?>" height="150px" width="100%" /></a>
</div>
<div class="title_wrapper bottom_title_wrap">
<a class="nodecor" href="<?php echo $post->guid; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?> "><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($product_meta_details['manufacturer'])) { echo $product_meta_details['manufacturer']; } ?></div>
</div>
<p class="price_wrapper">$<?php echo $product_meta_details['price']; ?></p>
</div>
<?php
}
}
add_shortcode('getrelatedproducts', 'get_post_data_with_details');
function add_css() {
?>
<style>
.store-data-container { margin-top:50px; }
.store-data-container a { text-decoration:none!important; }
.title_wrapper a { text-decoration:none!important; color: #fb7203!important; }
.title_wrapper a:hover { text-decoration:none!important; color: black!important; }
.product_wrapper {
border: 1px solid #dedede;
display:inline-block;
margin-bottom: 10px;
margin-right: 10px;
padding: 15px;
width: 160px;
vertical-align:top;
height:255px;
background-color: #fff;
}
.img_pro_wrap { height:190px; }
.bottom_pro_wrap { height:270px; padding-top:0px; }
.price_wrapper { float: right; font-weight: bold;}
.title_wrapper { border-bottom:1px solid #dedede; margin-bottom:0px; padding-bottom:10px; height:65px; }
.bottom_title_wrap { height: 60px;
margin-bottom: 0;
padding-bottom: 0; }
.nodecor { text-decoration:none; }
.default-img-class { padding-top:10px; }
.product_wrapper:hover {
-moz-box-shadow: inset 0 0 10px #000000;
-webkit-box-shadow: inset 0 0 10px #000000;
box-shadow: inset 0 0 10px #000000;
}
.search_product_wrapper {
text-align:center;
margin:50px;
}
.home_searchbox {
height: 30px;
width: 590px;
}
.btn_home_search {
height: 37px;
}
.gallery-holder {
width: 37%;
float:left;
}
.title_product a {
color: #3d3d3d;
font-size: 30px;
font-weight: 500;
letter-spacing: -1px;
line-height: 36px;
}
.right_body.availability {
display: inline-block;
line-height: 20px;
margin: 0 0 0 21px;
vertical-align: top;
}
.price { padding-top:30px; }
.price-current {
color: #28124a;
display: inline-block;
font-size: 40px;
font-weight: 700;
vertical-align: top;
}
.qnt-holder {
display: inline-block;
margin: 24px 0;
vertical-align: top;
}
.right_body #addto-cart {
margin: 0 0 0 18px;
}
.le-button.huge {
padding: 18px 52px;
}
.le-button {
background-color: #f27a24;
}
.le-button {
border: medium none;
border-radius: 4px;
color: #fff;
display: inline-block;
font-size: 15px;
font-weight: bold;
line-height: 20px;
padding: 10px 20px;
text-transform: capitalize;
transition: all 0.3s ease 0s;
}
.brand {
border-bottom: 2px solid #dedede;
padding-bottom: 40px;
}
.availability label {
display: inline-block;
font-weight: 700;
margin-bottom: 5px;
margin-top:5px;
color:grey;
}
.availability .available {
color: #59b210;
font-weight: 700 !important;
text-transform: capitalize;
}
.product_description_tab { padding-top:30px;}
.tab_button {
background-color: #f27a24;
border-radius: 10px 10px 0 0;
color: #fff;
font-size: 18px;
font-weight: bold;
padding: 20px 20px 20px 25px;
width: 115px;
}
.tab_details {
background-color: #fff;
border: 1px solid #dedede;
margin-right: 20px;
padding: 20px;
margin-bottom:40px;
}
.desc_tab1 { font-weight:bold; padding:5px; color:grey; font-size:17px; }
.desc_tab1_content { padding:5px; color:grey; }
.price_wrapper{
float: right;
font-weight: bold!important;
text-align:right;
width:100%;
}
</style>
<?php }
add_action('wp_footer', 'add_css');
?>
How to create simple plugin in WORDPRESS Is Very Helpful. Thanks for such Information
ReplyDeleteLooking for WordPress Support Visit :
WordPress Support
WordPress Technical Support
WordPress Support Phone Number
Superb Information
ReplyDeleteWP Support
Awesome Information
ReplyDelete24/7 WordPress Support
24/7 WP Support