Advertisement
plirof2

wordpress insert article with SQL

Nov 11th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.96 KB | None | 0 0
  1. <?php
  2. // Database credentials
  3. $host = 'localhost'; // Database host
  4. $user = 'username';  // Database user
  5. $password = 'password';  // Database password
  6. $dbname = 'wordpress';  // WordPress database name
  7.  
  8. // Establish connection to MySQL database
  9. $mysqli = new mysqli($host, $user, $password, $dbname);
  10.  
  11. // Check for a connection error
  12. if ($mysqli->connect_error) {
  13.     die("Connection failed: " . $mysqli->connect_error);
  14. }
  15.  
  16. // Prepare the post data
  17. $title = "My New Article";   // Post title
  18. $content = "This is the content of my new article."; // Post content
  19. $status = 'publish';  // Post status (draft, publish, etc.)
  20. $author_id = 1; // The user ID of the author (typically 1 is the admin)
  21.  
  22. // Insert into wp_posts table
  23. $post_sql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_title, post_content, post_status, post_type)
  24.             VALUES (?, NOW(), NOW(), ?, ?, ?, 'post')";
  25.  
  26. $stmt = $mysqli->prepare($post_sql);
  27. $stmt->bind_param("isss", $author_id, $title, $content, $status);
  28. $stmt->execute();
  29.  
  30. if ($stmt->affected_rows > 0) {
  31.     // Get the post ID of the newly inserted post
  32.     $post_id = $stmt->insert_id;
  33.     echo "Post created successfully! Post ID: " . $post_id . "\n";
  34.    
  35.     // Optionally, you can assign a category or tag to the post
  36.     // For simplicity, we're adding a default category (1 is usually the default "Uncategorized" category in WordPress)
  37.    
  38.     // Insert into wp_term_relationships
  39.     $term_taxonomy_id = 1; // Term ID for "Uncategorized" (you can change this to another category ID)
  40.     $term_sql = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (?, ?)";
  41.    
  42.     $term_stmt = $mysqli->prepare($term_sql);
  43.     $term_stmt->bind_param("ii", $post_id, $term_taxonomy_id);
  44.     $term_stmt->execute();
  45.    
  46.     if ($term_stmt->affected_rows > 0) {
  47.         echo "Category assigned successfully!\n";
  48.     } else {
  49.         echo "Error assigning category.\n";
  50.     }
  51.    
  52.     // Optionally, add custom fields (post meta) to the post
  53.     $meta_key = 'custom_meta_key';
  54.     $meta_value = 'custom_meta_value';
  55.    
  56.     $meta_sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (?, ?, ?)";
  57.    
  58.     $meta_stmt = $mysqli->prepare($meta_sql);
  59.     $meta_stmt->bind_param("iss", $post_id, $meta_key, $meta_value);
  60.     $meta_stmt->execute();
  61.    
  62.     if ($meta_stmt->affected_rows > 0) {
  63.         echo "Custom field added successfully!\n";
  64.     } else {
  65.         echo "Error adding custom field.\n";
  66.     }
  67.  
  68. } else {
  69.     echo "Error creating post.\n";
  70. }
  71.  
  72. // Close the database connection
  73. $stmt->close();
  74. $mysqli->close();
  75. ?>
  76.  
  77.  
  78. _____________
  79.  
  80. Sure! You can add an article to a WordPress site by directly inserting data into the WordPress database via SQL. However, doing so bypasses the normal WordPress hooks, actions, and sanitization, so **it's important to be very careful** when directly manipulating the database. The following is a basic example of how to add an article (post) using raw SQL.
  81.  
  82. ### Overview of WordPress Database Structure for Posts
  83.  
  84. WordPress stores posts in the following tables:
  85.  
  86. 1. **`wp_posts`**: This table holds the content of the post.
  87. 2. **`wp_postmeta`**: This table stores meta information (such as custom fields).
  88. 3. **`wp_terms`**: This table stores categories, tags, and other taxonomy information.
  89. 4. **`wp_term_taxonomy`**: This table relates terms (categories, tags) to specific taxonomies.
  90. 5. **`wp_term_relationships`**: This table links posts to their associated terms (categories, tags).
  91.  
  92. ### SQL Structure
  93.  
  94. 1. **Insert a post** into `wp_posts`.
  95. 2. Optionally, **assign categories/tags** to the post (you need to insert into `wp_term_taxonomy`, `wp_terms`, and `wp_term_relationships`).
  96. 3. **Add custom fields** (if necessary) in `wp_postmeta`.
  97.  
  98. Here’s a basic example of a PHP script that inserts a new post into WordPress using SQL.
  99.  
  100. ### PHP Script to Insert a WordPress Post Using SQL
  101.  
  102. ```php
  103. <?php
  104. // Database credentials
  105. $host = 'localhost'; // Database host
  106. $user = 'username';  // Database user
  107. $password = 'password';  // Database password
  108. $dbname = 'wordpress';  // WordPress database name
  109.  
  110. // Establish connection to MySQL database
  111. $mysqli = new mysqli($host, $user, $password, $dbname);
  112.  
  113. // Check for a connection error
  114. if ($mysqli->connect_error) {
  115.     die("Connection failed: " . $mysqli->connect_error);
  116. }
  117.  
  118. // Prepare the post data
  119. $title = "My New Article";   // Post title
  120. $content = "This is the content of my new article."; // Post content
  121. $status = 'publish';  // Post status (draft, publish, etc.)
  122. $author_id = 1; // The user ID of the author (typically 1 is the admin)
  123.  
  124. // Insert into wp_posts table
  125. $post_sql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_title, post_content, post_status, post_type)
  126.             VALUES (?, NOW(), NOW(), ?, ?, ?, 'post')";
  127.  
  128. $stmt = $mysqli->prepare($post_sql);
  129. $stmt->bind_param("isss", $author_id, $title, $content, $status);
  130. $stmt->execute();
  131.  
  132. if ($stmt->affected_rows > 0) {
  133.     // Get the post ID of the newly inserted post
  134.     $post_id = $stmt->insert_id;
  135.     echo "Post created successfully! Post ID: " . $post_id . "\n";
  136.    
  137.     // Optionally, you can assign a category or tag to the post
  138.     // For simplicity, we're adding a default category (1 is usually the default "Uncategorized" category in WordPress)
  139.    
  140.     // Insert into wp_term_relationships
  141.     $term_taxonomy_id = 1; // Term ID for "Uncategorized" (you can change this to another category ID)
  142.     $term_sql = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (?, ?)";
  143.    
  144.     $term_stmt = $mysqli->prepare($term_sql);
  145.     $term_stmt->bind_param("ii", $post_id, $term_taxonomy_id);
  146.     $term_stmt->execute();
  147.    
  148.     if ($term_stmt->affected_rows > 0) {
  149.         echo "Category assigned successfully!\n";
  150.     } else {
  151.         echo "Error assigning category.\n";
  152.     }
  153.    
  154.     // Optionally, add custom fields (post meta) to the post
  155.     $meta_key = 'custom_meta_key';
  156.     $meta_value = 'custom_meta_value';
  157.    
  158.     $meta_sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (?, ?, ?)";
  159.    
  160.     $meta_stmt = $mysqli->prepare($meta_sql);
  161.     $meta_stmt->bind_param("iss", $post_id, $meta_key, $meta_value);
  162.     $meta_stmt->execute();
  163.    
  164.     if ($meta_stmt->affected_rows > 0) {
  165.         echo "Custom field added successfully!\n";
  166.     } else {
  167.         echo "Error adding custom field.\n";
  168.     }
  169.  
  170. } else {
  171.     echo "Error creating post.\n";
  172. }
  173.  
  174. // Close the database connection
  175. $stmt->close();
  176. $mysqli->close();
  177. ?>
  178. ```
  179.  
  180. ### How the Script Works:
  181.  
  182. 1. **Database Connection**: The script starts by connecting to the MySQL database using the `mysqli` extension.
  183.    
  184. 2. **Insert Post into `wp_posts`**:
  185.    - The post is inserted into the `wp_posts` table with the necessary information such as the post's title, content, status, and the author.
  186.    - The `NOW()` function is used to insert the current date and time into `post_date` and `post_date_gmt`.
  187.  
  188. 3. **Assign a Category**:
  189.    - The script inserts a record into `wp_term_relationships`, associating the newly created post with a category (in this case, the default category with `term_taxonomy_id = 1`).
  190.    - If you want to assign custom categories or tags, you would need to first ensure those terms exist in the `wp_terms` table and have the appropriate `term_taxonomy_id` in `wp_term_taxonomy`.
  191.  
  192. 4. **Add Custom Fields** (optional):
  193.    - The script adds a custom meta field (`meta_key = 'custom_meta_key'`, `meta_value = 'custom_meta_value'`) into the `wp_postmeta` table, associating it with the post.
  194.  
  195. ### Important Notes:
  196.  
  197. - **Security**: Be sure to sanitize and validate any data coming from user input. SQL injection can occur if you are not careful with how you handle data, especially when it's coming from user input. This script uses prepared statements, which helps prevent SQL injection, but make sure to validate all inputs in real-world scenarios.
  198.  
  199. - **WordPress Hooks**: Directly inserting posts into the database bypasses WordPress' internal hooks, actions, and filters, meaning things like post metadata, post formats, and custom taxonomies may not work as expected unless you insert all the relevant data. It’s often better to use WordPress functions like `wp_insert_post()` if possible.
  200.  
  201. - **Default Categories**: In this example, the post is assigned to the default category (typically with `term_taxonomy_id = 1`, but you can verify it using the `wp_terms` table). If you want to add the post to custom categories or tags, you need to insert records into the `wp_terms`, `wp_term_taxonomy`, and `wp_term_relationships` tables accordingly.
  202.  
  203. ### Conclusion
  204.  
  205. While direct SQL inserts can be useful in certain scenarios (e.g., bulk importing posts), **it's highly recommended** to use WordPress's built-in functions like `wp_insert_post()` when possible, as they handle many internal tasks that might be missed in direct SQL (such as cache clearing, taxonomies, and custom fields).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement