获取URL Param插件并在Widget代码中插入结果

时间:2018-03-13 作者:Bruce Christy

我正在尝试从Get-Param插件插入结果,并将其插入WP页面上的Calendaly小部件中。

在代码模块中,如果我编写以下插件语法:[urlparam param="Book" /]

-和-页面的url为:https://www.example.com/booking/?Book=calendly.com/xxxxx

。。。正确显示结果:calendly.com/xxxxx

现在,当我尝试将插件语法放在同一个模块中时within 小部件代码,它不符合预期结果。小部件代码(注意数据url=)后的urlparam):

<!-- Calendly inline widget begin -->
<div class="calendly-inline-widget" data-url="[urlparam param="Book" /]" style="min-width:320px;height:580px;"></div>
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js"></script>
<!-- Calendly inline widget end -->
在内联小部件代码中嵌入此代码是否缺少某种语法?

1 个回复
SO网友:Dave Romsey

我认为这里的根本问题是shortcodes are not evaluated when placed inside HTML attributes. 您可以使用PHP和do_shortcode( \'[urlparam param="Book" /]\') 作为替代方法:

<!-- Calendly inline widget begin -->
<div class="calendly-inline-widget" data-url="<?php echo do_shortcode( \'[urlparam param="Book" /]\' ); ?>" style="min-width:320px;height:580px;"></div>
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js"></script>
<!-- Calendly inline widget end -->

Creating a plugin that contains a Calendly widget

另一种方法是创建一个输出所需HTML的小部件。

然后,通过将下面的代码保存到名为wpse-calendly.php 在名为wpse-calendly. 然后,将自定义Calendally插件复制到plugins 目录,激活插件,并将小部件添加到所需的小部件区域。

请注意,此解决方案仍然依赖于urlparam 功能正常的短代码。

/**
 * Register WPSE_Calendly widget.
 */
add_action( \'widgets_init\', \'wpse_register_calendly_widget\' );
function wpse_register_calendly_widget() {
    register_widget( \'WPSE_Calendly_Widget\' );
}

/**
 * Adds WPSE_Calendly widget.
 */

/**
 * Core class used to implement a WPSE Calendly widget.
 *
 * @see WP_Widget
 */
class WPSE_Calendly_Widget extends WP_Widget {

    /**
     * Sets up a new WPSE Calendly widget instance.
     */
    public function __construct() {
        $widget_ops = array(
            \'description\' => __( \'Display Calendly Widget.\', \'wpse_calendly\' ),
        );
        parent::__construct( \'wpse_calendly\', __( \'Calendly\', \'wpse_calendly\' ), $widget_ops );
    }

    /**
     * Outputs the content for the current Calendly widget instance.
     *
     * @param array $args     Display arguments including \'before_title\', \'after_title\',
     *                        \'before_widget\', and \'after_widget\'.
     * @param array $instance Settings for the current Calendly widget instance.
     */
    public function widget( $args, $instance ) {
        if ( ! empty( $instance[\'title\'] ) ) {
            $title = $instance[\'title\'];
        }

        // Bail if the urlparam shortcode has not been registered.
        if ( ! shortcode_exists( \'urlparam\' ) ) {
            return;
        }

        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
        $title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );

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

        ?>
<!-- Calendly inline widget begin -->
<div class="calendly-inline-widget" data-url="<?php echo do_shortcode( \'[urlparam param="Book" /]\' ); ?>" style="min-width:320px;height:580px;"></div>
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js"></script>
<!-- Calendly inline widget end -->
        <?php

        echo $args[\'after_widget\'];
    }

    /**
     * Handles updating settings for the current Calendly widget instance.
     *
     * @param array $new_instance New settings for this instance as input by the user via
     *                            WP_Widget::form().
     * @param array $old_instance Old settings for this instance.
     * @return array Settings to save or bool false to cancel saving.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance[\'title\'] = sanitize_text_field( $new_instance[\'title\'] );

        return $instance;
    }

    /**
     * Outputs the Calendly widget settings form.
     *
     * @param array $instance Current settings.
     */
    public function form( $instance ) {
        $title_id = $this->get_field_id( \'title\' );
        $instance[\'title\'] = ! empty( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';

        echo \'<p><label for="\' . $title_id .\'">\' . __( \'Title:\', \'wpse_calendly\' ) . \'</label>
            <input type="text" class="widefat" id="\' . $title_id .\'" name="\' . $this->get_field_name( \'title\' ) .\'" value="\' . $instance[\'title\'] .\'" />
        </p>\';
    }
}

结束

相关推荐

Primary menu shortcode name

我有一个快捷码,可以将菜单放在我想要的地方,但它只在主菜单上起作用。我不想对主菜单使用快捷键,我想创建一个名为shortcodemenu的新菜单,并在使用时让快捷键调用它。function print_menu_shortcode($atts, $content = null) { extract(shortcode_atts(array( \'name\' => null, \'class\' => null ), $atts)); return wp_nav_menu( a