TransWikia.com

How we insert values into database using metabox Wordpress?

WordPress Development Asked on December 8, 2021

function Review_Form(){
?>
<form action="" method="post">
  <div class="container">
    <h1>Add Review</h1>

    <hr>
 <label for="psw"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="user_name" id="user" required>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" id="email" required>

   

<label for="email"><b>Phone</b></label>
    <input type="text" placeholder="Enter Phone" name="Phone" id="Phone" required>
<div class="form-group">
    <label for="psw-repeat"><b>Comments</b></label>

    <textarea  rows="4" cols="30" name="review11" type="text" required>
      
      
    </textarea>
    <hr>
    </div>
   
    <button type="submit" class="registerbtn" name="Add_Review">Sumbit</button>
  </div>
  
</form>

<?php

function insert_data(){
    global $wpdb;

//$value=get_post_meta($post->ID,'_Review2_key',true);

if (isset($_POST['Add_Review'])){

    $Name=$_POST['user_name'];
    $Email=$_POST['email'];
    $Contact=$_POST['Phone'];
    $Comments=$_POST['review11'];

One Answer

The following example is adding a meta box to the post edit screen and the wporg_cpt edit screen.

 function wporg_add_custom_box()
  {
     $screens = ['post', 'wporg_cpt'];
     foreach ($screens as $screen) {
        add_meta_box(
        'wporg_box_id',           // Unique ID
        'Custom Meta Box Title',  // Box title
        'wporg_custom_box_html',  // Content callback, must be of type callable
         $screen                   // Post type
         );
        }
    }
    add_action('add_meta_boxes', 'wporg_add_custom_box');

The wporg_custom_box_html function will hold the HTML for the meta box.

The following example is adding form elements, labels, and other HTML elements.

function wporg_custom_box_html($post)
{
    ?>
    <label for="wporg_field">Description for this field</label>
    <select name="wporg_field" id="wporg_field" class="postbox">
        <option value="">Select something...</option>
        <option value="something">Something</option>
        <option value="else">Else</option>
    </select>
    <?php
}

Saving Values The following example will save the wporg_field field value in the _wporg_meta_key meta key, which is hidden.

function wporg_save_postdata($post_id)
{
    if (array_key_exists('wporg_field', $_POST)) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    }
}
add_action('save_post', 'wporg_save_postdata');

follow https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

Answered by Dharmishtha Patel on December 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP