Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeluno/4ef2600a0df1d91b9967 to your computer and use it in GitHub Desktop.
Save michaeluno/4ef2600a0df1d91b9967 to your computer and use it in GitHub Desktop.
Shows how to create asortable lables with Admin Page Framework
<?php
/*
Plugin Name: Admin Page Framework Example - Sortable Labels
Plugin URI: http://en.michaeluno.jp/admin-page-framework
Description: Shows how to create asortable lables.
Author: Michael Uno
Author URI: http://michaeluno.jp
Version: 1.0.0
Requirements: PHP 5.2.4 or above, WordPress 3.3 or above. Admin Page Framework 3.5.8 or above
*/
// Include the library file. Set your file path here.
$_sLibraryPath = defined( 'WP_DEBUG' ) && WP_DEBUG
? dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php'
: dirname( dirname( __FILE__ ) ) . '/admin-page-framework/library/admin-page-framework/admin-page-framework.php';
if ( ! class_exists( 'AdminPageFramework' ) && ! include( $_sLibraryPath ) ) {
return;
}
class APF_Example_SortableLables extends AdminPageFramework {
public function start() {}
/*
* Use the setUp() method to define settings of this meta box.
*/
public function setUp() {
$this->setRootMenuPage(
__( 'Sortable Labels', 'admin-page-framework-example' ) // top level menu name
);
$this->addSubMenuItems(
array(
'title' => __( 'Sortable Labels', 'admin-page-framework-example' ), // page title
'page_slug' => 'apf_example_sortable_labels', // page slug
)
);
}
/**
*
* @callback action load_{page slug}
*/
public function load_apf_example_sortable_labels() {
/**
* Adds setting fields in the meta box.
*/
$this->addSettingFields(
array(
'field_id' => 'sortable_labels',
'type' => 'hidden',
'title' => __( 'Sortale Labels', 'admin-page-framework-example' ),
'label' => $this->getLabelByValue(
$this->getValue( array( 'sortable_labels', 0 ), 'a' )
),
'default' => 'a',
'sortable' => true,
array( // second item
'label' => $this->getLabelByValue(
$this->getValue( array( 'sortable_labels', 1 ), 'b' )
),
'default' => 'b',
),
array( // third item
'label' => $this->getLabelByValue(
$this->getValue( array( 'sortable_labels', 2 ), 'c' )
),
'default' => 'c',
),
),
array(
'field_id' => 'submit',
'type' => 'submit',
)
);
}
/**
*
* @return string
*/
private function getLabelByValue( $sValue ) {
switch( $sValue ) {
case 'a':
return 'Apple';
case 'b':
return 'Banana';
case 'c':
return 'Cherry';
default:
return $sValue;
}
}
/**
*
* @callback do_{page slug}
*/
public function do_apf_example_sortable_labels() {
echo "<h3>Saved Options</h3>";
$this->oDebug->dump(
get_option( get_class( $this ), array() )
);
}
}
new APF_Example_SortableLables;
@xarkitu
Copy link

xarkitu commented May 23, 2015

Hey michaeluno,

This was my solution, I think it's simple and can give you ideas for inclusion in the framework.

$labels = array(
'a' => 'Apple',
'b' => 'Banana',
'c' => 'Cherry'
);

$opts = $this->getValue('sortable_labels',$labels) ;

    $this->addSettingFields(
        array(
            'field_id'          => 'sortable_labels',
            'type'              => 'hidden',
            'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
            'label'             => $labels[$opts[0]],
            'default'           => 'a',
            'sortable'          => true,
            array(  // second item
                'label'   => $labels[$opts[1]],
                'default' => 'b',
            ),    
            array(  // third item
                'label'   => $labels[$opts[2]],
                'default' => 'c',  
            ),    
        )
    );     

}

Thank you very much for your time and assistance (Y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment