Advertisement
ssaidz

novo.php

Dec 6th, 2023 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 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. function custom_url_data_importer_page() {
  39. // Tampilkan form untuk mengubah isi $url_list
  40. ?>
  41. <div class="wrap">
  42. <h2>NovelRingan Data Importer</h2>
  43. <form method="post" action="">
  44. <label for="url_list">Masukkan URL NovelRingan:</label><br>
  45. <textarea id="url_list" name="url_list" style=" min-width:500px; max-width:100%;min-height:250px;height:100%;width:100%;"></textarea><br>
  46. <input type="submit" name="submit_url_list" value="Import Data">
  47. </form>
  48. </div>
  49. <?php
  50.  
  51. if ( isset( $_POST['submit_url_list'] ) ) {
  52. // Jika form disubmit, proses dan simpan data sebagai post baru
  53. $url_list = $_POST['url_list'];
  54. $urls = explode("\n", $url_list);
  55.  
  56. foreach ($urls as $url) {
  57. $data = getDataFromUrl(trim($url));
  58. $post_data = array(
  59. 'post_title' => $data['title'],
  60. 'post_content' => $data['content'],
  61. 'post_status' => 'publish',
  62. 'post_author' => 1
  63. );
  64. wp_insert_post( $post_data );
  65. }
  66. }
  67. }
  68.  
  69. // Fungsi untuk mengambil judul dan konten dari URL
  70. function getDataFromUrl($url) {
  71. $ch = curl_init();
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. $output = curl_exec($ch);
  75. curl_close($ch);
  76.  
  77. // Mengambil judul dari tag <title>
  78. preg_match("/<title>(.*)<\/title>/i", $output, $title);
  79.  
  80. // Mengambil konten dari <div id="nr-nv-content">
  81. preg_match("/<div id=\"nr-nv-content\">(.*?)<\/div>/is", $output, $content);
  82.  
  83. // Mengembalikan judul dan konten
  84. return array("title" => $title[1], "content" => $content[1]);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement