简体   繁体   中英

Add custom buttons with custom actions in Edit Post screen in WordPress?

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.

The problem is probably caused by your use of a submit button.

Custom Meta Boxes are not intended to include submit buttons. The idea is that they contain form fields that get submitted when you click on the standard "Update" button. You can then save what is submitted in the save_post action hook.

Using a submit other than "Update" may be confusing WordPress and causing your problem.

I suggest that you change your Custom Meta Box to have a checkbox for Subscribe or a radio button for Subscribe/Unsubscribe that you can look at in the action hook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM