WordPress form shortcode based on form_id
我正在用WordPress练习。我创建了一个简单的表单插件,可以通过form_id从数据库中获取表单字段。
我试图将form_id添加到简码中,所以如果我输入带有表单ID的简码,我将基于form_id获得表单。
我具有用于获取表单字段的功能,而我正在遍历。结果很好,因此,如果我手动更改form_id,我将获得与相同form_id相同的结果。
获取表格的代码
1 2 3 4 5 6 7 8 9 10 11 | function my_html_form() { $results = my_form_get_form_id(); foreach ($results as $result) { $form_id = $result->id; $fields = my_form_get_fields($form_id); } //in here I am looping through $fields and getting all fields } |
在这里我将表单添加到简码中,我知道这样添加form_id是错误的,但是我不知道如何。
添加简码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function my_form_shortcode() { ob_start(); my_html_form(); return ob_get_clean(); } $results = my_form_get_form_id(); foreach ($results as $result) { $form_id = $result->id; $fields = my_form_get_fields($form_id); foreach ($fields as $field) { $fields_form_id = $field->form_id; add_shortcode("my_form_shortcode:$fields_form_id", 'my_html_form()' ); } } |
那么如何获取该
任何帮助将不胜感激。
您可以将属性添加到您的shortcode函数中,并使用do_shortcode('[my_form_shortcode id = 1]');
1 2 3 4 5 6 7 8 9 | function my_form_function($atts) { $atts = shortcode_atts(array( 'id' => 0, ), $atts ); ob_start(); my_html_form($atts['id']); return ob_get_clean(); } add_shortcode("my_form_shortcode", 'my_form_function' ); |
您可以在my_html_form中删除ID,因为它调用了函数(认为它将生成html)
已编辑
我更改了上面的代码以修复一些错误
作为参考检查链接
简码提示