dsdfd2322 2015-09-04 02:04
浏览 65

如何通过第三方插件在Wordpress帖子中制作自定义表单?

I have a child theme made where I made a custom form. It has exactly the same fields as the form in a plugin I am using (using Anspress). I want users to be able to submit through my form. I attempted to use the POST method to get my form to post to the PHP file where the plugin's question form is located. However, when I hit the submit button, nothing happens.

I'd settle for some sort of clue to get me started. I followed a W3Schools guide and also found a similar question on here that let to to what I attempted below.

Child theme's form

<?php /* Template Name: Main Form */ ?>



<?php get_header(); ?>


<div class="container">
    <div class="row">
        <form action=".../.../.../plugins/anspress-question-answer/includes/ask-form.php" method="POST">
            <div class="col-md-6">

                <div class="form-group">
                    <input id="addtitle" type="text" name="title" class="form-control" placeholder="Title">
                </div>
                <div class="form-group">
                    <textarea class="form-control" rows="3" name="description" id="addquestion" placeholder="Find this for me..."></textarea>
                </div>
                <div class="form-group">
                    <div class="input-group" id="addtags">
                        <input type="text" class="form-control" placeholder="Tags">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12" id="main_submit_div" name="submit_button">
                    <button type="button" class="btn btn-success" id="main_submit_button">Submit</button>
                </div>
            </div>
        </form>
    </div>
</div>

Plugin's form in the respective PHP file

<?php
/**
 * Form and controls of ask form
 *
 * @link http://anspress.io
 * @since 2.0.1
 * @license GPL2+
 * @package AnsPress
 */

class AnsPress_Ask_Form
{
    public function __construct()
    {
        add_filter('ap_ask_form_fields', array($this, 'ask_form_name_field'));
    }

    public function ask_form_name_field($args){
        if(!is_user_logged_in() && ap_opt('allow_anonymous'))
            $args['fields'][] = array(
                'name' => 'name',
                'label' => __('Name', 'ap'),
                'type'  => 'text',
                'placeholder'  => __('Enter your name to display', 'ap'),
                'value' => sanitize_text_field(@$_POST['name'] ),
                'order' => 12
            );

        return $args;
    }
}

new AnsPress_Ask_Form;

/**
 * Generate ask form
 * @param  boolean $editing
 * @return void
 */
function ap_ask_form($editing = false){
    global $editing_post;

    $is_private = false;
    if($editing){
        $is_private = $editing_post->post_status == 'private_post' ? true : false;
    }

    $args = array(
        'name'              => 'ask_form',
        'is_ajaxified'      => true,
        'submit_button'     => ($editing ? __('Update question', 'ap') : __('Post question', 'ap')),
        'fields'            => array(
            array(
                'name' => 'title',
                'label' => __('Title', 'ap'),
                'type'  => 'text',
                'placeholder'  => __('Question in one sentence', 'ap'),
                'desc'  => __('Write a meaningful title for the question.', 'ap'),
                'value' => ( $editing ? $editing_post->post_title : sanitize_text_field( @$_POST['title'] ) ),
                'order' => 5,
                'attr' => 'data-action="suggest_similar_questions"',
                'autocomplete' => false,
            ),
            array(
                'name' => 'title',
                'type'  => 'custom',
                'order' => 5,
                'html' => '<div id="similar_suggestions"></div>'
            ),
            array(
                'name' => 'description',
                'label' => __('Description', 'ap'),
                'type'  => 'editor',
                'desc'  => __('Write description for the question.', 'ap'),
                'value' => ( $editing ? apply_filters('the_content', $editing_post->post_content) : @$_POST['description']  ),
                'settings' => apply_filters( 'ap_ask_form_editor_settings', array(
                    'textarea_rows'     => 8,
                    'tinymce'           => ap_opt('question_text_editor') ? false : true,
                    'quicktags'         => ap_opt('question_text_editor') ? true : false ,
                    'media_buttons'     =>false,
                )),
            ),
            array(
                'name'  => 'ap_upload',
                'type'  => 'custom',
                'html' => ap_post_upload_form(),
                'order' => 10
            ),
            array(
                'name' => 'parent_id',
                'type'  => 'hidden',
                'value' => ( $editing ? $editing_post->post_parent : get_query_var('parent')  ),
                'order' => 20
            )
        ),
    );

    if(ap_opt('allow_private_posts'))
        $args['fields'][] = array(
            'name' => 'is_private',
            'type'  => 'checkbox',
            'desc'  => __('Only visible to admin and moderator.', 'ap'),
            'value' => $is_private,
            'order' => 12,
            'show_desc_tip' => false
        );

    if(ap_opt('recaptcha_site_key') == '')
        $reCaptcha_html = '<div class="ap-notice red">'.__('reCaptach keys missing, please add keys', 'ap').'</div>';
    else
        $reCaptcha_html = '<div class="g-recaptcha" id="recaptcha" data-sitekey="'.ap_opt('recaptcha_site_key').'"></div><script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl='.get_locale().'&onload=onloadCallback&render=explicit"  async defer></script><script type="text/javascript">var onloadCallback = function() {
        widgetId1 = grecaptcha.render("recaptcha", {
          "sitekey" : "'.ap_opt('recaptcha_site_key').'"
        });
      };</script>';

    if(ap_opt('enable_recaptcha'))
        $args['fields'][] = array(
            'name' => 'captcha',
            'type'  => 'custom',
            'order' => 100,
            'html' => $reCaptcha_html
        );

    /**
     * FILTER: ap_ask_form_fields
     * Filter for modifying $args
     * @var array
     * @since  2.0
     */
    $args = apply_filters( 'ap_ask_form_fields', $args, $editing );

    if($editing){
        $args['fields'][] = array(
            'name'  => 'edit_post_id',
            'type'  => 'hidden',
            'value' => $editing_post->ID,
            'order' => 20
        );
    }

    $form = new AnsPress_Form($args);

    echo $form->get_form();
    echo ap_post_upload_hidden_form();
}

/**
 * Generate edit question form, this is a wrapper of ap_ask_form()
 * @return void
 * @since 2.0.1
 */
function ap_edit_question_form()
{
    ap_ask_form(true);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿