我试图在代码中实现一种通过ID向特定用户显示特定联系人表单数据的方法,但我很难找到要将其添加到哪个部分的代码。
$user_ID = get_current_user_id();
if ( $user_ID == ('2') ) { 我目前在插件文件中的admin-mainpage.php中。它并不像我想象的那么简单,因为它没有通过db存储表单名。任何帮助都将不胜感激。
发布于 2020-02-04 13:04:35
看起来,Contact 7是表单部分的插件,而CFDB7是一个附带的插件,它将表单提交写入WP数据库。
获取表单ID
获取表单ID有几种方法,看起来最简单的方法是查看CF7创建的短代码。你可以在他们的文档里看到。代码类似于[contact-form-7 id="4" title="Foo"],id是表单ID (本例中为4)。
将正确的表单显示给正确的人
假设您知道要显示的用户ids和相关表单ids,您可以编写一个非常简单的短代码插件来为正确的人显示正确的表单。就像下面这个未经测试的代码一样。
//[user-form-display]
function user_form_display( $atts ){
$user_id = get_current_user_id();
if ($user_id == 2){
echo do_shortcode('[contact-form-7 id="4" title="Foo"]');
} else if ($user_id == 4){
echo do_shortcode('[contact-form-7 id="88" title="Bar"]');
}
}
add_shortcode( 'user-form-display', 'user_form_display' );然后,您可以将短代码放在常规post字段中,既不编辑CF7插件,也不编辑主题文件。
您还可以使短代码爱好者和绑定用户in直接在短代码参数中形成in。这将需要更多的努力,但可能是值得的。
获取表单数据您可以修改$args以包含基于与用户id或多个用户id的关联的表单id。表单in应该是该表中的一个字段。这就是下面所示的例子。
或者,您可以根据相同的关系修改信息的返回方式,方法是在$data_value行中设置if/ lines语句。这可能比较容易,但从长远来看会更混乱。
function specfic_table_data()
{
global $wpdb;
$user_id = get_current_user_id();
if($user_id == 1){
$form_ids = array(4,6);//only returns forms with id 4 and 6 when it's user w id 1
}
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$data = array();
$table_name = $cfdb->prefix.'db7_forms';
$args = array(
'post_type'=> 'wpcf7_contact_form',
'order' => 'ASC',
'posts_per_page' => 10,
'post__in' => $form_ids,
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$form_post_id = get_the_id();
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = $form_post_id");
$title = get_the_title();
$link = "<a class='row-title' href=admin.php?page=cfdb7-list.php&fid=$form_post_id>%s</a>";
$data_value['name'] = sprintf( $link, $title );
$data_value['count'] = sprintf( $link, $totalItems );
$data[] = $data_value;
endwhile;
var_dump($data);
}https://stackoverflow.com/questions/60049116
复制相似问题