Advertisement
ssaidz

novo2

Dec 7th, 2023 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Importer
  4. Description: Plugin untuk mengambil data dari URL dan menyimpannya sebagai post baru di <link>WordPress</link>.
  5. Version: 1.0
  6. */
  7.  
  8. // Action ketika plugin diaktifkan
  9. register_activation_hook( __FILE__, 'custom_url_data_importer_activate' );
  10.  
  11. function custom_url_data_importer_activate() {
  12. // Buat tabel kustom untuk menyimpan data URL
  13. global $wpdb;
  14. $table_name = $wpdb->prefix . 'custom_urls';
  15. $charset_collate = $wpdb->get_charset_collate();
  16. $sql = "CREATE TABLE $table_name (
  17. id mediumint(9) NOT NULL AUTO_INCREMENT,
  18. url_list text NOT NULL,
  19. PRIMARY KEY (id)
  20. ) $charset_collate;";
  21. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  22. dbDelta( $sql );
  23. }
  24.  
  25. // Tambahkan menu di dashboard untuk plugin
  26. add_action('admin_menu', 'custom_url_data_importer_menu');
  27.  
  28. function custom_url_data_importer_menu() {
  29. add_menu_page(
  30. 'NovelRingan',
  31. 'NovelRingan',
  32. 'manage_options',
  33. 'novelringan',
  34. 'custom_url_data_importer_page'
  35. );
  36. }
  37.  
  38.  
  39.  
  40. function custom_url_data_importer_page() {
  41. // Tampilkan form untuk mengubah isi $url_list
  42. ?>
  43. <div class="wrap">
  44. <h2>NovelRingan Data Importer</h2>
  45. <form method="post" action="">
  46. <label for="url_list"><b>Masukkan URL NovelRingan:</b></label><br>
  47. <textarea id="url_list" name="url_list" style=" min-width:300px; max-width:100%;min-height:250px;height:100%;width:100%;"></textarea><br>
  48. <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;">
  49. </form>
  50. </div>
  51. <?php
  52.  
  53. if ( isset( $_POST['submit_url_list'] ) ) {
  54. // Jika form disubmit, proses dan simpan data sebagai post baru
  55. $url_list = $_POST['url_list'];
  56. //echo $url_list;
  57. $urls = explode("\n", $url_list);
  58.  
  59. foreach ($urls as $url) {
  60. $data = getDataFromUrl(trim($url));
  61. $post_data = array(
  62. 'post_title' => $data['title'],
  63. 'post_content' => $data['content'],
  64. 'post_status' => 'publish',
  65. 'post_author' => 1
  66. );
  67. wp_insert_post( $post_data );
  68. }
  69. }
  70. }
  71.  
  72. // Fungsi untuk mengambil judul dan konten dari URL
  73. function getDataFromUrl($url) {
  74. $ch = curl_init();
  75. curl_setopt($ch, CURLOPT_URL, $url);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  77. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. $output = curl_exec($ch);
  80. curl_close($ch);
  81.  
  82. // Mengambil judul dari tag <title>
  83. preg_match("/<title>(.*)<\/title>/i", $output, $title);
  84. echo '<br/> #'.$title[1];
  85. // Mengambil konten dari <div id="nr-nv-content">
  86. preg_match("/<div id=\"nr-nv-content\">(.*?)<\/div><\/div><\/div><div class=\"nextprev/is", $output, $content);
  87. $konten2 = preg_replace('/<div class="klm_media".*?<\/div>/', '', $content);
  88. //echo $content[1];
  89. // Mengembalikan judul dan konten
  90. return array("title" => $title[1], "content" => $konten2[1]);
  91. }
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement