Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Database credentials
- $host = 'localhost'; // Database host
- $user = 'username'; // Database user
- $password = 'password'; // Database password
- $dbname = 'wordpress'; // WordPress database name
- // Establish connection to MySQL database
- $mysqli = new mysqli($host, $user, $password, $dbname);
- // Check for a connection error
- if ($mysqli->connect_error) {
- die("Connection failed: " . $mysqli->connect_error);
- }
- // Prepare the post data
- $title = "My New Article"; // Post title
- $content = "This is the content of my new article."; // Post content
- $status = 'publish'; // Post status (draft, publish, etc.)
- $author_id = 1; // The user ID of the author (typically 1 is the admin)
- // Insert into wp_posts table
- $post_sql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_title, post_content, post_status, post_type)
- VALUES (?, NOW(), NOW(), ?, ?, ?, 'post')";
- $stmt = $mysqli->prepare($post_sql);
- $stmt->bind_param("isss", $author_id, $title, $content, $status);
- $stmt->execute();
- if ($stmt->affected_rows > 0) {
- // Get the post ID of the newly inserted post
- $post_id = $stmt->insert_id;
- echo "Post created successfully! Post ID: " . $post_id . "\n";
- // Optionally, you can assign a category or tag to the post
- // For simplicity, we're adding a default category (1 is usually the default "Uncategorized" category in WordPress)
- // Insert into wp_term_relationships
- $term_taxonomy_id = 1; // Term ID for "Uncategorized" (you can change this to another category ID)
- $term_sql = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (?, ?)";
- $term_stmt = $mysqli->prepare($term_sql);
- $term_stmt->bind_param("ii", $post_id, $term_taxonomy_id);
- $term_stmt->execute();
- if ($term_stmt->affected_rows > 0) {
- echo "Category assigned successfully!\n";
- } else {
- echo "Error assigning category.\n";
- }
- // Optionally, add custom fields (post meta) to the post
- $meta_key = 'custom_meta_key';
- $meta_value = 'custom_meta_value';
- $meta_sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (?, ?, ?)";
- $meta_stmt = $mysqli->prepare($meta_sql);
- $meta_stmt->bind_param("iss", $post_id, $meta_key, $meta_value);
- $meta_stmt->execute();
- if ($meta_stmt->affected_rows > 0) {
- echo "Custom field added successfully!\n";
- } else {
- echo "Error adding custom field.\n";
- }
- } else {
- echo "Error creating post.\n";
- }
- // Close the database connection
- $stmt->close();
- $mysqli->close();
- ?>
- _____________
- 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.
- ### Overview of WordPress Database Structure for Posts
- WordPress stores posts in the following tables:
- 1. **`wp_posts`**: This table holds the content of the post.
- 2. **`wp_postmeta`**: This table stores meta information (such as custom fields).
- 3. **`wp_terms`**: This table stores categories, tags, and other taxonomy information.
- 4. **`wp_term_taxonomy`**: This table relates terms (categories, tags) to specific taxonomies.
- 5. **`wp_term_relationships`**: This table links posts to their associated terms (categories, tags).
- ### SQL Structure
- 1. **Insert a post** into `wp_posts`.
- 2. Optionally, **assign categories/tags** to the post (you need to insert into `wp_term_taxonomy`, `wp_terms`, and `wp_term_relationships`).
- 3. **Add custom fields** (if necessary) in `wp_postmeta`.
- Here’s a basic example of a PHP script that inserts a new post into WordPress using SQL.
- ### PHP Script to Insert a WordPress Post Using SQL
- ```php
- <?php
- // Database credentials
- $host = 'localhost'; // Database host
- $user = 'username'; // Database user
- $password = 'password'; // Database password
- $dbname = 'wordpress'; // WordPress database name
- // Establish connection to MySQL database
- $mysqli = new mysqli($host, $user, $password, $dbname);
- // Check for a connection error
- if ($mysqli->connect_error) {
- die("Connection failed: " . $mysqli->connect_error);
- }
- // Prepare the post data
- $title = "My New Article"; // Post title
- $content = "This is the content of my new article."; // Post content
- $status = 'publish'; // Post status (draft, publish, etc.)
- $author_id = 1; // The user ID of the author (typically 1 is the admin)
- // Insert into wp_posts table
- $post_sql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_title, post_content, post_status, post_type)
- VALUES (?, NOW(), NOW(), ?, ?, ?, 'post')";
- $stmt = $mysqli->prepare($post_sql);
- $stmt->bind_param("isss", $author_id, $title, $content, $status);
- $stmt->execute();
- if ($stmt->affected_rows > 0) {
- // Get the post ID of the newly inserted post
- $post_id = $stmt->insert_id;
- echo "Post created successfully! Post ID: " . $post_id . "\n";
- // Optionally, you can assign a category or tag to the post
- // For simplicity, we're adding a default category (1 is usually the default "Uncategorized" category in WordPress)
- // Insert into wp_term_relationships
- $term_taxonomy_id = 1; // Term ID for "Uncategorized" (you can change this to another category ID)
- $term_sql = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (?, ?)";
- $term_stmt = $mysqli->prepare($term_sql);
- $term_stmt->bind_param("ii", $post_id, $term_taxonomy_id);
- $term_stmt->execute();
- if ($term_stmt->affected_rows > 0) {
- echo "Category assigned successfully!\n";
- } else {
- echo "Error assigning category.\n";
- }
- // Optionally, add custom fields (post meta) to the post
- $meta_key = 'custom_meta_key';
- $meta_value = 'custom_meta_value';
- $meta_sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (?, ?, ?)";
- $meta_stmt = $mysqli->prepare($meta_sql);
- $meta_stmt->bind_param("iss", $post_id, $meta_key, $meta_value);
- $meta_stmt->execute();
- if ($meta_stmt->affected_rows > 0) {
- echo "Custom field added successfully!\n";
- } else {
- echo "Error adding custom field.\n";
- }
- } else {
- echo "Error creating post.\n";
- }
- // Close the database connection
- $stmt->close();
- $mysqli->close();
- ?>
- ```
- ### How the Script Works:
- 1. **Database Connection**: The script starts by connecting to the MySQL database using the `mysqli` extension.
- 2. **Insert Post into `wp_posts`**:
- - The post is inserted into the `wp_posts` table with the necessary information such as the post's title, content, status, and the author.
- - The `NOW()` function is used to insert the current date and time into `post_date` and `post_date_gmt`.
- 3. **Assign a Category**:
- - 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`).
- - 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`.
- 4. **Add Custom Fields** (optional):
- - 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.
- ### Important Notes:
- - **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.
- - **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.
- - **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.
- ### Conclusion
- 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