我正在为客户创造一些东西Class 我用一个Custom Post Type called \'PuSH Feeds\' 当用户添加新帖子并发布时,他们可以单击two buttons 我在Custom Meta Box.
一个按钮用于\'Subscribe\' 另一个是\'Unsubscribe\'. 我正在使用save_post action hook 并测试$_POST 全球有\'pushfeed-subscribe\' 或\'pushfeed-unsubscribe\' 然后做我需要做的事。然而,出于某种原因,我发现,一旦我在本地机器上单击订阅,脚本就会停止,因为它说它连续调用了100次等等,最后我收到了大量没有标题的重复帖子。
避免这种情况的最佳方法是什么?对于订阅提要(进入另一个类并执行subscribe方法)的这些特殊自定义操作,是否有更好的钩子可以使用?
这是我在Metabox中为我提到的两个按钮所做的标记
<input type="submit" class="button-secondary" name="pushfeed-subscribe" id="pushfeed-subscribe" value="Subscribe">
<input type="submit" class="button-secondary" name="pushfeed-unsubscribe" id="pushfeed-unsubscribe" value="Unsubscribe">
那么我有这个动作挂钩:
add_action( \'save_post\', array( $this, \'pushfeed_save_post_meta\' ) );
实际挂钩如下所示:
public function pushfeed_save_post_meta( $post_id ) {
// Bail if we\'re doing an auto save
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
// if our nonce isn\'t there, or we can\'t verify it, bail
if( !isset( $_POST[\'pushfeed-nonce-field\'] ) || !wp_verify_nonce( $_POST[\'pushfeed-nonce-field\'], basename( __FILE__ ) ) ) return;
// If Subsctiption ID is empty, generate a random long number and save it
if ( empty( $_POST[\'pushfeed-subscription-id\'] ) ) {
$random_number = substr(number_format(time() * mt_rand(),0,\'\',\'\'),0,10);
$pushfeed_subscription_id = $random_number . $post_id;
update_post_meta( $post_id, \'pushfeed-subscription-id\', $pushfeed_subscription_id );
}
...
if ( isset( $_POST[\'pushfeed-subscribe\'] ) || isset( $_POST[\'pushfeed-unsubscribe\'] ) ) {
$subscription_domain = get_post_meta($post_id, \'pushfeed-domain\', true);
$subscription_id = get_post_meta($post_id, \'pushfeed-subscription-id\', true);
$subscription_feed_url = get_post_meta($post_id, \'pushfeed-feed-url\', true);
$subscription_callback_url = $subscription_domain . \'/pushfeed/\' . $subscription_id;
$sub = PuSHSubscriber::instance($subscription_domain, $subscription_id, \'PuSHSubscription\', new PuSHEnvironment());
if ( isset( $_POST[\'pushfeed-subscribe\'] ) ) {
$sub->subscribe($subscription_feed_url, $subscription_callback_url);
} elseif ( isset( $_POST[\'pushfeed-unsubscribe\'] ) ) {
$sub->unsubscribe($subscription_feed_url, $subscription_callback_url);
}
}
}
我想知道为什么这篇文章要保存多个没有标题的副本。
But above all I would like to know if there is a better action hook I can call for these two custom actions.Update :
大家好。我最终使用了一个使用wordpress admin Ajax的Ajax请求。单击按钮然后触发订阅方法时使用php。完成后,subscription方法将执行get请求,如果返回200代码,则该方法将向Ajax返回true。