$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'); ?>plugin development - Use a textarea for a custom post type|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)

plugin development - Use a textarea for a custom post type

matteradmin8PV0评论

I have a custom plugin, using a custom post type for data entry capabilities in the admin section. I would like to use a textarea form, along with the checkboxes, select and text input fields I am presently using. However when I update the bost, my call back receives all the other input fields, not not the textarea field.

The code is rather large, covering lots of different input types, this is a refinement of that draw and save call:

function  CustomInput()
{
add_meta_box( 'List_Group1',
           __( 'Lists - Card Records : Manual Input', 'myplugin_textdomain'  ),
              'DrawCallBack',
              'customlist',
               'normal',
               'high',
               $args
               );           
 }
 add_action( 'add_meta_boxes', 'CustomInput' );
 add_action( 'save_post', 'SaveFields');


function DrawCallBack($post)
{
$Record = GetDBRecord();
echo '<textarea id="bizdesc" rows="2" cols="50">';
echo $Record['BizDescp'];
echo  '</textarea>';

echo '<input type=text id="YourName" name="YourName"  value="' .$Record['Name'] .'"/>'
}


function SaveFields($post_id)
{
$screen = get_current_screen();
if(strcmp($screen->post_type, 'customlist') !=0)
   return;

$Desc = sanitize_text_field( $_POST[ 'bizdesc ' ] ); 
$Name = sanitize_text_field( $_POST[ 'YourName' ] ); 
}

The standard input field comes in nicely and correctly. The textarea field does not. Not sure why?

Any ideas?

I have a custom plugin, using a custom post type for data entry capabilities in the admin section. I would like to use a textarea form, along with the checkboxes, select and text input fields I am presently using. However when I update the bost, my call back receives all the other input fields, not not the textarea field.

The code is rather large, covering lots of different input types, this is a refinement of that draw and save call:

function  CustomInput()
{
add_meta_box( 'List_Group1',
           __( 'Lists - Card Records : Manual Input', 'myplugin_textdomain'  ),
              'DrawCallBack',
              'customlist',
               'normal',
               'high',
               $args
               );           
 }
 add_action( 'add_meta_boxes', 'CustomInput' );
 add_action( 'save_post', 'SaveFields');


function DrawCallBack($post)
{
$Record = GetDBRecord();
echo '<textarea id="bizdesc" rows="2" cols="50">';
echo $Record['BizDescp'];
echo  '</textarea>';

echo '<input type=text id="YourName" name="YourName"  value="' .$Record['Name'] .'"/>'
}


function SaveFields($post_id)
{
$screen = get_current_screen();
if(strcmp($screen->post_type, 'customlist') !=0)
   return;

$Desc = sanitize_text_field( $_POST[ 'bizdesc ' ] ); 
$Name = sanitize_text_field( $_POST[ 'YourName' ] ); 
}

The standard input field comes in nicely and correctly. The textarea field does not. Not sure why?

Any ideas?

Share Improve this question edited Mar 9, 2019 at 1:45 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 9, 2019 at 1:09 Debbie KurthDebbie Kurth 4323 silver badges14 bronze badges 4
  • 3 Your textarea doesn't have a name. – Sally CJ Commented Mar 9, 2019 at 1:12
  • 1 Is there code missing from SaveFields? All it does is assign variables, but I don't see any code that saves those variables to a database/file/etc – Tom J Nowell Commented Mar 9, 2019 at 2:21
  • Tom, no code is missing. The code that pulls the values is the _POST. – Debbie Kurth Commented Mar 9, 2019 at 18:17
  • Found the problem. Partially to Sally, who made me look at the input names more closely. Name mismatch in a another part of the code. Thanks for your help Sally. – Debbie Kurth Commented Mar 9, 2019 at 18:18
Add a comment  | 

2 Answers 2

Reset to default 5

You forgot to add name ="bizdesc" to your textarea, so this

function DrawCallBack($post)
{
  $Record = GetDBRecord();
  echo '<textarea id="bizdesc" rows="2" cols="50">';
  echo $Record['BizDescp'];
  echo  '</textarea>';

  echo '<input type=text id="YourName" name="YourName"  value="' .$Record['Name'] .'"/>'
}

should be

 function DrawCallBack( $post )
 {
  $Record = GetDBRecord();
  echo '<textarea name="bizdesc" id="bizdesc" rows="2" cols="50">';
  echo esc_textarea( $Record['BizDescp'] );
  echo  '</textarea>';

  echo '<input type="text" id="YourName" name="YourName"  value="' .esc_attr( $Record['Name'] ) .'"/>';
}

I hope this helps.

Problem solved. It was a field naming problem.

Post a comment

comment list (0)

  1. No comments so far