Last active
August 23, 2018 05:23
-
-
Save hissy/1ea54a46fd07be9f4334 to your computer and use it in GitHub Desktop.
[Really Simple CSV Importer] An example code to replace save_post method of Really Simple CSV Importer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Replace save_post method of Really Simple CSV Importer | |
Description: This is an example add-on. | |
Author: Takuro Hishikawa | |
Version: 0.2 | |
*/ | |
class rscsvimporter_replace_save_post { | |
// singleton instance | |
private static $instance; | |
public static function instance() { | |
if ( isset( self::$instance ) ) | |
return self::$instance; | |
self::$instance = new rscsvimporter_replace_save_post; | |
self::$instance->run_init(); | |
return self::$instance; | |
} | |
public function __construct() { | |
/** Do nothing **/ | |
} | |
protected function run_init() { | |
add_action( 'init', array( $this, 'add_filter' ) ); | |
} | |
public function add_filter() { | |
add_filter( 'really_simple_csv_importer_class', array( $this, 'return_class')); | |
} | |
public function return_class() { | |
return get_class($this); | |
} | |
public function save_post($post, $meta, $terms, $thumbnail, $is_update) { | |
if (!class_exists('wp_post_helper',false)) { | |
return false; | |
} | |
$ph = new wp_post_helper($post); | |
foreach ($meta as $key => $value) { | |
// Add custom field if the given key already exists. | |
// The default of third variable in Really Simple CSV Importer is true | |
$ph->add_meta($key, $value, true); | |
} | |
foreach ($terms as $key => $value) { | |
$ph->add_terms($key, $value); | |
} | |
if ($thumbnail) $ph->add_media($thumbnail,'','','',true); | |
if ($is_update) | |
$result = $ph->update(); | |
else | |
$result = $ph->insert(); | |
// Set default post thumbnail (just an idea) | |
if ($result && !$thumbnail) { | |
set_post_thumbnail($ph->postid(),11); | |
} | |
unset($ph); | |
return $result; | |
} | |
} | |
$rscsvimporter_replace_save_post = rscsvimporter_replace_save_post::instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Takuro!
Thanks a lot for the plugin!
I did more research in this direction and discovered one flaw:
I can't pass my error message to show it in admin-panel after import CSV.
If I don't want to save some post with rscsvimporter_replace_save_post then I see only the message: "An error occurred while saving the post to database.". It would be nice to show my own message.
Or may be there is a way to do it?
Thanks in advance for your support!