mercredi 20 janvier 2016

How to create a section for wordpress plugin setting page using herbert plugin framework?

I am using Herbert Plugin Framework and creating a plugin.

here is the code I am 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 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'] ) : '');
      }
    }

now pageInit() needs admin_init hook , if I will create a constructor and try like add_action( 'admin_init', array( __CLASS__, 'pageInit' ) );, it is not working, if I will use it in panel creation and call createAdminPage taht is also not working, how it can be done .

It generates no error, may be it thinks like no settings are saved as only submit button is displayed .



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire