从输入值生成短代码

时间:2021-01-11 14:18:47

标签: php wordpress

所以我想做的是制作一个插件,用户可以在输入字段中输入任何内容,然后使用给定的“uid”来制作一个短代码。

因此,当我在某处输入 [text_field_1] 时,它会引用该字段中所写的内容。

所以我确定了固定数量的字段,所以它不需要生成新的字段等等,到目前为止它除了短代码部分之外都可以工作,我不知道该去哪里,我试过了一些不同的解决方案,但没有运气。一直给我错误... :) 我对此也很陌生,所以这可能是多虑了。

感谢您的帮助!

<?php
/**
* Plugin Name: Custom Shortcodes
* Description: Create custom shortcodes for use in wordpress
* Version: 0.1
**/

class custom_shortcode {

    public function __construct() {
        // Hook into the admin menu
        add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );

        // Add Settings and Fields
        add_action( 'admin_init', array( $this, 'setup_sections' ) );
        add_action( 'admin_init', array( $this, 'setup_fields' ) );
    }

    public function create_plugin_settings_page() {
        // Add the menu item and page
        $page_title = 'Custom Shortcode Settings Page';
        $menu_title = 'Custom Shortcode';
        $capability = 'manage_options';
        $slug = 'Custom_shortcode';
        $callback = array( $this, 'plugin_settings_page_content' );
        $icon = '';
        $position = 100;

        add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
    }

    public function plugin_settings_page_content() {?>
        <div class="wrap">
            <h2>Custom Shortcodes</h2><?php echo do_shortcode("[text_field_1]");
            if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
                  $this->admin_notice();
            } ?>
            <form method="POST" action="options.php">
                <?php
                    settings_fields( 'custom_shortcode' );
                    do_settings_sections( 'custom_shortcode' );
                    submit_button();
                ?>
            </form>
        </div> <?php
    }
    
    public function admin_notice() { ?>
        <div class="notice notice-success is-dismissible">
            <p>Dine ændringer er nu blevet opdateret!</p>
        </div><?php
    }

    public function setup_sections() {
        add_settings_section( 'section_1', 'Indtast dine shortcodes her', array( $this, 'section_callback' ), 'custom_shortcode' );
        add_settings_section( 'section_2', '', array( $this, 'section_callback' ), 'custom_shortcode' );
    }

    public function setup_fields() {
        $fields = array(
            array(
                'uid' => 'text_field_1',
                'label' => 'Firmanavn',
                'section' => 'section_1',
                'type' => 'text',
                'placeholder' => 'Indtast her',
            ),
            array(
                'uid' => 'text_field_2',
                'label' => 'test',
                'section' => 'section_2',
                'type' => 'text',
                'placeholder' => 'Indtast her',
            )
        );

        foreach( $fields as $field ){

            add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'custom_shortcode', $field['section'], $field );
            register_setting( 'custom_shortcode', $field['uid'] );
        }
    }

    public function field_callback( $arguments ) {

        $value = get_option( $arguments['uid'] );

        if( ! $value ) {
            $value = $arguments['default'];
        }

        switch( $arguments['type'] ){
            case 'text':
                printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" /> <input value="[%1$s]" readonly />', $arguments['uid'], $arguments['type'], $arguments['placeholder'], $value );
                break;
        }

    }
    
}

new custom_shortcode();
?>

0 个答案:

没有答案
相关问题