php - How do I create a section (form) for the WordPress plugin setting page (admin panel)? -
i using herbert plugin framework , creating plugin.
here code using:
panels.php
$panel->add([ 'type' => 'panel', 'as' => 'mainpanel', 'title' => 'plugin', 'rename' => 'general', 'slug' => 'plugin-admin-settings', 'icon' => 'dashicons-chart-area', 'uses' => __namespace__ . '\controllers\plugincontroller@createadminpage' ]);
now plugincontroller
<?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 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'] ) : ''); } }
now pageinit()
needs admin_init
hook. if create constructor , try add_action('admin_init', array(__class__, 'pageinit'));
, not working. if use in panel creation , call createadminpage taht not working. how can done?
it generates no error, , submit button displayed.
we need focus on want send data.
answer: administration side of wordpress, is, need use panel job. when send data client side, use route.
here code.
creating panel
$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 save using post. 'save' => __namespace__ . '\controllers\analyticacontroller@save', ] ]);
controller display page , save data in database
<?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 form } public function save(){ // validate , save data } }
you need focus on use herbert\framework\models\option;
. means there model called option represents wp_options
table.
now, functions use here?
we can use laravel eloquent , schema here. may understand it; can lot of things here. having pre-built models , working on same laravel.
note: forget hooks time. can still suit theme, of cases not need theme.
Comments
Post a Comment