通过插件发布后编辑帖子?

发布于 2024-11-27 02:39:55 字数 356 浏览 0 评论 0原文

我正在尝试编写一个插件来编辑已发布帖子的内容。我尝试过使用这个:

function edit( $post_ID ) {
    $content = "Hello. This is a test.";

    $post_info = get_post($post_ID);    
    $post_info->post_content = "$content";

    wp_update_post( $post_info );
}

add_action('publish_post', 'edit');

虽然那不起作用。它进入一个循环(因为它正在再次发布)并且仅在超时时结束。还有其他方法可以做到这一点吗?

I'm trying to write a plugin which edits the content of a published post. I've tried using this:

function edit( $post_ID ) {
    $content = "Hello. This is a test.";

    $post_info = get_post($post_ID);    
    $post_info->post_content = "$content";

    wp_update_post( $post_info );
}

add_action('publish_post', 'edit');

Although that's not working. It enters a loop (because it's being published again) and only ends when it times out. Would there be another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-12-04 02:39:55

我认为函数中必须有一个静态变量来跟踪该函数是否已被调用。另外,wp_update_post 接受一个数组而不是一个对象——至少我是这样做的。

function edit( $post_ID ) {
    static $plugin_has_updated = false;
    if ($plugin_has_updated) return;
    $plugin_has_updated = true;
    $content = "Hello. This is a test.";
    $post_arr = array("ID"=>$post_ID, "post_content"=>$content);
    wp_update_post( $post_arr );
}

add_action('publish_post', 'edit');

I think you have to have a static variable in the function that tracks whether the function has been called. Also, wp_update_post takes an array rather than an object -- at least that's the way I do it.

function edit( $post_ID ) {
    static $plugin_has_updated = false;
    if ($plugin_has_updated) return;
    $plugin_has_updated = true;
    $content = "Hello. This is a test.";
    $post_arr = array("ID"=>$post_ID, "post_content"=>$content);
    wp_update_post( $post_arr );
}

add_action('publish_post', 'edit');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文