我正在编写一个插件来创建custom
段,框和文本块,允许您将数据保存在Wordpress database
中。例如,产品自定义页面,您可以给出值。
我已经制作了三个文件。
它不希望将框和输入字段的值保存到数据库中。
我已经记录了PHP文档中的所有内容,以便您可以更好地帮助我:)(每个人都可以自由提供反馈:))。
我使用类媒体框创建一个名称空间的类。这包括$ name(框的名称),$ post(获取postID)和$ output(生成html)
在构造函数中,我添加$ post,以便它可以在函数中调用它自己。
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_boxes
/**
* @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);
}
}
当我使用costum值调用update_post_meta时(update_post_meta($ this-&gt; post,&#39; _api_UserEnabled&#39;,&#39; aaaaa&#39;);)它不起作用:/。< / p>
/*
Plugin Name: WPAUTH2
Plugin URI: https://xxx
Description: AUTH SERVER
Version: 1.2
Author: Daan Seegers
Author URI: https://xxx
License: GPLv2 or later
*/
require_once 'vendor/autoload.php';
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
new \App\controller\register\new_register_post_type('API', 'oauth-api');
add_action('init', function () {
if (!isset($_GET['post']) || !is_admin()) {
return;
}
$post = get_post(intval($_GET['post']));
\App\controller\build\mediabox::factory($post, 'oauth-api')
->build_textbox('Tralalala')->build_selectbox('brah' , array("gmx", "sss", "ssaa"))->build_radio("hello", array("Volvo", "BMW", "Toyota"));
});
<?php
/**
* Register taxonomies
*/
/**
* Add meta box
*
* @param post $post The post object
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes
*/
namespace App\controller\register;
class new_register_post_type
{
private $name;
private $slug;
public function __construct($name, $slug)
{
$this->name = $name;
$this->slug = $slug;
add_action('init', [$this, 'API_setup']);
}
public function API_setup($name)
{
$labels = array(
'name' => __($this->name, $this->name . '_example_plugin'),
'singular_name' => __($this->name, $this->name . '_example_plugin'),
'add_new_item' => __('Add New ' . $this->name, $this->name . '_example_plugin'),
'edit_item' => __('Edit ' . $this->name, $this->name . '_example_plugin'),
'new_item' => __('New ' . $this->name, $this->name . '_example_plugin'),
'not_found' => __('No ' . $this->name . ' found', $this->name . '_example_plugin'),
'all_items' => __('All ' . $this->name, $this->name . '_example_plugin'),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'has_archive' => true,
'map_meta_cap' => true,
'menu_icon' => 'dashicons-performance',
'supports' => array('title'),
);
register_post_type($this->slug, $args);
}
}
<?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);
}
}
答案 0 :(得分:0)
你需要使用
update_post_meta($this->post->ID, '_api_UserEnabled', 'aaaaa');
而不是
update_post_meta($this->post, '_api_UserEnabled', 'aaaaa');