{"id":212,"date":"2020-06-29T12:25:32","date_gmt":"2020-06-29T12:25:32","guid":{"rendered":"https:\/\/codexjunction.com\/?p=212"},"modified":"2023-10-29T16:23:35","modified_gmt":"2023-10-29T16:23:35","slug":"insert-wp-db-in-wordpress","status":"publish","type":"post","link":"https:\/\/codexjunction.com\/insert-wp-db-in-wordpress\/","title":{"rendered":"Insert WP Db in WordPress"},"content":{"rendered":"<p data-sourcepos=\"1:1-1:159\">To insert data into the WordPress database, you can use the <code>wpdb<\/code> object. The <code>wpdb<\/code> object is a global object that provides access to the <a href=\"https:\/\/codexjunction.com\/html-vs-wordpress\/\" rel=\"nofollow\">WordPress database<\/a>.<\/p>\n<p data-sourcepos=\"3:1-3:141\">To insert data into a WordPress database table, you can use the <code>wpdb-&gt;insert()<\/code> function. The <code>wpdb-&gt;insert()<\/code> function takes two arguments:<\/p>\n<ul data-sourcepos=\"5:1-7:0\">\n<li data-sourcepos=\"5:1-5:48\">The name of the table to insert the data into.<\/li>\n<li data-sourcepos=\"6:1-7:0\">An array of the data to insert. The array keys should be the column names and the array values should be the column values.<\/li>\n<\/ul>\n<p data-sourcepos=\"8:1-8:94\">For example, to insert a new post into the <code>wp_posts<\/code> table, you would use the following code:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">global<\/span> <span class=\"hljs-variable\">$wpdb<\/span>;\r\n\r\n<span class=\"hljs-variable\">$post_data<\/span> = <span class=\"hljs-keyword\">array<\/span>(\r\n  <span class=\"hljs-string\">'post_title'<\/span> =&gt; <span class=\"hljs-string\">'My New Post'<\/span>,\r\n  <span class=\"hljs-string\">'post_content'<\/span> =&gt; <span class=\"hljs-string\">'This is the content of my new post.'<\/span>,\r\n);\r\n\r\n<span class=\"hljs-variable\">$wpdb<\/span>-&gt;insert(<span class=\"hljs-string\">'wp_posts'<\/span>, <span class=\"hljs-variable\">$post_data<\/span>);\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"21:1-21:151\">You can also use the <code>wpdb-&gt;prepare()<\/code> function to prepare your SQL queries before executing them. This is a good way to prevent SQL injection attacks.<\/p>\n<p data-sourcepos=\"23:1-23:118\">For example, to insert a new post into the <code>wp_posts<\/code> table using <code>wpdb-&gt;prepare()<\/code>, you would use the following code:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">global<\/span> <span class=\"hljs-variable\">$wpdb<\/span>;\r\n\r\n<span class=\"hljs-variable\">$post_data<\/span> = <span class=\"hljs-keyword\">array<\/span>(\r\n  <span class=\"hljs-string\">'post_title'<\/span> =&gt; <span class=\"hljs-string\">'My New Post'<\/span>,\r\n  <span class=\"hljs-string\">'post_content'<\/span> =&gt; <span class=\"hljs-string\">'This is the content of my new post.'<\/span>,\r\n);\r\n\r\n<span class=\"hljs-variable\">$wpdb<\/span>-&gt;prepare(<span class=\"hljs-string\">'INSERT INTO wp_posts (post_title, post_content) VALUES (%s, %s)'<\/span>, <span class=\"hljs-variable\">$post_data<\/span>[<span class=\"hljs-string\">'post_title'<\/span>], <span class=\"hljs-variable\">$post_data<\/span>[<span class=\"hljs-string\">'post_content'<\/span>]);\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"36:1-36:108\">Once you have executed the <code>wpdb-&gt;insert()<\/code> function, the data will be inserted into the <a href=\"https:\/\/wordpress.com\/\" rel=\"nofollow\">WordPress<\/a> database.<\/p>\n<p data-sourcepos=\"38:1-38:14\"><strong>Important:<\/strong><\/p>\n<ul data-sourcepos=\"40:1-42:0\">\n<li data-sourcepos=\"40:1-40:121\">Be careful when inserting data into the WordPress database. If you insert invalid data, it could corrupt your database.<\/li>\n<li data-sourcepos=\"41:1-42:0\">Always backup your database before making any changes.<\/li>\n<\/ul>\n<p data-sourcepos=\"43:1-43:170\">If you are not sure how to insert data into the WordPress database, you can consult the WordPress documentation or contact your WordPress hosting provider for assistance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To insert data into the WordPress database, you can use the wpdb object. The wpdb object is a global object that provides access to the WordPress database. To insert data into a WordPress database table, you can use the wpdb-&gt;insert() function. The wpdb-&gt;insert() function takes two arguments: The name of the table to insert the data into. An array of the data to insert. The array keys should be the column names and the array values should be the column values. For example, to insert a new post into the wp_posts table, you would use the following code: PHP global [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":877,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[243,512,508,510,511,509,242],"class_list":["post-212","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-wpdb","tag-a-online-course-web-tutorial","tag-learn-java-online","tag-learn-laravel-online","tag-learn-php-online","tag-learn-python-online","tag-wp-db"],"_links":{"self":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":5,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":2645,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/212\/revisions\/2645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media\/877"}],"wp:attachment":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}