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.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c