dongtuo6562 2014-12-11 10:45
浏览 40
已采纳

在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.

  • 写回答

1条回答 默认 最新

  • duanqian3464 2015-02-03 00:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题