1

Looking at this code below it work add options perfectly and saved them. However I will like to use those data at front page in my plugin short code.

class MySettingsPage
{
    /**
     * Holds the values to be used in the fields callbacks
     */
    private $options;

    /**
     * Start up
     */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin', 
            'My Settings', 
            'manage_options', 
            'my-setting-admin', 
            array( $this, 'create_admin_page' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( 'my_option_name' );
        ?>
        <div class="wrap">
            <h2>My Settings</h2>           
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'my_option_group' );   
                do_settings_sections( 'my-setting-admin' );
                submit_button(); 
            ?>
            </form>
        </div>
        <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {        
        register_setting(
            'my_option_group', // Option group
            'my_option_name', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'My Custom Settings', // Title
            array( $this, 'print_section_info' ), // Callback
            'my-setting-admin' // Page
        );  

        add_settings_field(
            'id_number', // ID
            'ID Number', // Title 
            array( $this, 'id_number_callback' ), // Callback
            'my-setting-admin', // Page
            'setting_section_id' // Section           
        );      

        add_settings_field(
            'title', 
            'Title', 
            array( $this, 'title_callback' ), 
            'my-setting-admin', 
            'setting_section_id'
        );      
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input['id_number'] ) )
            $new_input['id_number'] = absint( $input['id_number'] );

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

        return $new_input;
    }

    /** 
     * Print the Section text
     */
    public function print_section_info()
    {
        print 'Enter your settings below:';
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function id_number_callback()
    {
        printf(
            '<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
            isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
        );
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function title_callback()
    {
        printf(
            '<input type="text" id="title" name="my_option_name[title]" value="%s" />',
            isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
        );
    }

****I have try something like This below****

public function DataToget()
    {   

    $jugHigh=get_option( $this->options['title'] );
    return $jugHigh;
    }

}

I have try to make use of it at front page no luck

if ( !function_exists("getdat") ){ 
function getdat_func() {
$getdatClass = new MySettingsPage();

$output=$getdatClass->DataToget();

$output= apply_filters( 'getdat_func', $output );       
    return $output; 
}
}
add_shortcode( 'getdat', 'getdat_func' );

1 Answer 1

2

You are saving data to an option named my_option_name so your attempt to retrieve data from an option named $this->options['title'] isn't going to work. You will need something like this:

public function DataToget($field = '') {   
  if (empty($field)) return;
  $jugHigh = get_option( 'my_option_name' );
  if (!isset($jugHigh[$field])) return;
  return $jugHigh[$field];
}

With a shortcode like:

if ( !function_exists("getdat_func") ){ 
  function getdat_func() {
    $getdatClass = new MySettingsPage();
    $output=$getdatClass->DataToget('title');
    $output= apply_filters( 'getdat_func', $output );       
    return $output; 
  }
}
add_shortcode( 'getdat', 'getdat_func' );

But really, just roll the shortcode callback into your class:

function getdat_func() {
  $output = $this->DataToget('title');
  $output= apply_filters( 'getdat_func', $output );       
  return $output; 
}

And add this line to your constructor:

add_shortcode( 'getdat', array($this,'getdat_func') );
1
  • thank you very much I later got another work around that why I delay in response thank you very much both of your methods worked
    – ShapCyber
    Commented Jul 2, 2015 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.