wp query - insert value from html into data base with wordpress

admin2025-06-02  3

I am writing a simple plugin that can write text in textarea and show it down in another section like a post, i want to save this text into the data base in wp-post table i wrote this :

$posts=$_POST['text-post'];

function insert_post(){
   global $wpdb;
   $table_name = $wpdb -> prefix . "wp_eslam_users";
   $wpdb -> insert ($table_name ,'text-post' -> $post);

}

I am writing a simple plugin that can write text in textarea and show it down in another section like a post, i want to save this text into the data base in wp-post table i wrote this :

$posts=$_POST['text-post'];

function insert_post(){
   global $wpdb;
   $table_name = $wpdb -> prefix . "wp_eslam_users";
   $wpdb -> insert ($table_name ,'text-post' -> $post);

}
Share Improve this question edited Mar 14, 2019 at 11:59 Tanmay Patel 8111 gold badge7 silver badges11 bronze badges asked Mar 14, 2019 at 11:40 rihemrihem 11 bronze badge 4
  • 1 Do you mean you want to create a new post using your textarea as the post's main content? WordPress already supplies a function wp_insert_post that will correctly insert the data for you. – Andy Macaulay-Brook Commented Mar 14, 2019 at 12:03
  • 1 @rihem Want to create new post ? – Pratik Patel Commented Mar 14, 2019 at 12:09
  • like facebook we write post and we share it i want to save it in data base – rihem Commented Mar 14, 2019 at 12:52
  • Note that $wpdb -> prefix is the "wp_", so $wpdb->prefix."wp_eslam_users" == "wp_wp_eslam_users" (with the default prefix and no multisite) – Rup Commented Mar 14, 2019 at 13:09
Add a comment  | 

1 Answer 1

Reset to default 2

If you want to insert content into the posts table, you should create a new custom post type first. Once you have a custom post type, you can follow through with what you're doing above with a couple changes.

<?php
function insert_post() {
    // Check to make sure your content exists.
    if ( ! isset( $_POST['text-post'] ) || empty ( $_POST['text-post'] ) ) {
        return false;
    }

    // Sanitizing user input is extremely important.
    $post_content = sanitize_textarea_field( $_POST['text-post'] );
    // Used as an identifying string for this post. Not required.
    $hash = wp_hash( $post_content );

    // Build the insert post array.
    $post_arr = array(
        'post_status' => 'publish',
        'post_author' => 1, // Set to the ID of author you want to associate this with.
        'post_type'   => 'your_custom_post_type', // Change this to your custom post type slug.
        'post_content' => $post_content,
        // Not required. I like to store a hash of the content to make sure it's only posted once.
        'meta_input' => array(
            $hash => 'import_hash',
        )
    );
    return wp_insert_post( $post_arr, true );
}

The only thing I didn't cover here is where the $_POST content is coming from. I assume you already have that covered. If not, I recommend looking at making an AJAX endpoint.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748806417a313875.html

最新回复(0)