Support » Developing with WordPress » Save customizer settings to post table

  • Resolved peyman1988

    (@peyman1988)


    Hi,
    I am working on a new theme which I would like to save some of its settings (theme options) as a new post type.
    I have extended the WP_Customize_Setting class and added my settings with the extended class , but none of my extended classes methods are getting called.

    
    class Kirki_Setting_Template_Widget extends WP_Customize_Setting
    {
        const TYPE = 'template-widget';
        public $type = self::TYPE;
    
        public function __construct(WP_Customize_Manager $manager, $id, array $args = array())
        {
            parent::__construct($manager, $id, $args);
            error_log("object $id with type " . $args['type']);
        }
    
        protected function update($value) {
            error_log("update $this->id");
            parent::update($value);
        }
    
        public function value() {
            error_log("value $this->id");
            return parent::value();
        }
    
        public function json() {
            error_log("json $this->id");
            return parent::json();
        }
    
        public function sanitize($value) {
            error_log("sanitize $this->id");
            return parent::sanitize($value); // TODO: Change the autogenerated stub
        }
    
        public function js_value()
        {
            error_log("js_value $this->id");
            return parent::js_value(); // TODO: Change the autogenerated stub
        }
    }
    

    In above code, only the constructor is getting called.
    Could anyone point me to the right direction or a simple sample.
    I have read the code for “Customize Post” and “Kirki” but I cant find what I’m doing wrong.
    Best regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Why do you want to use the posts table? It’s really not the right place for settings. Perhaps there’s a better way to do what you want.

    I want to have templates for widgets, for eg. a sliding navigation in front page and a fixed small black one for categories page…
    users can append templates like nav_menu.
    I think It’s basically what WordPress is doing in nav_menus…

    Moderator bcworkz

    (@bcworkz)

    The Customizer API is primarily intended for adding your own settings saved as theme mods. Going beyond that gets pretty involved. Adding your own setting class by itself does little, it needs an associated control, and neither of them are utilized unless the related section and panel is modified to utilize your classes. While nav menu items are indeed special post types, I don’t see how they utilize templates of any sort.

    Since you are using templates for widgets, I think you would want to save templates as widget settings through the Widget API. Then users can use the default Widget Customizer.

    To use the Customizer for anything beyond simple theme mods, I suggest you refer to how core code handles certain things, like how the nav menus are implemented if you are following the nav menu sort of structure. You’ll see that nav menus in particular have a lot of dedicated classes to handle everything, from their own panel and section to numerous controls and settings.

    The Customizer API is primarily intended for adding your own settings saved as theme mods.

    I will disagree with this.

    Implementing a custom Setting that stores data in posts instead of theme mods is pretty straightforward. You can refer to the class used for the Custom CSS one implementation, which stores its data in the posts table: https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/customize/class-wp-customize-custom-css-setting.php

    The trickiest part of customizing posts is the preview logic, since ensuring the customized state applies to WP_Query results is complicated. This is what the Customize Posts plugin implements. But if you limit yourself to how you query the post then you can use the model of the Custom CSS Setting post type.

    The key thing is to implement the update, value, and preview methods to specifically interface with posts instead. Once you have done this, then all you do is instantiate the custom Setting class when adding the new setting like so:

    $wp_customize->add_setting( new My_Custom_Setting( $wp_customize, 'foo', $args ) );

    This is in contrast to adding a non-custom setting which would then look like this instead:

    $wp_customize->add_setting( 'foo', $args );

    Here are a couple examples of registering custom settings in core:

    * https://github.com/WordPress/wordpress-develop/blob/4.8.1/src/wp-includes/class-wp-customize-manager.php#L4223-L4227
    * https://github.com/WordPress/wordpress-develop/blob/4.8.1/src/wp-includes/class-wp-customize-manager.php#L4058-L4062
    * https://github.com/WordPress/wordpress-develop/blob/4.8.1/src/wp-includes/class-wp-customize-manager.php#L3997-L3999

    Another example of using a Customizer setting to model a post can be found here: https://github.com/xwp/wp-customize-featured-content-demo/blob/develop/php/class-featured-item-property-customize-setting.php

    thanks for the replies.

    You can refer to the class used for the Custom CSS one implementation, which stores its data in the posts table

    I managed to get it working by reading the implementation of “Custom CSS”.
    I haven’t test it much but everything looks good so far.
    thanks so much.

    I think you would want to save templates as widget settings through the Widget API

    what I mean from widget templates is something like “sidebar” templates or “widget area” templates, I want to have different customized css for “navigation” sidebar in different pages for eg.
    I looked a little into “widget api” and it seems its for widget controls theme selfs, I will look into it more, thank you 🙂

    • This reply was modified 3 months, 3 weeks ago by  peyman1988.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.