dongluo3331 2017-04-12 17:18
浏览 52
已采纳

获取钩子的当前页面ID

I use a hook from the plugin Formidable Pro (frm_to_email) where I absolutely need to have the ID of the current page (for Advanced Custom Fields). As the code is located in function.php, it seems impossible for me to retrieve it. What can I do to get this value?

function custom_set_email_value($recipients, $values, $form_id, $args){
    global $post;
    $profil_obj = get_field('profil_obj', $post->ID); // If I put the ID directly (10 for example), it works

    if( $form_id == get_field('popup_form_id', 'option') && $args['email_key'] == get_field('popup_email_id', 'option') ){
        if($profil_obj) {
            foreach( $profil_obj as $post) {
                setup_postdata($post);
                $recipients[] = get_field('profil_email', $post->ID);
            }
        }
        wp_reset_postdata();
    }
    return $recipients;
}
add_filter('frm_to_email', 'custom_set_email_value', 10, 4);
  • 写回答

3条回答 默认 最新

  • dongyin0628 2017-04-12 23:07
    关注

    You should be able to get the post ID at that stage from the URL, with url_to_postid():

    <?php
    $scheme  = (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] === "off") ? "http" : "https";
    $url     = "$scheme://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $post_id = url_to_postid( $url );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘