2

I am creating something for a client and I have a Class that I created with a Custom Post Type called 'PuSH Feeds' and when the user adds a new post and publishes it they can then click on one of two buttons that I have in the Custom Meta Box.

One button is for 'Subscribe' and the other for 'Unsubscribe'. I am using the save_post action hook and testing if the $_POST global has that 'pushfeed-subscribe' or 'pushfeed-unsubscribe' and then do what I need to do. However for some reason I have found that once I click on the subscribe on my local machine stops the script because it says it did 100 consecutive calls etc and I end up with loads of duplicate posts with no title.

What would be the best way to avoid this and is there a better hook I can use for these special custom actions I want to activate of subscribing to a feed (which goes into another class and performs the subscribe method)?

This is the markup I have for those two buttons I mentioned with is inside the 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">

Then I have this for the action hook:

add_action( 'save_post', array( $this, 'pushfeed_save_post_meta' ) );

The actual hook is like this:

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);
        }

    }

}

I am trying to find out why is it that the post is saving multiple duplicates with no title. But above all I would like to know if there is a better action hook I can call for these two custom actions.

Update :

Hi everyone. I ended up using an Ajax request using the wordpress admin-ajax.php when clicking a button and then firing of the subscription method. Once that is done the subscription method will do a get request and if returns a 200 code then the method returns true to the Ajax.

0

Not sure if this is the exact problem you have, but it sounds like the 'save_post' hook is going recursive.

What you need to do is unhook, do your updates, then rehook.

{
    remove_action('save_post', array( $this, 'pushfeed_save_post_meta' ));
    // Do update work...
    add_action('save_post', array( $this, 'pushfeed_save_post_meta' ));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.