具有唯一ID的Wordpress插件简码

时间:2019-03-05 00:24:05

标签: php wordpress plugins shortcode

我正在尝试开发我的第一个插件,即表单生成器。一切正常,但我似乎无法为短代码分配唯一的ID。无论我使用什么ID,该简码都会呈现该表格。

我要使用的简码是[my_mail_form id = 24] <-呈现表单 但是这些也会呈现[my_mail_form] [my_mail_form id = 420]形式

当然,目标是为每个表单分配唯一的ID并仅在正确指定表单ID的情况下呈现该表单。

这是我正在使用的代码...

function render_my_mail_form( $atts,$post ){
    $formContent = '';
    $formContent .= '<form method="post" action="">';
    $formContent .= '<input type="text" name="full_name" placeholder="Your Full Name">';
    $formContent .= '<br />';
    $formContent .= '<input type="text" name="email_address" placeholder="Your Email Address">';
    $formContent .= '<br />';
    $formContent .= '<input type="text" name="phone_number" placeholder="Your Phone Number">';
    $formContent .= '<br />';
    $formContent .= '<textarea name="comments" placeholder="Type Your Comment Here"></textarea>';
    $formContent .= '<input type="submit" name="submit_my_form" class="" value="Submit">';
    $formContent .= '</form>';

// I would like to use $post->ID to assign the unique ID
$atts = shortcode_atts(
    array(
        'id' => '24',
    ),
    $atts,
    );
return $formContent;
}
add_shortcode( 'my_mail_form', 'render_my_mail_form' );

我已经花了整整一天的时间在这里到处搜索,并且尝试许多不同的语法只是无法使此操作正确执行。

感谢您的帮助。

0 个答案:

没有答案