Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Importer
- Description: Plugin untuk mengambil data dari URL dan menyimpannya sebagai post baru di <link>WordPress</link>.
- Version: 1.0
- */
- // Action ketika plugin diaktifkan
- register_activation_hook( __FILE__, 'custom_url_data_importer_activate' );
- function custom_url_data_importer_activate() {
- // Buat tabel kustom untuk menyimpan data URL
- global $wpdb;
- $table_name = $wpdb->prefix . 'custom_urls';
- $charset_collate = $wpdb->get_charset_collate();
- $sql = "CREATE TABLE $table_name (
- id mediumint(9) NOT NULL AUTO_INCREMENT,
- url_list text NOT NULL,
- PRIMARY KEY (id)
- ) $charset_collate;";
- require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
- dbDelta( $sql );
- }
- // Tambahkan menu di dashboard untuk plugin
- add_action('admin_menu', 'custom_url_data_importer_menu');
- function custom_url_data_importer_menu() {
- add_menu_page(
- 'NovelRingan',
- 'NovelRingan',
- 'manage_options',
- 'novelringan',
- 'custom_url_data_importer_page'
- );
- }
- function custom_url_data_importer_page() {
- // Tampilkan form untuk mengubah isi $url_list
- ?>
- <div class="wrap">
- <h2>NovelRingan Data Importer</h2>
- <form method="post" action="">
- <label for="url_list"><b>Masukkan URL NovelRingan:</b></label><br>
- <textarea id="url_list" name="url_list" style=" min-width:300px; max-width:100%;min-height:250px;height:100%;width:100%;"></textarea><br>
- <input type="submit" name="submit_url_list" value="Import Data" style="width:70%;margin: 15px auto; display:block;padding: 8px;background:blue ;color:white;border:2px solid lime;border-radius:5px;">
- </form>
- </div>
- <?php
- if ( isset( $_POST['submit_url_list'] ) ) {
- // Jika form disubmit, proses dan simpan data sebagai post baru
- $url_list = $_POST['url_list'];
- //echo $url_list;
- $urls = explode("\n", $url_list);
- foreach ($urls as $url) {
- $data = getDataFromUrl(trim($url));
- $post_data = array(
- 'post_title' => $data['title'],
- 'post_content' => $data['content'],
- 'post_status' => 'publish',
- 'post_author' => 1
- );
- wp_insert_post( $post_data );
- }
- }
- }
- // Fungsi untuk mengambil judul dan konten dari URL
- function getDataFromUrl($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $output = curl_exec($ch);
- curl_close($ch);
- // Mengambil judul dari tag <title>
- preg_match("/<title>(.*)<\/title>/i", $output, $title);
- echo '<br/> #'.$title[1];
- // Mengambil konten dari <div id="nr-nv-content">
- preg_match("/<div id=\"nr-nv-content\">(.*?)<\/div><\/div><\/div><div class=\"nextprev/is", $output, $content);
- $konten2 = preg_replace('/<div class="klm_media".*?<\/div>/', '', $content);
- //echo $content[1];
- // Mengembalikan judul dan konten
- return array("title" => $title[1], "content" => $konten2[1]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement