我想在帖子内容创建或更新后对其运行过滤器。
我希望在用户提交帖子后发生这种情况,因为这可能是一个有点长的过程(查找/替换以搜索词汇表术语并将其替换为链接)。
实现这一目标最有效、最可靠的方法是什么?
提前感谢,
我想在帖子内容创建或更新后对其运行过滤器。
我希望在用户提交帖子后发生这种情况,因为这可能是一个有点长的过程(查找/替换以搜索词汇表术语并将其替换为链接)。
实现这一目标最有效、最可靠的方法是什么?
提前感谢,
好的,这是我刚刚编写的一些代码。完全未经测试,只是即兴写下。。。所以,不要期望它在您投入使用时能百分之百地发挥作用,但它的核心概念就在那个里,还有大量的腿部工作
add_action( \'my_filter_posts_content\', \'my_filter_content\' );
add_action( \'save_post\', \'my_set_content_filter\' );
if( !wp_next_scheduled( \'my_filter_posts_content\' ) ) {
wp_schedule_event( time(), \'hourly\', \'my_filter_posts_content\' );
}
function my_filter_content() {
//check to see if posts need to be parsed
if( get_option( \'my_updated_posts\' ) == false )
return false;
//parse posts
$ids = unserialize( get_option( \'my_updated_posts\' ) );
foreach( $ids as $v ) {
YOUR_FUNCTION_HERE( $v );
}
//make sure no values have been added while loop was running
$id_recheck = unserialize( get_option( \'my_updated_posts\' ) );
my_close_out_filter( $ids, $id_recheck );
/*
once all options, including any added during the running of what
could be a long cronjob are done, remove the value and close out
*/
delete_option( \'my_updated_posts\' );
return true;
}
function my_set_content_filter( $post_id ) {
//get the previous value
$ids = unserialize( get_option( \'my_updated_posts\' ) );
//add new value if necessary
if( !in_array( $post_id, $ids ) ) {
$ids[] = $post_id;
update_option( \'my_updated_posts\', serialize( $ids ) );
}
}
function my_close_out_filter( $beginning_array, $end_array ) {
$diff = array_diff( $beginning_array, $end_array );
if( !empty ( $diff ) ) {
foreach( $diff as $v ) {
YOUR_FUNCTION_HERE( $v );
}
}
my_close_out_filter( $end_array, unserialize( get_option( \'my_updated_posts\' ) ) );
}
请看我的答案here. 这是在安装了WP的Linux中运行本机crontab的概念证明。
至于WP Cron功能,请注意以下注意事项:
WP Cron是加载WP时运行的伪Cron。WP检查WP Cron是否计划运行或落后于计划运行,然后执行Cron脚本如果选择WP Cron路线,here\'s a great article 向您展示如何使用它。您还可以查看Codex中的WP函数here.
我更喜欢Linux crontab的可靠性,尤其是对于集成的、重量级cron脚本。对于非常轻量级的脚本,我有时会使用WP-Cron。
希望这有帮助!
save\\u post操作是否适用于您?
<?php
add_action( \'save_post\', \'wp_save_post\' );
function wp_save_post()
{
// do stuff
}
您还可以考虑edit\\u post或publish\\u post您可以在中查看其他操作Codex
我有一个Wordpress网络,我的任务是禁用WP Cron并将其替换为Apache Cron。我已经设置了一个PHP脚本,当Apache Cron调用该脚本时,它将在网络下的所有站点中循环,并向该站点的wp Cron发出请求。php页面,从而执行其cron。我想使用Wordpress的瞬态特性来限制我的PHP脚本,因为Wordpress限制了自己的cron。然而,当我深入研究代码时,我看到doing\\u cron瞬态已设置(在cron.php#217中),但从未取消设置。瞬态是否一直未设置,或者Wor