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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题