通过插件发布后编辑帖子?
我正在尝试编写一个插件来编辑已发布帖子的内容。我尝试过使用这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为函数中必须有一个静态变量来跟踪该函数是否已被调用。另外,wp_update_post 接受一个数组而不是一个对象——至少我是这样做的。
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.