【问题标题】:how ever I disabled autosave draft but still it works我如何禁用自动保存草稿但它仍然有效
【发布时间】:2020-06-15 20:26:00
【问题描述】:

我正在编写一个 wordpress 函数来在帖子发布后触发一个函数我正在使用 add_action("save_post", "") 但我只希望我的函数仅在用户只点击发布按钮

这是我写的

add_action("save_post", "test");

    function test( $post_id ) {
    if( !(wp_is_post_revision( $post_id ) ) || wp_is_post_autosave( $post_id ) ) {
        return;
    }
        $post_log = get_stylesheet_directory() . "/post.txt";
        $message =  get_the_title($post_id) . ' was saved';

        if( file_exists ( $post_log )) {
            $file = fopen( $post_log, 'a');
            fwrite($file, $message."\n");
        } else {
            $file = fopen( $post_log, "w");
            fwrite($file, $message."\n");
        }
        fclose($file);

}

但是通过此代码,我的 post.txt 文件中的每个帖子都会保存两次结果,其中一个用于自动保存草稿,另一个用于常规保存。

【问题讨论】:

  • 你在哪里运行这个?直接在浏览器中?在 Node.js 中?在浏览器中但使用 Webpack 之类的东西进行转换?其他地方?
  • 所以“直接在浏览器中”。
  • 但是,当我尝试使用 parcel localhost 打开的 html 格式的相同文件时,它可以正常工作,但在任何其他服务器上都无法正常工作
  • 您使用的是什么版本的 GSAP? GSAP installation page 和其中的视频很可能可以帮助您正确导入 GSAP,尤其是安装助手。

标签: wordpress hook


【解决方案1】:

浏览器不是 Node.js。网站是通过 HTTP 加载的,而不是从文件系统加载的。浏览器没有 Node.js 的模块名称解析系统。您不能只将模块名称放在那里。

将'gsap' 替换为模块的URL。

例如from '/path/to/gsap.js';.

但是,当我尝试使用 parcel localhost 打开的 html 格式的相同文件时,它可以正常工作,但在任何其他服务器上它都无法正常工作

嗯,是的。这就是 parcel 的要点:捆绑 JavaScript 模块以在浏览器中运行。

您似乎已成功使用开发服务器构建项目,但您只是将开发文件复制到生产网络服务器而不是 making a production build 并复制其输出。

【讨论】:

  • @MohsenAmani — 不。我所说的与文件系统中文件的组织无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多