【问题标题】:How to use shortcode of contact form 7 in custom widget?如何在自定义小部件中使用联系表格 7 的简码?
【发布时间】:2019-11-20 00:51:06
【问题描述】:

我从联系表格 7 中复制的短代码不会出现在网页上。

我创建了一个自定义小部件。安装联系表格 7 并复制简码。 在小部件字段中粘贴简码。 我在函数小部件中得到了这个shortocde,比如文本'[contact-form-7 id="66" title="Contact form widget"]'。 我写了do_shortcode($instance['shortcode_text']),但我没有在页面上看到表格。

我的班级

<?php

if( ! defined('ABSPATH') ) exit;

// Класс виджета
class ContactFormWidget extends WP_Widget {
    function __construct() {
        // Запускаем родительский класс
        parent::__construct(
            '', // ID виджета, если не указать (оставить ''), то ID будет равен названию класса в нижнем регистре: my_widget
            'Contact form widget',
            array('description' => 'Widget for contact form')
        );

        // стили скрипты виджета, только если он активен
        if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
            add_action('wp_enqueue_scripts', array( $this, 'add_my_widget_scripts' ));
            add_action('wp_head', array( $this, 'add_my_widget_style' ) );
        }
    }

    // Вывод виджета
    function widget($args, $instance ) {
        $args['before_widget'] = '<div class="col-md-3"><div class="newsletter">';
        $args['after_widget'] = '</div></div>';

        $title = apply_filters( 'widget_title', $instance['title'] );
        $text = $instance['text'];

        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        $html = '';

        $html .= '<div class="detail">';


        $html .= '<div class="signup-text">
                    <i class="icon-dollar"></i>
                    <span>'.$text.'</span>
                </div>';
        do_shortcode($instance['shortcode_text']);

        $html .= '</div>';

        echo $html;

        echo $args['after_widget'];
    }

    // Сохранение настроек виджета (очистка)
    function update( $new_instance, $old_instance ) {

        $instance = array();
        $instance['title'] = ( !empty( $new_instance['title'] ) ) ? ( $new_instance['title'] ) : '';
        $instance['text'] = ( !empty( $new_instance['text'] ) ) ? ( $new_instance['text'] ) : '';
        $instance['shortcode_text'] = ( !empty( $new_instance['shortcode_text'] ) ) ? ( $new_instance['shortcode_text'] ) : '';

        return $instance;
    }

    // html форма настроек виджета в Админ-панели
    function form( $instance ) {
        $title = @ $instance['title'] ?: 'Default title';
        $text = @ $instance['text'] ? : '';
        $shortcode_text = @ $instance['shortcode_text'] ? : '';
        ?>

        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'text:' ); ?></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" type="text" value="<?php echo esc_attr( $text ); ?>">
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'shortcode_text' ); ?>"><?php _e( 'shortcode_text:' ); ?></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'shortcode_text' ); ?>" name="<?php echo $this->get_field_name( 'shortcode_text' ); ?>" type="text" value="<?php echo esc_attr( $shortcode_text ); ?>">
        </p>
        <?php 
    }

    // скрипт виджета
    function add_my_widget_scripts() {
        // фильтр чтобы можно было отключить скрипты
        if( ! apply_filters( 'show_my_widget_script', true, $this->id_base ) )
            return;

        $theme_url = get_stylesheet_directory_uri();

        wp_enqueue_script('my_widget_script', $theme_url .'/my_widget_script.js' );
    }

    // стили виджета
    function add_my_widget_style() {
        // фильтр чтобы можно было отключить стили
        if( ! apply_filters( 'show_my_widget_style', true, $this->id_base ) )
            return;
        ?>
        <style>
            .my_widget a{ display:inline; }
        </style>
        <?php
    }
}

// Регистрация класса виджета
add_action( 'widgets_init', 'register_contact_form_widget' );
function register_contact_form_widget() {
    register_widget( 'ContactFormWidget' );
}

我没有错误,但网页中的代码不包含表单。

【问题讨论】:

    标签: wordpress widget shortcode


    【解决方案1】:
    【解决方案2】:

    您应该在 Widget 内容中使用它https://contactform7.com/faq/can-i-embed-a-contact-form-into-my-template-file/

    echo do_shortcode( '[contact-form-7 id="1234" title="Contact form 1"]' ); 
    

    【讨论】:

    • 谢谢。我忘记在 $html 变量中添加短代码。
    猜你喜欢
    • 2016-08-06
    • 2012-10-22
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多