Follow us on

Codestar Support Forum » WordPress Plugins » Codestar Framework

Metaboxes do not appear when changing Page Template

  1. Hello,

    Well i sent you a private message some days ago with the issue and as you mentioned i just opened this new topic so maybe we can find a solution.

    Original message:

    - The metaboxes do not appear anymore when you change the page template (you can see the same issue on your main demo): https://prnt.sc/Bk0_Bb9jsYlN (only when you select page template, then update and finally reload the page, the metabox will appear).

    So for example, if i create a new page, change "page template" the first time nothing will appear in the metabox section.

    ---------------------------------------------------

    I tried some solutions from my side and i found this: https://wordpress.stackexchange.com/questions/339134/how-to-get-value-of-selected-page-template-in-gutenberg-editor

    So i created a temporal solution (probably you will need to sanitize and validate, the fire an event when the APP start) but the trick is wp.data variable (which is only available on Gutenberg editors).

    //
      // Metabox Page Templates Listener
      //
      $.fn.csf_page_templates = function() {
        if ( this.length ) {
    
          $(document).on('change', '.editor-page-attributes__template select, #page_template, .components-select-control__input', function() {
    
            var maybe_value = $(this).val() || 'default';
    
            // Detect if is Gutenberg and retrieve data
            if( wp.data !== undefined ){
                var maybe_value = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' ) || 'default';
            }
    
            $('.csf-page-templates').removeClass('csf-metabox-show').addClass('csf-metabox-hide');
            $('.csf-page-'+maybe_value.toLowerCase().replace(/[^a-zA-Z0-9]+/g,'-')).removeClass('csf-metabox-hide').addClass('csf-metabox-show');
    
          });
    
        }
      };CopyCopied!

    It's not the best idea to detect on.change events for all select boxes (with that class) but there are not much options if WP team removed all unique ID's.

    Regards!

    Posted 1 year ago #

  2. Codestar
    Admin

    Hi,

    Actually I have a solution for now ( without listen all select boxes ). As we seen in page source there is a static class name ".edit-post-post-status" and next item is template. In this case, we can edit like this.

    // from
    $(document).on('change', '.editor-page-attributes__template select, #page_template', function() {
    
    // to
    $(document).on('change', '.editor-page-attributes__template select, #page_template, .edit-post-post-status + div select', function() {CopyCopied!

    It works well. Try it and what you think ?

    Regards, Codestar

    Posted 1 year ago #
  3. Hi, Codestar, thank you very much for your quick reply (even on a weekend).

    I think that solution will work, but i know there are some customers with third party plugins that could create or alter the sidebar metaboxes order.

    For the moment i will use this one:

    $(document).on('change', '.editor-page-attributes__template select, #page_template, .components-panel .components-select-control__input', function() {CopyCopied!

    That will reduce to just a few select boxes the change event.

    The components-panel class will not appear on gutenberg blocks for example, Contact Form 7 https://prnt.sc/LvZpdCUoPjvZ

    I know WP team could take a very long time until a pull request is approved (don't know why they removed something so important like a unique block) but just wanted to share my idea/code just in case it could help you in your future updates with this awesome plugin.

    Thank you very much for your time, regards!

    Posted 1 year ago #

  4. Codestar
    Admin

    Hi,

    You're welcome, I love to help always

    Yes, right. It can take long time. I noted this subject for next updates. Also If you find anything more share with me

    Regards, Codestar

    Posted 1 year ago #