save_post 操作未在 wordpress 中触发
save_post action isnt triggered in wordpress
我正在开发一个主题,其中包含将管理面板中的自定义字段添加到所有页面的逻辑,但我已包含在排除数组中的页面除外。这由函数 showSection() 显示。它工作正常,但似乎 save_post_page 操作未触发,因此数据未更新:
function __construct() {
if ($this->showSection()) {
$this->title = get_post_meta(get_the_ID(), 'lorraine_landing_title', true);
$this->subtitle = get_post_meta(get_the_ID(), 'lorraine_landing_subtitle', true);
$this->addAction('add_meta_boxes_page', array($this, 'addMetaBox') );
$this->addAction('save_post_page', array($this, 'saveMetaBox') );
}
}
public function showSection() {
if (isset($_GET['post'])) {
$slug = get_post_field('post_name', $_GET['post']);
return !in_array($slug, $this->pageWithoutSections);
}
}
function saveMetaBox($postId) {
if (isset($_POST['lorraine_landing_title'])) {
check_admin_referer('lorraine_admin_landing_save', 'lorraine_admin_landing_nonce_field');
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
//Save data
update_post_meta($postId, 'lorraine_landing_title', sanitize_text_field( $_POST['lorraine_landing_title']));
update_post_meta($postId, 'lorraine_landing_subtitle', sanitize_text_field( $_POST['lorraine_landing_subtitle']));
}
}
如果我不调用 showSection() save_post_page 将触发操作。似乎 $_GET['post'] 导致了冲突,但我需要获取页面 ID 来显示或隐藏部分,而且我无法访问 get_the_ID() 函数。
更新
get_post_meta 工作是因为我有另一个函数 printTemplate 需要 metabox html 文件并在其中实例化 class:
function addMetaBox() {
add_meta_box('lorraine-admin-landing', __('Landing'), array($this, 'printTemplate'), 'page', 'normal', 'default');
}
function printTemplate($post, $box) {
require_once( get_stylesheet_directory() . '/components/admin/landing/landing-html.php');
}
登陆-html.php文件
<?php $adminLanding = new AdminLanding(); ?>
<?php wp_nonce_field('lorraine_admin_landing_save', 'lorraine_admin_landing_nonce_field'); ?>
<div class="form__item">
<label for="name"><?php _e('Título', 'lorraine'); ?></label>
<input name="lorraine_landing_title" type="text" class="form__input" value="<?php echo $adminLanding->getTitle(); ?>" maxlength="60" placeholder="<?php _e('Introduce el título de la landing', 'lorraine'); ?>">
</div>
<div class="form__item">
<label for="name"><?php _e('Subtítulo', 'lorraine'); ?></label>
<input name="lorraine_landing_subtitle" type="text" class="form__input" value="<?php echo $adminLanding->getSubtitle(); ?>" maxlength="60" placeholder="<?php _e('Introduce el subtítulo de la landing', 'lorraine'?>">
</div>
更新 2
所以...在操作中挂钩“add_meta_boxes_page”get_the_ID() 可用并且在代码中这样写:
add_meta_boxes
Runs when "edit post" page loads.
“编辑 post”页面加载哪个挂钩?
https://codex.wordpress.org/Plugin_API/Action_Reference#Administrative_Actions
解法:
从构造函数中删除 showSection 方法并将其添加到 addMetabox 中:
function __construct() {
$this->title = get_post_meta(get_the_ID(), 'lorraine_landing_title', true);
$this->subtitle = get_post_meta(get_the_ID(), 'lorraine_landing_subtitle', true);
$this->addAction('add_meta_boxes_page', array($this, 'addMetaBox') );
$this->addAction('save_post_page', array($this, 'saveMetaBox') );
}
function addMetaBox() {
if ($this->showSection()) {
add_meta_box('lorraine-admin-landing', __('Landing'), array($this, 'printTemplate'), 'page', 'normal', 'default');
}
}
我正在开发一个主题,其中包含将管理面板中的自定义字段添加到所有页面的逻辑,但我已包含在排除数组中的页面除外。这由函数 showSection() 显示。它工作正常,但似乎 save_post_page 操作未触发,因此数据未更新:
function __construct() {
if ($this->showSection()) {
$this->title = get_post_meta(get_the_ID(), 'lorraine_landing_title', true);
$this->subtitle = get_post_meta(get_the_ID(), 'lorraine_landing_subtitle', true);
$this->addAction('add_meta_boxes_page', array($this, 'addMetaBox') );
$this->addAction('save_post_page', array($this, 'saveMetaBox') );
}
}
public function showSection() {
if (isset($_GET['post'])) {
$slug = get_post_field('post_name', $_GET['post']);
return !in_array($slug, $this->pageWithoutSections);
}
}
function saveMetaBox($postId) {
if (isset($_POST['lorraine_landing_title'])) {
check_admin_referer('lorraine_admin_landing_save', 'lorraine_admin_landing_nonce_field');
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
//Save data
update_post_meta($postId, 'lorraine_landing_title', sanitize_text_field( $_POST['lorraine_landing_title']));
update_post_meta($postId, 'lorraine_landing_subtitle', sanitize_text_field( $_POST['lorraine_landing_subtitle']));
}
}
如果我不调用 showSection() save_post_page 将触发操作。似乎 $_GET['post'] 导致了冲突,但我需要获取页面 ID 来显示或隐藏部分,而且我无法访问 get_the_ID() 函数。
更新
get_post_meta 工作是因为我有另一个函数 printTemplate 需要 metabox html 文件并在其中实例化 class:
function addMetaBox() {
add_meta_box('lorraine-admin-landing', __('Landing'), array($this, 'printTemplate'), 'page', 'normal', 'default');
}
function printTemplate($post, $box) {
require_once( get_stylesheet_directory() . '/components/admin/landing/landing-html.php');
}
登陆-html.php文件
<?php $adminLanding = new AdminLanding(); ?>
<?php wp_nonce_field('lorraine_admin_landing_save', 'lorraine_admin_landing_nonce_field'); ?>
<div class="form__item">
<label for="name"><?php _e('Título', 'lorraine'); ?></label>
<input name="lorraine_landing_title" type="text" class="form__input" value="<?php echo $adminLanding->getTitle(); ?>" maxlength="60" placeholder="<?php _e('Introduce el título de la landing', 'lorraine'); ?>">
</div>
<div class="form__item">
<label for="name"><?php _e('Subtítulo', 'lorraine'); ?></label>
<input name="lorraine_landing_subtitle" type="text" class="form__input" value="<?php echo $adminLanding->getSubtitle(); ?>" maxlength="60" placeholder="<?php _e('Introduce el subtítulo de la landing', 'lorraine'?>">
</div>
更新 2
所以...在操作中挂钩“add_meta_boxes_page”get_the_ID() 可用并且在代码中这样写:
add_meta_boxes
Runs when "edit post" page loads.
“编辑 post”页面加载哪个挂钩?
https://codex.wordpress.org/Plugin_API/Action_Reference#Administrative_Actions
解法:
从构造函数中删除 showSection 方法并将其添加到 addMetabox 中:
function __construct() {
$this->title = get_post_meta(get_the_ID(), 'lorraine_landing_title', true);
$this->subtitle = get_post_meta(get_the_ID(), 'lorraine_landing_subtitle', true);
$this->addAction('add_meta_boxes_page', array($this, 'addMetaBox') );
$this->addAction('save_post_page', array($this, 'saveMetaBox') );
}
function addMetaBox() {
if ($this->showSection()) {
add_meta_box('lorraine-admin-landing', __('Landing'), array($this, 'printTemplate'), 'page', 'normal', 'default');
}
}