php 重力Wiz //重力形式//通过[gravityforms]短代码自定义字段属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力Wiz //重力形式//通过[gravityforms]短代码自定义字段属性相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Gravity Wiz // Gravity Forms // Custom Field Properties via [gravityforms] Shortcode
 *
 * Provide a custom "field_groups" attribute for the [gravityforms] shortcode. This allows you to modify field
 * properties (e.g. label, cssClass, isRequired, etc).
 *
 * Set Field ID 1 as not required.
 * [gravityform id="1" field_props="1:isRequired:0"]
 *
 * Set "my-special-class" as the CSS class for field ID 2.
 * [gravityform id="1" field_props="2:cssClass:my-special-class"]
 *
 * Set the discount amount to 10% and discount type to flat for GP eCommerce Fields Discount field ID 3.
 * [gravityform id="1" field_props="3:taxAmount:10,3:taxAmountType:flat"]
 *
 * @version  0.9.1
 * @author   David Smith <david@gravitywiz.com>
 * @license  GPL-2.0+
 * @link     http://gravitywiz.com/
 *
 * Plugin Name:  Gravity Forms - Shortcode Field Properties
 * Plugin URI:   http://gravitywiz.com/
 * Description:  Specify custom field properties with the "field_props" attribute for the [gravityforms] shortcode.
 * Author:       Gravity Wiz
 * Version:      0.9.1
 * Author URI:   http://gravitywiz.com
 */
class GW_Shortcode_Attr_Field_Props {

	private $_field_props;

    public function __construct( $args = array() ) {

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

	    add_filter( 'shortcode_atts_gravityforms', array( $this, 'stash_shortcode_field_props_attr' ), 10, 4 );
	    add_filter( 'gform_pre_render', array( $this, 'set_field_props' ) );

    }

    public function stash_shortcode_field_props_attr( $out, $pairs, $atts ) {

    	// We don't really need to return the field_props attribute but let's do it just to be thorough.
	    $defaults = array_merge( $pairs, array(
		    'field_props' => ''
	    ) );

	    $out = shortcode_atts( $defaults, $atts );

	    // Now let's parse and stash the field_props attribute for use in the gform_pre_render hook (after which it will be cleared).
		$this->_field_props = $this->parse_field_props( $out['field_props'] );

	    return $out;
    }

    public function parse_field_props( $string ) {

	    $props = array();

    	if( empty( $string ) ) {
    		return $props;
	    }

    	$strings = explode( ',', $string );

    	foreach( $strings as $_string ) {
		    list( $field_id, $prop, $value ) = explode( ':', $_string );
		    if( ! isset( $props[ $field_id ] ) ) {
			    $props[ $field_id ] = array();
		    }
		    $props[ $field_id ][ $prop ] = $value;
	    }

	    return $props;
    }

    public function set_field_props( $form ) {

    	if( empty( $this->_field_props ) ) {
    		return $form;
	    }

	    foreach( $form['fields'] as &$field ) {

    		if( ! isset( $this->_field_props[ $field->id ] ) ) {
    			continue;
		    }

		    foreach( $this->_field_props[ $field->id ] as $prop => $value ) {
    			$field->$prop = $value;
		    }

	    }

    	return $form;
    }

    public function is_applicable_form( $form ) {

        $form_id = isset( $form['id'] ) ? $form['id'] : $form;

        return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
    }

}

# Configuration

new GW_Shortcode_Attr_Field_Props();

以上是关于php 重力Wiz //重力形式//通过[gravityforms]短代码自定义字段属性的主要内容,如果未能解决你的问题,请参考以下文章

php 重力Wiz //重力形式//时间敏感选择

php 重力Wiz //重力形式//货币转换器

php 重力Wiz //重力形式//条件提交按钮:全部必需

php 重力Wiz //重力形式//修改自定义字段的日期格式

php 重力Wiz //重力形式//修改条目中的日期字段格式

php 重力Wiz //重力形式//如果电子邮件存在则重定向