Php 自定义字段(在自定义页面中)未保存(wordpress)(底部的完整代码)
我正在编写一个插件来创建Php 自定义字段(在自定义页面中)未保存(wordpress)(底部的完整代码),php,wordpress,Php,Wordpress,我正在编写一个插件来创建自定义的段塞、方框和文本块,允许您将数据保存在Wordpress数据库中。例如,产品自定义页面,您可以为其提供值 我做了三份档案 插件主文件(仅显示mediabox.php代码) 媒体盒 注册\新\后类型 这一刻,我正在关注mediabox.php代码。 它不希望将框和输入字段的值保存到数据库中 我已经在PHP文档中记录了所有内容,以便您能够更好地帮助我:)(每个人都可以免费提供反馈:) 我用带有类媒体框的名称空间启动类。其中包括$name(框的名称)、$post(获
自定义的
段塞、方框和文本块,允许您将数据保存在Wordpress数据库中
。例如,产品自定义页面,您可以为其提供值
我做了三份档案
- 插件主文件(仅显示mediabox.php代码)
- 媒体盒
- 注册\新\后类型
namespace App\controller\build;
class mediabox
{
protected $name;
protected $post;
protected $output = "";
/**
* mediabox constructor.
* @param $name
*/
public function __construct($post, $name)
{
// In de __construct roepen wij de add_meta_boxes aan, die de boxen dienen aan te maken
$this->name = $name;
$this->post = $post;
add_action('add_meta_boxes', [$this, 'api_add_meta_boxes']);
//Save function
if (!isset($_POST['api_meta_box_nonce']) || !wp_verify_nonce($_POST['api_meta_box_nonce'], basename(__FILE__))) {
return;
}
// return if autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check the user's permissions.
if (!current_user_can('edit_post', $post)) {
return;
}
update_post_meta($this->post, '_api_UserEnabled', 'aaaaa');
}
在上面的小代码中,我调用了一个无法正常工作的更新函数
/**
* @param $name
* @return mediabox
* Hier wordt de factorie functie aangeroepen
*/
public static function factory($post, $name)
{
return new self($post, $name);
}
此外,我还创建了一个称为自己的工厂
在此之后,我称之为add_meta_框
/**
* @param $name
*/
public function api_add_meta_boxes($name)
{
// Roept api_meta_box aan
add_meta_box('api_meta_box', __($name, 'api_example_plugin'), [$this, 'api_build_meta_box'], $this->name, 'normal', 'high');
wp_nonce_field(basename(__FILE__), 'api_meta_box_nonce');
}
最后,我制作了生成框和文本字段的函数
/**
* @param $post
* @param $build_name
* @return $this
* In deze functie kan een radiobutton worden aangemaakt, hier dient een array aan toegevoegd te worden
*/
public function build_radio($build_name, $array)
{
$current_UserEnabled = get_post_meta($this->post->ID, '_api_UserEnabled', true);
$this->output .= "
<h3>{$build_name} </h3>";
foreach ($array as $key) {
$this->output .= "
<input type='radio' name='{$build_name}' value = '{$key}'" . checked($current_UserEnabled, $key) . ">{$key}<br>";
?>
</div>
<?php
# code...
}
return $this;
}
/**
* @param $post
* @param $build_name
* @return $this
*/
public function build_textbox($build_name)
{
$current_Username = get_post_meta($this->post->ID, $build_name, true);
$this->output .= "<h3>{$build_name}</h3><p><input type='text' name='{$build_name}' value='{$current_Username}'/></p>";
return $this;
}
/**
* @param $post
* @param $build_name
* @return $this
*/
public function build_selectbox($build_name, $userinfo)
{
$this->output .= "
<h3>{$build_name} </h3>";
foreach ($userinfo as $myuserinfo) {
$this->output .= "<p>{$myuserinfo}</p>";
$this->output .= "<input type=checkbox name='{$build_name}' value='{$myuserinfo}' " . checked((in_array($build_name, $myuserinfo)) ? $myuserinfo : '', $myuserinfo) . ">";
}
?>
</p>
<?php
return $this;
}
/**
* @param $post
* @return $this
*/
public function api_build_meta_box($post)
{
echo $this->output;
return $this;
var_dump($_post);
}
}
新建\u register\u post\u type.php
您需要使用
update_post_meta($this->post->ID, '_api_UserEnabled', 'aaaaa');
而不是
update_post_meta($this->post, '_api_UserEnabled', 'aaaaa');
<?php
namespace App\controller\build;
class mediabox
{
protected $name;
protected $post;
protected $output = "";
/**
* mediabox constructor.
* @param $name
*/
public function __construct($post, $name)
{
// In de __construct roepen wij de add_meta_boxes aan, die de boxen dienen aan te maken
$this->name = $name;
$this->post = $post;
add_action('add_meta_boxes', [$this, 'api_add_meta_boxes']);
//Save function
if (!isset($_POST['api_meta_box_nonce']) || !wp_verify_nonce($_POST['api_meta_box_nonce'], basename(__FILE__))) {
return;
}
// return if autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check the user's permissions.
if (!current_user_can('edit_post', $post)) {
return;
}
update_post_meta($this->post->ID, '_api_UserEnabled', 'aaaaa');
}
/**
* @param $name
* @return mediabox
* Hier wordt de factorie functie aangeroepen
*/
public static function factory($post, $name)
{
return new self($post, $name);
}
/**
* @param $name
*/
public function api_add_meta_boxes($name)
{
// Roept api_meta_box aan
add_meta_box('api_meta_box', __($name, 'api_example_plugin'), [$this, 'api_build_meta_box'], $this->name, 'normal', 'high');
wp_nonce_field(basename(__FILE__), 'api_meta_box_nonce');
}
/**
* @param $post
* @param $build_name
* @return $this
* In deze functie kan een radiobutton worden aangemaakt, hier dient een array aan toegevoegd te worden
*/
public function build_radio($build_name, $array)
{
$current_UserEnabled = get_post_meta($this->post->ID, '_api_UserEnabled', true);
$this->output .= "
<h3>{$build_name} </h3>";
foreach ($array as $key) {
$this->output .= "
<input type='radio' name='{$build_name}' value = '{$key}'" . checked($current_UserEnabled, $key) . ">{$key}<br>";
?>
</div>
<?php
# code...
}
return $this;
}
/**
* @param $post
* @param $build_name
* @return $this
*/
public function build_textbox($build_name)
{
$current_Username = get_post_meta($this->post->ID, $build_name, true);
$this->output .= "<h3>{$build_name}</h3><p><input type='text' name='{$build_name}' value='{$current_Username}'/></p>";
return $this;
}
/**
* @param $post
* @param $build_name
* @return $this
*/
public function build_selectbox($build_name, $userinfo)
{
$this->output .= "
<h3>{$build_name} </h3>";
foreach ($userinfo as $myuserinfo) {
$this->output .= "<p>{$myuserinfo}</p>";
$this->output .= "<input type=checkbox name='{$build_name}' value='{$myuserinfo}' " . checked((in_array($build_name, $myuserinfo)) ? $myuserinfo : '', $myuserinfo) . ">";
}
?>
</p>
<?php
return $this;
}
/**
* @param $post
* @return $this
*/
public function api_build_meta_box($post)
{
echo $this->output;
return $this;
var_dump($_post);
}
}
update_post_meta($this->post->ID, '_api_UserEnabled', 'aaaaa');
update_post_meta($this->post, '_api_UserEnabled', 'aaaaa');