2017-04-12 169 views
1

我使用来自插件Formidable Pro(frm_to_email)的钩子,我绝对需要拥有当前页面的ID(对于高级自定义字段)。由于代码位于function.php,我似乎无法检索它。我能做些什么来获得这个价值?获取钩子的当前页面ID

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); 

回答

1

你应该能够在那个阶段从URL获得那个职位ID,与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); 
+0

这很好用!谢谢! – meneldil

0

您是否尝试以下两种方法之一:

global $post; 
echo $post->ID; 

global $wp_query; 
echo $wp_query->post->ID; 

或者,如果你知道你正在使用表单的页面的ID,您可以添加页面ID作为隐藏的表单字段值。因此,例如,如果您有两个页面出现在表单上,​​您可以创建两个完全相同的表单,但是其隐藏字段的值等于给定表单所显示页面的ID。

+0

嗨,既不是两种解决方案的工作很遗憾。关于蒙面字段的想法,为什么不呢,我必须看看我是否陷入了同样的问题,因为我会使用另一个钩子:我无法复制表单,页面太多。感谢您的搜索路径。 :) – meneldil

0

您必须将全局$ post添加到您的函数中。

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); 
+0

嗨,我忘了在我的示例代码中指定它。 这不幸的是不起作用。 – meneldil