Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass custom settings to custom control #1425

Closed
danielortiz opened this issue Jun 25, 2017 · 4 comments
Closed

Cannot pass custom settings to custom control #1425

danielortiz opened this issue Jun 25, 2017 · 4 comments
Milestone

Comments

@danielortiz
Copy link
Contributor

danielortiz commented Jun 25, 2017

Issue description:

I'm creating a custom control that displays a dropdown of posts to be chosen just like this.

<?php
if ( class_exists( 'WP_Customize_Control' ) ) :
	add_action( 'customize_register', function( $wp_customize ) {

		class Martin_Customize_Dropdown_Posts_Control extends WP_Customize_Control {
			public $type = 'dropdown_posts';

			public $post_type = 'post';

			public function render_content() {

				$latest = new WP_Query( array(
					'post_type'   => $this->post_type,
					'post_status' => 'publish',
					'orderby'     => 'date',
					'order'       => 'DESC',
				) );
				?>
				<label>
					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
					<select <?php $this->link(); ?>>
						<option selected> --- </option>
						<?php
						while ( $latest->have_posts() ) {
							$latest->the_post();
							echo '<option ' . selected( $this->value(), get_the_ID() ) . " value='" . get_the_ID() . "'>" . the_title( '', '', false ) . '</option>';
						}
						?>
					</select>
				</label>
				<?php
			}
		}
		// Register our custom control with Kirki
		add_filter( 'kirki/control_types', function( $controls ) {
			$controls['dropdown_posts'] = 'Martin_Customize_Dropdown_Posts_Control';
			return $controls;
		} );
	});
endif;

but trying to overwrite the post_type (or any other arg that I come up with), it simply doens't work

$field_id   = $section_id . '-posts-id';
Martin_Kirki::add_field( 'post', array(
	'type'        => 'dropdown_posts',
	'settings'    => str_replace( '-', '_', $field_id ),
	'label'       => __( 'Select a post', 'martin' ),
	'section'     => $section_id,
	'post_type'   => 'page',
	'default'     => 'none',
	'priority'    => 10,
) );

Version used:

(Did you try using the develop branch from github? There's a chance your issue has already been adressed there)
3.0.5

Using theme_mods or options?

theme_mods

Maybe I'm just missing something...

@aristath aristath modified the milestones: 3.0.5, 3.0.6 Jun 25, 2017
@aristath aristath modified the milestones: 3.0.6, 3.0.7 Jun 26, 2017
@danielortiz
Copy link
Contributor Author

danielortiz commented Jun 27, 2017

digging a bit more, it might be something related to these lines
(Kirki_Field class, __construct, line 349)

		// Set the class properties using the parsed args.
		foreach ( $args as $key => $value ) {
			$this->$key = $value;
		}

(Kirki_field class, set_field, line 410)

		// Get all arguments with their values.
		$args = get_class_vars( __CLASS__ );
		foreach ( $args as $key => $default_value ) {
			$args[ $key ] = $this->$key;
		}

as get_class_varsonly gets the class vars defined by default, not the ones added later (with $this->$key = $value;)

might be pure speculation though..I'm mainly a js dev.. I'm not that familiarized with PHP/WP

I'll keep digging...

Edit:
tested adding this just for testing purposes

		// Get all arguments with their values.
		$args = get_class_vars( __CLASS__ );
		foreach ( $args as $key => $default_value ) {
			$args[ $key ] = $this->$key;
		}
		if (isset($this->post_type)) $args[ 'post_type' ] = $this->post_type;

and it worked, so I think that's really the problem (the field I'm trying to add is named 'post_type')

edit2:
replaced $args = get_class_vars( __CLASS__ ); for $args = get_object_vars( $this ); and it did the trick..
I'll put a PR, but as I said, I'm not really a PHP dev, so I'm not sure if this change has any other implication, so of course, feel free to ignore it and fix it the way you want

@aristath
Copy link
Contributor

👍 Thanks!
The only reason I haven't dealt with this one yet is because there were other bugs that were a bit more urgent. As version 3 solidifies and bugs are fixed, I'm starting to deal with the "less urgent" issues. Not that this is any less important than others, just not as widely used.
I'll deal with any outstanding issues later today and push another update.

@danielortiz
Copy link
Contributor Author

No problem :)
Did you se I left a PR related to this?

@aristath
Copy link
Contributor

Yep, I just merged it, thanks! 👍

It will be included in the next release later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants