Viewing 4 reply threads
  • Author
    Posts
    • #2296
      ralmestro
      Member

      Hey…one more time, congrats for the awesome work…

      I have a question: is possible to set a Metabox for pages except for a custom page template?

      Thanks

    • #2298
      ralmestro
      Member

      reading the forum I find the solution added by jmcphail in Suggestion: Be able to add metaboxes field to a specific page to add the metabox to specific page template before was implemented by default on piklist, the I make a variation to implemente the possibility to exclude the metabox for a list of pages templates

      Only need to overwrite the function register_meta_boxes_callback on class-piklist-cpt.php

      public static function register_meta_boxes_callback($arguments)
        {
          global $post, $pagenow;
      
          extract($arguments);
      
          $current_user = wp_get_current_user();
      
          $data = get_file_data($path . '/parts/' . $folder . '/' . $part, apply_filters('piklist_get_file_data', array(
                    'name' => 'Title'
                    ,'context' => 'Context'
                    ,'description' => 'Description'
                    ,'capability' => 'Capability'
                    ,'role' => 'Role'
                    ,'priority' => 'Priority'
                    ,'order' => 'Order'
                    ,'type' => 'Post Type'
                    ,'lock' => 'Lock'
                    ,'collapse' => 'Collapse'
                    ,'status' => 'Status'
                    ,'new' => 'New'
                    ,'id' => 'ID'
                    ,'div' => 'DIV'
                    ,'template' => 'Template'
                    ,'nointemplate' => 'No Template' // ADDED - get the list of templates where not be included
                    ,'box' => 'Meta Box'
                  ), 'meta-boxes'));
      
          $data = apply_filters('piklist_add_part', $data, 'meta-boxes');
      
          $types = empty($data['type']) ? get_post_types() : explode(',', $data['type']);
          
          foreach ($types as $type)
          {
            $type = trim($type);
      
            $statuses = !empty($data['status']) ? explode(',', $data['status']) : false;
            $ids = !empty($data['id']) ? explode(',', $data['id']) : false;
            $name = !empty($data['name']) ? $data['name'] : 'piklist_meta_' . piklist::slug($part);
            $notemplates = !empty($data['nointemplate']) ? explode(', ', $data['nointemplate']) : false; // ADDED - Convert to Array
            
            if (post_type_exists($type)
              && (!$data['capability'] || ($data['capability'] && current_user_can(strtolower($data['capability']))))
              && (!$data['role'] || ($data['role'] && piklist_user::current_user_role($data['role'])))
              && (!$data['status'] || ($data['status'] && in_array($post->post_status, $statuses)))
              && (!$data['new'] || ($data['new'] && $pagenow != 'post-new.php'))
              && (!$data['id'] || ($data['id'] && in_array($post->ID, $ids)))
              && (!$data['template'] || ($data['template'] && $data['template'] == pathinfo(get_page_template_slug($post->ID), PATHINFO_FILENAME)))
              && (!$data['nointemplate'] || ($data['nointemplate'] && !in_array(pathinfo(get_page_template_slug($post->ID), PATHINFO_FILENAME), $notemplates))) // ADDED - compare to exclude id the templates are in the array
            )
            {
              $id = !empty($data['div']) ? $data['div'] : 'piklist_meta_' . piklist::slug($part);
              $textdomain = isset(piklist_add_on::$available_add_ons[$add_on]) && isset(piklist_add_on::$available_add_ons[$add_on]['TextDomain']) ? piklist_add_on::$available_add_ons[$add_on]['TextDomain'] : null;
               
              add_meta_box(
                $id
                ,!empty($textdomain) ? __($name, $textdomain) : $name
                ,array('piklist_cpt', 'meta_box')
                ,$type
                ,!empty($data['context']) ? $data['context'] : 'normal'
                ,!empty($data['priority']) ? $data['priority'] : 'low'
                ,array(
                  'part' => $part
                  ,'add_on' => $add_on
                  ,'order' => $data['order'] ? $data['order'] : null
                  ,'config' => $data
                )
              );
      
              if (isset($data['box']) && strtolower($data['box']) == 'false')
              {
                add_filter("postbox_classes_{$type}_{$id}", array('piklist_cpt', 'lock_meta_boxes'));
                add_filter("postbox_classes_{$type}_{$id}", array('piklist_cpt', 'no_meta_boxes'));
      
                if ($name == 'piklist_meta_' . piklist::slug($part))
                {
                  add_filter("postbox_classes_{$type}_{$id}", array('piklist_cpt', 'no_title_meta_boxes'));
                }
              }
              else
              {
                if (isset($data['lock']) && strtolower($data['lock']) == 'true')
                {
                  add_filter("postbox_classes_{$type}_{$id}", array('piklist_cpt', 'lock_meta_boxes'));
                }
                if (isset($data['collapse']) && strtolower($data['collapse']) == 'true')
                {
                  add_filter("postbox_classes_{$type}_{$id}", array('piklist_cpt', 'collapse_meta_boxes'));
                }
              }
              add_filter("postbox_classes_{$type}_{$id}", array('piklist_cpt', 'default_classes'));
            }
          }
        }
      

      How to Use

      Build your piklist meta-boxes field file with the following example header declaration:

      /*
      Title: Normal Page Metabox
      Post Type: page
      No Template: template-slug-1, template-slug-2
      */

      In your theme only need to create your templates where not need the use the metabox and add the slug name without the .php, as you see can add more than one template page using ‘, ‘ between them.

      In the code I commented the lines added, Hope this solution can help!

    • #2299
      Steve
      Keymaster

      @ralmestro– Modifying the code of a plugin is bad practice. The next time you upgrade Piklist, all your changes will be lost.

      We created a tutorial illustrating the proper way to hide a meta box when a certain page template is selected. Let us know what you think. You can find the tutorial here >

    • #2305
      ralmestro
      Member

      @steve – I know about the updates, sorry but I not explained myself well, should have specified the solution as a suggestions.

      Thanks for the tutorial, is a simple and well explained solution.

    • #2308
      Steve
      Keymaster

      @ralmestro– Great! Let us know if you need anything else. Closing ticket.

Viewing 4 reply threads
  • The topic ‘Metabox for pages except for a custom page template?’ is closed to new replies.