Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Examples or docs? #224

Open
scottopolis opened this issue Aug 20, 2016 · 9 comments
Open

Examples or docs? #224

scottopolis opened this issue Aug 20, 2016 · 9 comments

Comments

@scottopolis
Copy link

Hey guys, this looks like an awesome plugin. I'm excited to use it, but I'm confused how to add/remove post meta to the customizer.

I have a custom post type with a bunch of registered meta fields, but none of them show in the post customizer. Do you have example code for adding/removing post meta fields?

Thanks!

@westonruter
Copy link
Contributor

westonruter commented Aug 20, 2016

@scottopolis Thank you.

You're right, the postmeta fields you add to a metabox on the edit post admin screen will not automatically show up in the Customizer. Even with the new 4.6 API for registering meta, these fields will not automatically appear (see #192 to start a discussion on that).

But actually the Featured Image is, in fact, postmeta. For one, you can refer to the featured image code in the plugin for how it is accomplished there. Additionally, the Page Template is also postmeta, so you can refer to that.

I also have a quick-and-dirty single-file plugin example (somewhat contrived) for how to add a postmeta for injecting some text before a post in The Loop: https://gist.github.com/westonruter/f8473159e48fbacbef9dcd6498155846

The code should be refactored and cleaned up some more to be a better example, but all of the pieces are there, including:

  • Registering the postmeta for a given post type that support a given post type feature.
  • Adding the control to sections for supporting post types.
  • Adding the partial for rendering the postmeta in the preview.

@westonruter
Copy link
Contributor

And actually, if you have some ideas for good example postmeta that could be added as Customize Posts examples, that would be good too.

@scottopolis
Copy link
Author

Thanks Weston!

@westonruter
Copy link
Contributor

I'll keep this open because I want to add the example to the readme.

@westonruter westonruter reopened this Aug 22, 2016
@westonruter westonruter added this to the 0.8 milestone Aug 22, 2016
@westonruter westonruter removed this from the 0.8.0 milestone Sep 21, 2016
@scottopolis
Copy link
Author

scottopolis commented Sep 27, 2016

Hey Weston, your example works great, I'm trying to use a select menu instead of the post editor, but I'm not able to get this to save. Any idea why? https://gist.github.com/scottopolis/66b0b631b328309ce959dff9766abda8

(Everything shows correctly, I added post type support in another file, it just doesn't save)

@scottopolis
Copy link
Author

Nevermind, figured it out. Forgot to add input_attrs to params.

I updated the gist if you want to use it as an example of a custom select menu for post meta.

@jqlee85
Copy link

jqlee85 commented Dec 16, 2016

Hey guys, thanks for building this plugin and those examples.

I'm experimenting with this and I've built a quick and dirty plugin expanding on those examples. Basically is parses a JSON file full of info for Custom Post Meta and adds the fields to the customizer. Definitely needs some work, but it seems to be working on my test site for 'text', 'checkbox', 'select' and 'post_editor' controls.

https://github.com/Invulu/custom-customizer-postmeta

Just thought I'd leave that here for anyone looking through the thread.

@jqlee85
Copy link

jqlee85 commented Dec 21, 2016

Hi @westonruter,

I'm really excited about this plugin and the direction of the customizer! Thanks for putting so much time into this.

I'm hoping to add a module to our themes that will add customizer support for Custom Post Meta, but I'm struggling a bit to understand exactly how all this is working. I've got an example plugin built from your earlier example that has successfully added some CPM fields to the customizer. But I've run into some issues with the selective refresh. I'm using it for some text fields, and when editing the text fields, it just inserts the CPM text field value into the selected element on the page. How would I go about connecting a custom selective refresh partial to render that change? Would that be something I could do through the params below?

ensuredPartial = new api.selectiveRefresh.partialConstructor.deferred( partialId, { params: { selector: <?php echo wp_json_encode( '.'.$this->current_post_type.'-'.$this->meta_key.'-' ); ?> + String( postId ), settings: [ setting.id ], containerInclusive: false, fallbackRefresh: false } } );

Also is there any documentation for what the valid parameters are for that api.controlConstructor.{control type} call? I'm trying to add a default value to a checkbox CPM field.

control = new api.controlConstructor.post_editor( customizeId, { params: { section: section.id, label: controlLabel, settings: { 'default': customizeId } } } );

I've been trying to dig into the code, but between this plugin and all the core stuff it's a lot to take in, if you could point me to specific docs, classes or files I should be looking at to understand this, that would be awesome!

@westonruter
Copy link
Contributor

@jqlee85 hey, the properties of api.controlConstructor are just aliases of JS Control subclasses. They get associated with corresponding WP_Customize_Control subclasses in PHP by virtue of having the same type. If you're looking for the method to set the default value, however, you're not looking in the right place. The value of the control is the value of its associated setting, so you should be setting the default value of the setting instead. The default you see in the constructor of the post_editor you see there is the default setting to link the control with, not the default value for that setting.

As for an example, I'd suggest the single-file example I reference above: https://gist.github.com/westonruter/f8473159e48fbacbef9dcd6498155846

For specifying the default value in that example, you'd do so in the call to $customize_posts->register_post_type_meta().

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

No branches or pull requests

3 participants