代码在unctions.php中工作,但不能插入到Gavity表单中

时间:2015-09-13 作者:csharpdude77

我正在尝试为Gavity表单创建一个插件,我有一些在函数中工作的代码。php并使其正常工作,但我需要了解的是插件avity表单,即如何将代码集成到插件中,添加过滤器是否正常工作。

add_filter("gform_confirmation", "custom_confirmation", 10, 4);
function custom_confirmation($confirmation, $form, $lead, $ajax){
 $gfe = $lead[\'id\'];
我使用这里的代码示例从gravity生成一个插件https://github.com/rocketgenius/simpleaddon

simpleaddon。php源代码

<?php
    /*
    Plugin Name: Gravity Forms Simple Add-On
    Plugin URI: http://www.gravityforms.com
    Description: A simple add-on to demonstrate the use of the Add-On Framework
    Version: 1.1
    Author: Rocketgenius
    Author URI: http://www.rocketgenius.com
    ------------------------------------------------------------------------
    Copyright 2012-2013 Rocketgenius Inc.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */
    //------------------------------------------
    if (class_exists("GFForms")) {
        GFForms::include_addon_framework();
        class GFSimpleAddOn extends GFAddOn {
            protected $_version = "1.1";
            protected $_min_gravityforms_version = "1.7.9999";
            protected $_slug = "simpleaddon";
            protected $_path = "asimpleaddon/asimpleaddon.php";
            protected $_full_path = __FILE__;
            protected $_title = "Gravity Forms Simple Add-On";
            protected $_short_title = "Simple Add-On";
            public function init(){
                parent::init();
                add_filter("gform_submit_button", array($this, "form_submit_button"), 10, 2);
            }
            // Add the text in the plugin settings to the bottom of the form if enabled for this form
            function form_submit_button($button, $form){
                $settings = $this->get_form_settings($form);
                if(isset($settings["enabled"]) && true == $settings["enabled"]){
                    $text = $this->get_plugin_setting("mytextbox");
                    $button = "<div>{$text}</div>" . $button;
                }
                return $button;
            }
            public function plugin_page() {
                ?>
                This page appears in the Forms menu
            <?php
            }
            public function form_settings_fields($form) {
                return array(
                    array(
                        "title"  => "Simple Form Settings",
                        "fields" => array(
                            array(
                                "label"   => "My checkbox",
                                "type"    => "checkbox",
                                "name"    => "enabled",
                                "tooltip" => "This is the tooltip",
                                "choices" => array(
                                    array(
                                        "label" => "Enabled",
                                        "name"  => "enabled"
                                    )
                                )
                            ),
                            array(
                                "label"   => "My checkboxes",
                                "type"    => "checkbox",
                                "name"    => "checkboxgroup",
                                "tooltip" => "This is the tooltip",
                                "choices" => array(
                                    array(
                                        "label" => "First Choice",
                                        "name"  => "first"
                                    ),
                                    array(
                                        "label" => "Second Choice",
                                        "name"  => "second"
                                    ),
                                    array(
                                        "label" => "Third Choice",
                                        "name"  => "third"
                                    )
                                )
                            ),
                            array(
                                "label"   => "My Radio Buttons",
                                "type"    => "radio",
                                "name"    => "myradiogroup",
                                "tooltip" => "This is the tooltip",
                                "choices" => array(
                                    array(
                                        "label" => "First Choice"
                                    ),
                                    array(
                                        "label" => "Second Choice"
                                    ),
                                    array(
                                        "label" => "Third Choice"
                                    )
                                )
                            ),
                            array(
                                "label"   => "My Horizontal Radio Buttons",
                                "type"    => "radio",
                                "horizontal" => true,
                                "name"    => "myradiogrouph",
                                "tooltip" => "This is the tooltip",
                                "choices" => array(
                                    array(
                                        "label" => "First Choice"
                                    ),
                                    array(
                                        "label" => "Second Choice"
                                    ),
                                    array(
                                        "label" => "Third Choice"
                                    )
                                )
                            ),
                            array(
                                "label"   => "My Dropdown",
                                "type"    => "select",
                                "name"    => "mydropdown",
                                "tooltip" => "This is the tooltip",
                                "choices" => array(
                                    array(
                                        "label" => "First Choice",
                                        "value" => "first"
                                    ),
                                    array(
                                        "label" => "Second Choice",
                                        "value" => "second"
                                    ),
                                    array(
                                        "label" => "Third Choice",
                                        "value" => "third"
                                    )
                                )
                            ),
                            array(
                                "label"   => "My Text Box",
                                "type"    => "text",
                                "name"    => "mytext",
                                "tooltip" => "This is the tooltip",
                                "class"   => "medium",
                                "feedback_callback" => array($this, "is_valid_setting")
                            ),
                            array(
                                "label"   => "My Text Area",
                                "type"    => "textarea",
                                "name"    => "mytextarea",
                                "tooltip" => "This is the tooltip",
                                "class"   => "medium merge-tag-support mt-position-right"
                            ),
                            array(
                                "label"   => "My Hidden Field",
                                "type"    => "hidden",
                                "name"    => "myhidden"
                            ),
                            array(
                                "label"   => "My Custom Field",
                                "type"    => "my_custom_field_type",
                                "name"    => "my_custom_field"
                            )
                        )
                    )
                );
            }
            public function settings_my_custom_field_type(){
                ?>
                <div>
                    My custom field contains a few settings:
                </div>
                <?php
                    $this->settings_text(
                        array(
                            "label" => "A textbox sub-field",
                            "name" => "subtext",
                            "default_value" => "change me"
                        )
                    );
                    $this->settings_checkbox(
                        array(
                            "label" => "A checkbox sub-field",
                            "choices" => array(
                                array(
                                    "label" => "Activate",
                                    "name" => "subcheck",
                                    "default_value" => true
                                )
                            )
                        )
                    );
            }
            public function plugin_settings_fields() {
                return array(
                    array(
                        "title"  => "Simple Add-On Settings",
                        "fields" => array(
                            array(
                                "name"    => "mytextbox",
                                "tooltip" => "This is the tooltip",
                                "label"   => "This is the label",
                                "type"    => "text",
                                "class"   => "small",
                                "feedback_callback" => array($this, "is_valid_setting")
                            )
                        )
                    )
                );
            }
            public function is_valid_setting($value){
                return strlen($value) < 10;
            }
            public function scripts() {
                $scripts = array(
                    array("handle"  => "my_script_js",
                          "src"     => $this->get_base_url() . "/js/my_script.js",
                          "version" => $this->_version,
                          "deps"    => array("jquery"),
                          "strings" => array(
                              \'first\'  => __("First Choice", "simpleaddon"),
                              \'second\' => __("Second Choice", "simpleaddon"),
                              \'third\'  => __("Third Choice", "simpleaddon")
                          ),
                          "enqueue" => array(
                              array(
                                  "admin_page" => array("form_settings"),
                                  "tab"        => "simpleaddon"
                              )
                          )
                    ),
                );
                return array_merge(parent::scripts(), $scripts);
            }
            public function styles() {
                $styles = array(
                    array("handle"  => "my_styles_css",
                          "src"     => $this->get_base_url() . "/css/my_styles.css",
                          "version" => $this->_version,
                          "enqueue" => array(
                              array("field_types" => array("poll"))
                          )
                    )
                );
                return array_merge(parent::styles(), $styles);
            }
        }
        new GFSimpleAddOn();
    }

1 个回复
SO网友:Dave from Gravity Wiz

将add\\u filter()调用放入init函数:

public function init(){
    parent::init();
    add_filter("gform_submit_button", array($this, "form_submit_button"), 10, 2);
    add_filter("gform_confirmation", array( $this, \'custom_confirmation\' ), 10, 4 );
}
然后将custom\\u confirmation()函数作为方法添加到类中:

class GFSimpleAddOn extends GFAddOn {

    /* all the other properties and methods */

    public function custom_confirmation($confirmation, $form, $lead, $ajax){
        $gfe = $lead[\'id\'];
        // rest of your code
    }

}

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请