• Hello!!
    First, sorry for my posibly bad English…
    I try to create my own theme of WP from the start.
    I created a control of type button:

    $wp_customize->add_control( 'nuevo-post', array(
        'section'  => 'Posts',
        'settings' => array(),
        'type' => 'button',
        'input_attrs'  => array(
            'value' => __( 'Nuevo post' ) ,
        ),
        'capability' => 'edit_posts',
    ) );

    The button appears, but I want to add funcionality when clicking.
    I would like, for example, to call a function.
    How can I do it?

    • This topic was modified 8 years, 7 months ago by mtsa.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    It partly depends on what the function is supposed to do. Customizer items are supposed to alter the appearance of the preview. There’s a system in place to make that happen. If you are not altering the preview appearance, that system is of little help. Your button is then just like any other DOM element. Add an event listener to the button element. The listener callback then handles whatever it is you need to do. For example, a new post could be added by a custom Ajax handler or by utilizing the REST API.

    Thread Starter mtsa

    (@mtsa)

    Thanks you.

    My intention is to create a new article or post on my page through Customizer. I’m begginer to WordPress and I’ve searched a lot to find out how to create a control button. What I want is that when you click, repeat the settings and be able to add a new post, to make my theme more usable.

    My code (it’s just a simple example):

    
    $wp_customize->add_section( 'Posts', array(
            'title'     => __( 'Nuevo post:', 'mytheme' ),
            'description' => __( 'Añade un nuevo post' ),
            'priority'  => 0,
        
        ));
    
       $wp_customize->add_setting('titulo_post', array(
            'default' => __('Título', 'mytheme'), 
            'sanitize_callback' => 'my_sanitize_text_field',
        ));
    
        $wp_customize->add_control('titulo_post', array(
            'type' => 'text',
            'label' => __('Título:', 'mytheme'),
            'section' => 'Posts',
            'priority' => 1,
        ));  
    
       $wp_customize->add_setting('contenido_post', array(
            'default' => __('Contenido', 'mytheme'), 
            'sanitize_callback' => 'my_sanitize_text_field',
        ));
    
        $wp_customize->add_control('contenido_post', array(
            'type' => 'text',
            'label' => __('Contenido:', 'mytheme'),
            'section' => 'Posts',
            'priority' => 2,
        ));  
    
    $wp_customize->add_control( 'nuevo-post', array(
        'section'  => 'Posts',
        'settings' => array('titulo_post', 'contenido_post'),
        'type' => 'button',
        'input_attrs'  => array(
            'value' => __( 'Nuevo post' ) ,
        ),
        'capability' => 'edit_posts',
    ) );

    I read the information here: https://make.wordpress.org/core/2016/03/10/customizer-improvements-in-4-5/

    The problem is that the button has no funcionality for the moment, and I don’t know how do it.

    How do I add the event listener to this button? Do it have any specific attribute?

    Or maybe what I want to do has an easier way and I’m complicating it… I am beginner with php and with wordpress.

    Can you help me, please?

    • This reply was modified 8 years, 7 months ago by mtsa.
    • This reply was modified 8 years, 7 months ago by mtsa.
    • This reply was modified 8 years, 7 months ago by mtsa.
    Moderator bcworkz

    (@bcworkz)

    Adding a listener is all done through JavaScript or jQuery. The callback for that listener can then make an Ajax request to the server, which then executes a PHP script to add the post.

    Where is the data for the post coming from? Or is this merely a shortcut to the usual post edit screen? There may be an easier way to add a post. How you should do this depends on what user experience you want. I don’t know what your intentions are and how adding posts from the customizer would make your theme more usable. The way I use WP, it doesn’t makes sense to me.

    I’m normally in the Customizer only when I want to alter something about my site’s appearance. When I’m doing that, it wouldn’t occur to me to need to create a post. And if I’m not in the customizer, it’s just as easy to go to the new post screen as it is to go to the customizer. Once my site is set up, I’ve little reason to use the customizer at all. The easiest way to add a post is through the “New” item in the Admin Bar. Going through the customizer does not sound easier to use IMO.

    It’s your theme though, you should set it up the way you like. If you disregard how you would accomplish it, where would you most want to place a button that is most convenient for adding a post? A front end sidebar perhaps? It’s hard to beat the Admin Bar for convenience. Not everyone has the bar visible on the front end, which is why a sidebar location might be good. It would only be shown to logged in users who have adequate capability of course.

    Thread Starter mtsa

    (@mtsa)

    All of this is because on my page I have several Post types.

    As I have seen, in wordpress only you can add Posts formed by a title and a content.
    New post

    I want to introduce several types of post, which I have created this way, but with HTML and adding classes, and I have put a different CSS according to the category.

    If a user would like to use my theme, he should use my same defined classes and create the different posts as I think them.

    If I could, from Customizer with specific settings, create the specific content that the user should put, he would not need to know what HTML tags and classes to put.

    Maybe you should leave the theme as is, and the user add the content as it suits him and add the classes and tags him want?

    Moderator bcworkz

    (@bcworkz)

    Thanks for explaining further.

    Perhaps one of the biggest challenges in any user interface design is how much control to leave to the user. You alienate users either way you go. Give them lots of control and choices and the UI becomes too complicated, too hard to understand, learn, etc. Make the UI super easy by removing choices and then your UI is inflexible, rigid, unaccommodating, lacks reasonable options, etc. You cannot win! 🙂

    In general, the WP approach is to keep the UI as simple as possible, but allow for flexibility when desired through plenty of hooks and pluggable functions so plugins and themes can provide added flexibility and choices where the authors like. I think you would be well served to keep this in mind as you develop your own theme. You don’t have to include hooks initially, but at some point you should consider what things users may wish to change that you do not support through the UI. Provide hooks and pluggable functions so they may do so without having to hack up your code.

    By all means provide custom post types and pre-defined categories to fulfill your vision. Whatever HTML and classes with CSS are needed to support that should be accommodated through your templates. I think it’s a mistake to expect users to use the right HTML and classes, your templates should do this for them. If a user wants to do something different, they can make a child theme or utilize the hooks you provide through a plugin to change this.

    Anything that cannot be managed through templates, such as special content that occurs within the user’s content, should be offered as shortcodes so the user still does not need to know the correct HTML and classes to use, the shortcode will manage that.

    While there are some exceptions, the customizer is meant to be only used for altering the appearance and general arrangement of the site. Colors, layout, columns, sidebars, etc. Actual site content is managed elsewhere. Things like menus and widgets are technically “content” but they are recurring elements that relate to appearance and arrangement, so are also managed through the customizer.

    IMO, creating actual content as contained in posts and pages is not what the customizer is for. You seem to believe differently, and that’s fine. I’m still unclear on what is supposed to happen when a user clicks this button in the customizer. Beyond the fact a new post is to be created. What’s the title? How is the content input? How are many of the other options for posts specified?

    Is this all done through a special form? Or is the user simply taken to the appropriate edit screen, much like clicking a “New” item in the admin bar would do? Unless the button is actually an anchor link, some sort of client side script is invoked on the click, mousedown, or tap action. That script could be the entire UI, or it can merely invoke some server side response. This fact remains no matter where your button is.

    If the button remains in the customizer, so be it. That is what needs to happen. If you do decide to move the button elsewhere, getting the button to appear will be different, but what happens after the click remains the same.

    Thread Starter mtsa

    (@mtsa)

    Thanks again!
    Now I really believe that what I wanted to do was to control the user too much.
    I should let him do things for himself and control only small things of appearance.

    Thank you very much for your explanations, I had entered a spiral and I was complicating it too much.

    Moderator bcworkz

    (@bcworkz)

    Heh, theme appearance is hardly a “small” thing! I hope it’s clear that I’m not telling you what to do, my intent is only to point out things that could be a problem. You are free to ignore that and do what you want 🙂

    It can be a difficult decision on what to leave to a user and what to do for them. Either one can cause difficulty. It’s all too easy to get caught up in building the ideal path to accomplish something, forgetting that others will have preferred ways to accomplish the same thing.

    Depending on the nature of a post type, it may be perfectly reasonable to build a customized interface to add new posts. I do not believe the customizer would be the place to access such an interface. It may be fine to do so for some users who have no previous WP experience. For users comfortable with the WP back end, getting to this interface through the customizer would probably never occur to them. Part of good UI design is placing controls where users expect to find them, or at least in a place easily discovered.

    I wish you all the best with your theme development.

    Thread Starter mtsa

    (@mtsa)

    Don’t worry! I don’t think that you are telling you me what to do.
    I simply thought of WordPress in another way and I realized. 🙂

    I wanted the user almost didn’t have to think and now I see that things shouldn’t be like this. No wonder I didn’t find information about it before! xD

    I will follow the way of placing controls that the user expects to find and just as I would expect.

    Thanks you again!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Modify the Customizer’ is closed to new replies.