我正在开发一个简单的插件,它可以判断用户是否点击按钮并在WordPress中设置Cookie.一切正常,但当我try 更新帖子或页面时,我得到"更新失败.响应不是有效的JSON响应".我想不出我怎么才能解决这个问题.此外,我还询问我是否进行了正确的过程来挂接翻译并在Worpress init上显示对话框.

if(!defined('ABSPATH')) {
    exit;
}

// ACTION
add_action('wp_enqueue_scripts', 'cp_scripts');

function cp_scripts() {
    wp_enqueue_style('cookie-style', plugins_url('style.css', __FILE__));
    wp_enqueue_script('cookie-script', plugins_url('script.js', __FILE__));
}   

add_action('init', 'my_plugin_show_confirm');
// add_action('init', 'register_pll_translation');
 
function register_pll_translation() {
    if(function_exists('pll_register_string')) {
        pll_register_string( 'cp_form_subtitle', "string", 'cp-form', false );
        pll_register_string( 'cp_form_age_ok', "string", 'cp-form', false );
    }
}

function my_plugin_show_confirm() {
    if (!isset($_COOKIE['cp_enter']) && !is_admin() )  {
    $current_url = get_the_permalink();

    if(isset($_POST['submit'])) {
        setcookie('cp_enter', 1 , time() + (60 * 60 * 24 * 30), COOKIEPATH, COOKIE_DOMAIN);
        return;
    } ?>
   
    <div id="my-plugin-confirm">
        <form action="<?php echo $current_url ?>" method="post">
             <h2><?php echo esc_html(get_bloginfo('name'));?></h2>
             <p><Click to enter</p>
            <button type="submit" name="submit">Enter</button>
        </form>
    </div>

    <?php } ?>

推荐答案

我添加了一个新的函数my_plugin_set_cookie(),该函数与init挂钩.此函数用于判断表单是否已提交并设置Cookie.设置Cookie后,它会将用户重定向到同一页,以避免在用户刷新表单时重新发送表单.my_plugin_show_confirm()函数现在只负责输出表单.

add_action('init', 'my_plugin_set_cookie');
add_action('wp_footer', 'my_plugin_show_confirm');

function my_plugin_set_cookie() {
    if (!isset($_COOKIE['cp_enter']) && !is_admin() && isset($_POST['submit'])) {
        setcookie('cp_enter', 1 , time() + (60 * 60 * 24 * 30), COOKIEPATH, COOKIE_DOMAIN);
        wp_redirect($_SERVER['REQUEST_URI']); // Redirect to the same page to avoid resending the form on refresh
        exit;
    }
}

function my_plugin_show_confirm() {
    if (!isset($_COOKIE['cp_enter']) && !is_admin() )  {
        $current_url = get_the_permalink(); ?>

        <div id="my-plugin-confirm">
            <form action="<?php echo $current_url ?>" method="post">
                <h2><?php echo esc_html(get_bloginfo('name'));?></h2>
                <p>Click to enter</p>
                <button type="submit" name="submit">Enter</button>
            </form>
        </div>

    <?php }
}

Php相关问答推荐

我如何知道BaseController中当前运行的是哪个控制器?

将带有值的属性添加到WooCommerce产品,保留现有属性

如何使用php-amqplib连接到ActiveMQ Classic Docker镜像

如何修复警告:当使用WordPress get_the_Terms()时,Foreach()参数必须是ARRAY|OBJECT类型?

如何实现对数据库表中多列的唯一约束?

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

PHP根据特定索引值从中删除子数组

为什么只有最后一次点击的点赞/心形会按预期改变 colored颜色 ,而其他的保持正常 colored颜色 ?

如何按类别过滤自定义帖子类型

文件::Files()响应返回空对象数组

Woocommerce单一产品Ajax添加到带有自定义购物车项目数据的购物车

将自定义字段添加到管理产品 在 WooCommerce 3.2+ 中批量编辑

在 WooCommerce 中应用优惠券时显示购物车商品折扣金额

WooCommerce 以编程方式添加的费用不会持续存在

过滤会员计划标题和标签

使用 Laravel 和 Vue 进行图像表单验证

在 laravel 中更新表单时如何在 Select 选项中保留旧值?

我需要获取传递到这个数组中的数据,但我没有得到它,因为在 Laravel 中数组是这样出现的

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

使用 GROUP_CONCAT 规范化/转置来自查询的数据