Options field


Hi, i want to use the beans fields functionality to have an option, but i don't understand where the options menu appears

this is my code

add_action( 'admin_init', 'wst_spalatobeans_options' );
function wst_spalatobeans_options() {
 $fields = array(
    array(
      'id'      => 'css_dev_mode',
      'label'   => 'Css dev mode',
      'type'    => 'checkbox',
      'default' => 0
    ),

  );

  beans_register_options( $fields, 'options', 'options', array( 'title' => 'Css dev mode' ) );

}

Hi Alexandra,

From the docs:

beans_register_options( array $fields, string $menu_slug, string $section, array $args = array() )

Every page has an unique menu slug. As far as i know, your menu slug 'options' is not registered on wordpress. The function will register fields only if menu slug is matched.

Solution 1.

Beans options page uses 'beans_settings' as menu slug. To display fields there your function should look like this.

// changed 'options' to 'beans_settings'.
beans_register_options( $fields, 'beans_settings', 'options', array(
  'title' => 'Css dev mode'
) );

Solution 2.

In case you want to create an entire new page with only your fields. Make a similar file like lib/api/admin-options.php, here's a simple example:

final class _Beans_My_Admin {

 /**
     * My menu slug.
     */
    private $menu_slug = 'my_options';

  /**
  * Constructor.
  */
 public function __construct() {
   add_action( 'admin_menu', array( $this, 'admin_menu' ), 150 );
    add_action( 'admin_init', array( $this, 'register' ), 20 );
 }

 /**
  * Add my menu.
  */
 public function admin_menu() {
    add_theme_page(
         __( 'My settings', 'tm-beans' ),  // page title.
            __( 'My Settings', 'tm-beans' ),  // menu title.
            'manage_options',                 // capability.
            $this->menu_slug,                 // menu slug.
            array( $this, 'display_screen'    // callback.
        ) );
 }

 /**
  * My options page content.
  */
 public function display_screen() {
    ?>
    <div class="wrap">
      <h2><?php _e( 'My Settings', 'tm-beans' ); ?></h2>
      <?php beans_options( $this->menu_slug ); ?>
   </div>
    <?php
 }

 /**
  * Register options.
   */
 public function register() {
      $fields = array(
          array(
              'id'      => 'css_dev_mode',
              'label'   => 'Css dev mode',
              'type'    => 'checkbox',
              'default' => 0
          ),
      );

      beans_register_options( $fields, $this->menu_slug, 'options', array(
          'title' => 'Css dev mode'
        ) );
 }
}

new _Beans_My_Admin();

And then extend page with fields from anywhere on your project just by using your new menu slug 'my_options' as reference to the page.


Thank you, having it in beans settings is perfect. I kenw it was something like that, but i don't know where to find the menu names. Can you indicate me where i can find them. Another idea would be to use use the beans dev mode as my css dev mode, but i don't know where to find the name of this field. I have searched in the admin folder of beans, and didn't find it


Now i have problem to retrieve the field value i do like that :

$css_dev_mode = beans_get_term_meta('css_dev_mode',0);

but even if the box is checked, it always get back the default value I tested it with a text field, and the problem is the same


Hello Alex,

In the above code, you've added a new option to the Beans Settings page that's called css_dev_mode. That means it's an option and not meta.

How do you get an option out of the database? Use WordPress' get_option() function.

$css_dev_mode_enabled = (bool) get_option( 'css_dev_mode', false );

Give that a try.


Thank you, having it in beans settings is perfect. I kenw it was something like that, but i don't know where to find the menu names. Can you indicate me where i can find them.

On browser's adress bar, find where page=menu_slug. Example: .../wp-admin/themes.php?page=beans_settings. Menu slug for this page is beans_settings.

Another idea would be to use use the beans dev mode as my css dev mode, but i don't know where to find the name of this field.

Beans dev mode has beans_dev_mode as ID.

$css_dev_mode_enabled = (bool) get_option( 'beans_dev_mode', false );

The easiest way to discover fields ID's registered with Beans Options is to open the Database -> Options Table and find for fields prefixed with beans_*.

I have searched in the admin folder of beans, and didn't find it

Option Fields are registered at their respective folders.

  1. Mode options & Settings Page are registered at lib/api/admin-menu.php.
  2. Compiler options are registered at lib/api/compiler/class-options.php.
  3. Image options are registered at lib/api/image/class-options.php

Thank you Tonya, thank you Jozu, this is vety usefull!

Write a reply

Login or register to write a reply, it's free!