• Resolved Layson

    (@glennlaysonjr)


    First biggest fan of what all you all have created!

    Two questions:

    1) I have set up a options page to control custom post type plugin. I have tried to follow all the sample code that has been added to the Github page. I have been able to set all the date fields that I need but having trouble echoing those out on my template page. (Mainly being used to control color and title text) With the normal CMB2 meta fields I just used:

    <?php echo get_post_meta( $post->ID, 'dc_sermons_dc_color', true ); ?>

    This added my data just right and have had no problems. I have also tried to use the sample data shown below and changed out the prefix

    myprefix_get_option( 'test_text' )

    changed to:

    <?php echo dc_options_get_option( 'title_color' ); ?>

    This only echos the entire options page code. I have also tried

    $field_value = myprefix_get_option( 'field_id' );

    and changed out to show the correct information and still nothing. I have also made sure that I have added the correct sample that is found here and tried to just use the given sample fields just to try and get it working.

    https://github.com/CMB2/CMB2-Snippet-Library/blob/master/options-and-settings-pages/theme-options-cmb.php

    It feels like nothing I’m doing is working. Anyway for some help to echo the data I need.

    2) Is there any way to have the “Options” show up under the custom post type that is it apart of not stilling on its own? I have seen a lot of questions about this but no one seem to have the answer.

    Thanks for you help long time coder but first time building WP plugin.

    • This topic was modified 7 years, 8 months ago by Layson.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Layson

    (@glennlaysonjr)

    @hmbashar,

    Thanks for the comment. I have tried this before and spent the last two hours trying to get it to work again and still doesn’t work. It echos out null for me. And any other try being listed above keeps echoing out the page html.

    Anyone have a solution similar to

    <?php echo get_post_meta( $post->ID, 'dc_sermons_dc_color', true ); ?>

    that I have been using to echo standard metadata from CMB2?

    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    @glennlaysonjr

    If this is truly from an option page that has been set up and rendered, then get_post_meta() isn’t going to be the right function here, since that relies on a post ID. Options pages won’t have a post ID to work with ??

    I am going to assume you copy/pasted the function declaration for myprefix_get_option() from the examples. The question here is what exactly does it return, and in which conditions.

    If you could provide your entire CMB2 configuration code, I can give it a look over and provide some starter code for displaying.

    Thread Starter Layson

    (@glennlaysonjr)

    @tw2113

    I did know that the code snip was for ID and for the meta only. I didn’t know if there was a similar code to echo my options.

    here is my option page:

    <?php
    /**
     * CMB2 Theme Options
     * @version 0.1.0
     */
    class DCOptions_Admin {
    	/**
     	 * Option key, and option page slug
     	 * @var string
     	 */
    	protected $key = 'dcoptions_options';
    	/**
     	 * Options page metabox id
     	 * @var string
     	 */
    	protected $metabox_id = 'dcoptions_option_metabox';
    	/**
    	 * Options Page title
    	 * @var string
    	 */
    	protected $title = '';
    	/**
    	 * Options Page hook
    	 * @var string
    	 */
    	protected $options_page = '';
    	/**
    	 * Holds an instance of the object
    	 *
    	 * @var Myprefix_Admin
    	 */
    	protected static $instance = null;
    	/**
    	 * Returns the running object
    	 *
    	 * @return Myprefix_Admin
    	 */
    	public static function get_instance() {
    		if ( null === self::$instance ) {
    			self::$instance = new self();
    			self::$instance->hooks();
    		}
    		return self::$instance;
    	}
    	/**
    	 * Constructor
    	 * @since 0.1.0
    	 */
    	protected function __construct() {
    		// Set our title
    		$this->title = __( 'Hub Options', 'dcoptions' );
    	}
    	/**
    	 * Initiate our hooks
    	 * @since 0.1.0
    	 */
    	public function hooks() {
    		add_action( 'admin_init', array( $this, 'init' ) );
    		add_action( 'admin_menu', array( $this, 'add_options_page' ) );
    		add_action( 'cmb2_admin_init', array( $this, 'add_options_page_metabox' ) );
    	}
    	/**
    	 * Register our setting to WP
    	 * @since  0.1.0
    	 */
    	public function init() {
    		register_setting( $this->key, $this->key );
    	}
    	/**
    	 * Add menu options page
    	 * @since 0.1.0
    	 */
    	public function add_options_page() {
    		$this->options_page = add_menu_page( $this->title, $this->title, 'manage_options', $this->key, array( $this, 'admin_page_display' ) );
    		// Include CMB CSS in the head to avoid FOUC
    		add_action( "admin_print_styles-{$this->options_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
    	}
    	/**
    	 * Admin page markup. Mostly handled by CMB2
    	 * @since  0.1.0
    	 */
    	public function admin_page_display() {
    		?>
    		<div class="wrap cmb2-options-page <?php echo $this->key; ?>">
    			<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
    			<?php cmb2_metabox_form( $this->metabox_id, $this->key ); ?>
    		</div>
    		<?php
    	}
    	/**
    	 * Add the options metabox to the array of metaboxes
    	 * @since  0.1.0
    	 */
    	function add_options_page_metabox() {
    		// hook in our save notices
    		add_action( "cmb2_save_options-page_fields_{$this->metabox_id}", array( $this, 'settings_notices' ), 10, 2 );
    		$cmb = new_cmb2_box( array(
    			'id'         => $this->metabox_id,
    			'hookup'     => false,
    			'cmb_styles' => false,
    			'show_on'    => array(
    				// These are important, don't remove
    				'key'   => 'options-page',
    				'value' => array( $this->key, )
    			),
    		) );
    		// Set our CMB2 fields
    		$cmb->add_field( array(
    			'name' => __( 'Test Text', 'dcoptions' ),
    			'desc' => __( 'field description (optional)', 'dcoptions' ),
    			'id'   => 'test_text',
    			'type' => 'text',
    			'default' => 'Default Text',
    		) );
    		$cmb->add_field( array(
    			'name'    => __( 'Test Color Picker', 'dcoptions' ),
    			'desc'    => __( 'field description (optional)', 'dcoptions' ),
    			'id'      => 'test_colorpicker',
    			'type'    => 'colorpicker',
    			'default' => '#bada55',
    		) );
    	}
    	/**
    	 * Register settings notices for display
    	 *
    	 * @since  0.1.0
    	 * @param  int   $object_id Option key
    	 * @param  array $updated   Array of updated fields
    	 * @return void
    	 */
    	public function settings_notices( $object_id, $updated ) {
    		if ( $object_id !== $this->key || empty( $updated ) ) {
    			return;
    		}
    		add_settings_error( $this->key . '-notices', '', __( 'Settings updated.', 'myprefix' ), 'updated' );
    		settings_errors( $this->key . '-notices' );
    	}
    	/**
    	 * Public getter method for retrieving protected/private variables
    	 * @since  0.1.0
    	 * @param  string  $field Field to retrieve
    	 * @return mixed          Field value or exception is thrown
    	 */
    	public function __get( $field ) {
    		// Allowed fields to retrieve
    		if ( in_array( $field, array( 'key', 'metabox_id', 'title', 'options_page' ), true ) ) {
    			return $this->{$field};
    		}
    		throw new Exception( 'Invalid property: ' . $field );
    	}
    }
    /**
     * Helper function to get/return the Myprefix_Admin object
     * @since  0.1.0
     * @return Myprefix_Admin object
     */
    function dcoptions_admin() {
    	return DCOptions_Admin::get_instance();
    }
    /**
     * Wrapper function around cmb2_get_option
     * @since  0.1.0
     * @param  string $key     Options array key
     * @param  mixed  $default Optional default value
     * @return mixed           Option value
     */
    function dcoptions_get_option( $key = '', $default = false ) {
    	if ( function_exists( 'cmb2_get_option' ) ) {
    		// Use cmb2_get_option as it passes through some key filters.
    		return cmb2_get_option( dcoptions_admin()->key, $key, $default );
    	}
    	// Fallback to get_option if CMB2 is not loaded yet.
    	$opts = get_option( dcoptions_admin()->key, $default );
    	$val = $default;
    	if ( 'all' == $key ) {
    		$val = $opts;
    	} elseif ( is_array( $opts ) && array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
    		$val = $opts[ $key ];
    	}
    	return $val;
    }
    // Get it started
    dcoptions_admin();

    I just copied the template as a starting point and changed up the myprefix.

    I have changed back to the default fields just because i was having issues and didn’t know if it was my fields I created. I am assuming that all I would have to do then is change out the ID with the new field ID to display then after the given code?

    Also thanks for getting back so quickly I am on a deadline to produce this plugin.

    G

    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Based on the provided code, I was able to type up this little bit and have it work, after I hit save on the options page once, and using the default values provided

    function michael_admin_init() {
    	$test_text = dcoptions_get_option( 'test_text' );    // Default Text
    	$color = dcoptions_get_option( 'test_colorpicker' ); // #bada55
    
    	echo '<span style="color:' . $color . '">' . $test_text . '</span>';
    }
    add_action( 'admin_init', 'michael_admin_init' );
    

    Used admin_init just for a quick hook that I knew would reach the screen.

    Thread Starter Layson

    (@glennlaysonjr)

    I didn’t change a thing and I am getting this:

    Fatal error: Uncaught Error: Call to undefined function dcoptions_get_option()

    and was using this to display on my template page:

    <?php $select = michael_admin_init(); echo $select; ?>

    Any thoughts?

    I guess I should give more of a description of what all I am using it for.

    I am using the options page to control the css on my archive, tax and single page via the class…. So what I did before in CMB2 for my metabox via the custom post type was the following:

    .plyr--video .plyr__controls button.tab-focus:focus,
            .plyr--video .plyr__controls button:hover {
                background: <?php echo get_post_meta( $post->ID, 'dc_sermons_dc_color', true ); ?> !important;
            }

    This worked great for custom changes like player colors on each custom post type that is different or it displays the default.

    Now what I am needing is the ability for them to change the colors on the archive page or global colors like links, titles or button colors. That is why I am trying to echo the option field to my css to change the colors and so on.

    Hope this helps out more what all I am doing and maybe will show where my thoughts are.

    Again thanks for your help!
    G

    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If you’re getting an undefined function error, then you’re somehow trying to use the function before it gets loaded. For what it’s worth, my test code earlier had the michael_admin_init callback defined right after I had defined the class, all in the same file.

    Where are you presently loading this code to create the metabox?

    What hook(s) are you trying to fetch everything on for display?

    The order may be a bit off in this case.

    Technically you could just do <?php michael_admin_init(); ?> and get the same results, since the callback already echos ?? But that’s a divergence away from the real issue.

    Thread Starter Layson

    (@glennlaysonjr)

    Here is the final update on what all was happening. Thanks to Michael for the suggestion. I set up my plugin so that it loads scripts for “Front End” + “Admin” and then another that just loads the scripts on the “Admin”. I was originally loading the CMB2 Options script on jut the admin side so nothing was being loaded on the site. I fixed this and now everything works great.

    Also for future note. This code also works to echo out any of the fields:

    <?php echo dcoptions_get_option( 'test_text' ) ?>

    Awesome product and support!

    • This reply was modified 7 years, 8 months ago by Layson.
    • This reply was modified 7 years, 8 months ago by Layson.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Woohoo, glad it got figured out ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Echo Data from Options Page in Custom Post Type Plugin’ is closed to new replies.