根据用户首选项创建快捷代码

时间:2014-09-16 作者:Ben The Novice

背景:根据用户输入创建动态短代码。此短代码应更改的输出do_shortcode 基于用户选择的字段。

示例:在页面上,用户选择

“演示:(X)滑板(X)篮球()曲棍球”

单击“提交”时,执行此操作的短代码应显示为:

[shortcode_name types="Skateboarding, Basketball"]
由于曲棍球没有被选中,曲棍球就不包括在内。

这就是我想要实现的,我只是很难确定如何真正做到这一点。

2 个回复
SO网友:Johannes Pille

理论对你来说可能很难,但实际上很清楚你在努力实现什么。

短代码是将动态内容插入页面(或帖子)的好方法,您的想法失败的地方是“动态短代码”的概念。

使用快捷码将表单插入到内容中,并让回调处理提交后发生的事情。

熟悉how forms are handled in PHPdifference between HTTP-POST and HTTP-GET (Resource 2, Resource 3).

练习将以下内容作为概念证明,而不是复制/粘贴就绪的解决方案:

短代码:[wpse_161632_sports_selection_form]

PHP:

<?php

/*
Plugin Name: WPSE_161632
Plugin URI: http://wordpress.stackexchange.com/questions/161632/
Description: Shortcode form handler example
Version: 1.0
Author: Johannes Pilkahn
License: GPL3
*/

/*  Copyright 2014  Johannes Pilkahn

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 3, as
    published by the Free Software Foundation.

    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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


if ( ! class_exists( \'WPSE_161632\' ) ) :

class WPSE_161632 {

    /**
     * Form handler
     *
     * @since 1.0
     * @access public
     * @see constructor
     */
    public function form_handler( $atts = \'\' )
    {
        // only relevant if the SC is supposed to have extra parameters
        extract( shortcode_atts( array(
            \'example_attribute\' => 1 
        ), $atts ) );

        if ( ( isset( $_POST[\'types\'] ) ) {
            foreach( $_POST[\'types\'] as $type ) {
                // do something
            }
            $this->output( true );
        } else {
            $this->output( false );
        }
    }

    /**
     * Outputs the form
     *
     * @since 1.0
     * @access public
     * @param bool $output
     */
    public function output( $submitted )
    {
        $options = array(
            array(
                \'value\' => \'skateboarding\'
                \'label\' => __( \'Skateboarding\', \'your-text-domain\' )
            ),
            array(
                \'value\' => \'basketball\'
                \'label\' => __( \'Basketball\', \'your-text-domain\' )
            ),
            array(
                \'value\' => \'hockey\'
                \'label\' => __( \'Hockey\', \'your-text-domain\' )
            )
        );

        if ( $submitted ) {
            echo __( \'The form has been submitted!\', \'your-text-domain\' );
        }

        echo \'<form action="#" method="post">\';

        foreach ( $options as $option ) {
            echo \'<input type="checkbox" \'
                . \'value="\' . $option[\'value\'] . \'" \'
                . \'name="types[]" \'
                . \'id="types_\' . $option[\'value\'] . \'"\';

            if ( $submitted && in_array( $option[\'value\'], $_POST[\'types\'] ) ) {
                echo \' checked="checked"\';
            }

            echo \' /><label for="types_\' . $option[\'value\'] . \'">\'
                . $option[\'label\'] . \'</label>\';
        }
        echo \'</form>\';
    }

    /**
     * Constructor
     *
     * @since 1.0
     * @access public
     */
    public function __construct() {
        add_shortcode(
            \'wpse_161632_sports_selection_form\',
            array( $this, \'form_handler\' )
        );
    }

}

endif; // class exists

$wpse_161632 = new WPSE_161632;

?>

SO网友:Caleb

单击“插入短代码”按钮,您将希望使用JavaScript/jQuery查看选中了哪些复选框,并从中创建短代码。创建短代码后,使用window.send_to_editor(\'[shortcode]\').

总的来说是这样的:

$("#insert-shortcode").on(\'click\',function(ev) {
    ev.preventDefault();
    var show = \'\';
    $(".checkboxes").each(function() {
        if ($(this).is(":checked"))
            show += $(this).val() + ",";
    });
    window.send_to_editor(\'[shortcode show="\' + show.substring(0,(show.length-1)) + \'"]\');
});
没有测试过,但类似的东西应该可以工作。当然,根据您的具体姓名调整选择器。我对一些事情做了假设,所以如果我跳过了你需要进一步解释的部分,请告诉我们。

结束

相关推荐

Basic do shortcodes question

我使用WordPress codex页面进行编码。正如您所看到的,我有两个PHP代码。页面能够获得代码的值,但它不遵守CSS规则,我相信这是因为我以错误的方式插入了PHP代码。您能看一下下面的代码并提出问题所在吗?<?php echo do_shortcode(\'[groups_non_member group=\"VPS\"]\' .\"<p class=\'vod-time-price\'>\" . the_field(\'run_t\') . \"MIN | $\". the