$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - insert value from html into data base with wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far