Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为WordPress插件设置页面(管理面板)创建一个部分(表单)?_Php_Wordpress_Herbert - Fatal编程技术网

Php 如何为WordPress插件设置页面(管理面板)创建一个部分(表单)?

Php 如何为WordPress插件设置页面(管理面板)创建一个部分(表单)?,php,wordpress,herbert,Php,Wordpress,Herbert,我正在使用插件框架,正在创建一个插件 以下是我正在使用的代码: panels.php 现在请插入InControl <?php namespace Plugin\Controllers; use Herbert\Framework\Models\Option; use Herbert\Framework\RedirectResponse; use Herbert\Framework\Http; use \Google_Client; us

我正在使用插件框架,正在创建一个插件

以下是我正在使用的代码:

panels.php 现在请插入InControl

 <?php
    namespace Plugin\Controllers;

    use Herbert\Framework\Models\Option;
    use Herbert\Framework\RedirectResponse;
    use Herbert\Framework\Http;
    use \Google_Client;
    use \Google_Service_Analytics;
    use Plugin\Helper;

    class PluginController {

        public static function createAdminPage()
        {
            $this->option = get_option('pluginAuthenticationSetting');

            //if (!isset($this->option['authenticationCode'])):
            //if (get_option('pluginAuthenticationSetting') == FALSE):

                return view('@Plugin/auth.twig', [
                  'title'   => 'Analytics Reports',
                  'content' => SELF::settings()
                ]);
            //endif;
        }

        public static function settings()
        {
            settings_fields('pluginAuthenticationSetting');
            do_settings_sections('pluginAuthenticationSetting');
            submit_button();
        }

        public static function pageInit()
        {
            wp_register_script(
                'plugin',
                Helper::assetUrl('/jquery/plugin.js'),
                array( 'jquery' )
            );

            wp_localize_script(
               'plugin',
               'ajax_object',
               array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
               'we_value' => 1234 )
            );

            register_setting(
               'pluginAuthenticationSetting',
               'plugin_authorization_setting',
               array( __CLASS__, 'sanitize' )
            );

            add_settings_section(
               'authenticationSection',
               'Authentication Section',
               array( __CLASS__, 'printAuthenticationSection' ),
               'pluginAuthenticationSetting'
            );

            add_settings_field(
               'authenticationCode',
                'Authentication Code',
               array( __CLASS__, 'authenticationCodeCallback' ),
               'apluginAuthenticationSetting',
               'authenticationSection'
            );
        }

        public function sanitization( $input )
        {
            $new_input = array();

            if (isset( $input['authenticationCode']))
                $new_input['authenticationCode'] = sanitize_text_field($input['authenticationCode']);

            return $new_input;
        }

        public static function printAuthenticationSection()
        {
            print 'Enter Your Authentication Code Below:';
        }

        public static function authenticationCodeCallback()
        {

            printf( '<input type="text" id="authentication" name="analyticaAuthenticationSetting[authenticationCode]" value="%s" />', isset( $this->option['authenticationCode'] ) ? esc_attr( $this->option['authenticationCode'] ) : '');
        }
    }

我们需要把重点放在我们想要发送数据的地方

答:对于WordPress的管理方面,也就是说,我们需要使用面板来完成这项工作。当我们向客户端发送数据时,我们使用路由

这是代码

创建面板 控制器显示页面并将数据保存在数据库中

 <?php
    namespace Plugin\Controllers;

    use Herbert\Framework\Models\Option;
    use Herbert\Framework\RedirectResponse;
    use Herbert\Framework\Http;
    use \Google_Client;
    use \Google_Service_Analytics;
    use Plugin\Helper;

    class PluginController {

        public static function createAdminPage()
        {
            $this->option = get_option('pluginAuthenticationSetting');

            //if (!isset($this->option['authenticationCode'])):
            //if (get_option('pluginAuthenticationSetting') == FALSE):

                return view('@Plugin/auth.twig', [
                  'title'   => 'Analytics Reports',
                  'content' => SELF::settings()
                ]);
            //endif;
        }

        public static function settings()
        {
            settings_fields('pluginAuthenticationSetting');
            do_settings_sections('pluginAuthenticationSetting');
            submit_button();
        }

        public static function pageInit()
        {
            wp_register_script(
                'plugin',
                Helper::assetUrl('/jquery/plugin.js'),
                array( 'jquery' )
            );

            wp_localize_script(
               'plugin',
               'ajax_object',
               array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
               'we_value' => 1234 )
            );

            register_setting(
               'pluginAuthenticationSetting',
               'plugin_authorization_setting',
               array( __CLASS__, 'sanitize' )
            );

            add_settings_section(
               'authenticationSection',
               'Authentication Section',
               array( __CLASS__, 'printAuthenticationSection' ),
               'pluginAuthenticationSetting'
            );

            add_settings_field(
               'authenticationCode',
                'Authentication Code',
               array( __CLASS__, 'authenticationCodeCallback' ),
               'apluginAuthenticationSetting',
               'authenticationSection'
            );
        }

        public function sanitization( $input )
        {
            $new_input = array();

            if (isset( $input['authenticationCode']))
                $new_input['authenticationCode'] = sanitize_text_field($input['authenticationCode']);

            return $new_input;
        }

        public static function printAuthenticationSection()
        {
            print 'Enter Your Authentication Code Below:';
        }

        public static function authenticationCodeCallback()
        {

            printf( '<input type="text" id="authentication" name="analyticaAuthenticationSetting[authenticationCode]" value="%s" />', isset( $this->option['authenticationCode'] ) ? esc_attr( $this->option['authenticationCode'] ) : '');
        }
    }
$panel->add([
    'type' => 'panel',
    'as'   => 'mainPanel',
    'title' => 'Analytica',
    'rename' => 'General',
    'slug' => 'analytica-admin-settings',
    'icon' => 'dashicons-chart-area',
    'uses' => __NAMESPACE__ . '\Controllers\AnalyticaController@index',
    'post' => [
        // Sending data to save using post.
        'save' => __NAMESPACE__ . '\Controllers\AnalyticaController@save',
        ]
]);
<?php namespace Analytica\Controllers;

    use Herbert\Framework\Models\Option;
    use Herbert\Framework\RedirectResponse;
    use Herbert\Framework\Http;
    use Herbert\Framework\Enqueue;
    use Herbert\Framework\Notifier;
    use \Google_Client;
    use Analytica\Helper;

    class AnalyticaController {
        public function index(){
            // Display the form
        }

        public function save(){
            // Validate and save the data
        }
    }