Viewing 25 reply threads
  • Author
    Posts
    • #8359
      noobie
      Participant

      As per title, I am trying to hide the meta box for a specific template following the tutorial (https://piklist.com/user-guide/tutorials/hide-meta-boxes-page-template/)

      I have added the filters in my functions.php and included the comment “Hide for Template” to the comment block but still cannot hide the meta box.

      What should I look out for to resolve this issue?

    • #8360
      Steve
      Keymaster

      Which version of Piklist are you using?

    • #8362
      noobie
      Participant

      Dear Steve

      As per your questions, the below is the stated version.

      Piklist Version 0.9.9.12
      WordPress Helpers Version 1.17.0

    • #8363
      Steve
      Keymaster

      @noobie

      Please change the filter names:

      piklist_get_file_data TO piklist_part_data
      piklist_add_part TO piklist_part_add

      Let me know if that works.

    • #8364
      noobie
      Participant

      Dear Steve

      Nope, it isn’t working.
      Below is the comment block for the metabox and the codes to be placed under functions.php

      /*
      Title: Right Bar Section
      Description: Customized right bar section
      Post Type: page
      Hide for Template: page-full-width
      Order: 3
      */

      add_filter(‘piklist_part_data’, ‘my_custom_comment_block’, 10, 2);
      function my_custom_comment_block($data, $type)
      {
      // If not a Meta-box section than bail
      if($type != ‘meta-boxes’)
      {
      return $data;
      }

      // Allow Piklist to read our custom comment block attribute: “Hide for Template”, and set it to hide_for_template
      $data[‘hide_for_template’] = ‘Hide for Template’;

      return $data;
      }

      add_filter(‘piklist_part_add’, ‘my_hide_for_template’, 10, 2);
      function my_hide_for_template($data, $type)
      {
      global $post;

      // If not a meta box than bail
      if($type != ‘meta-boxes’)
      {
      return $data;
      }

      // Check if any page template is set in the comment block
      if (!empty($data[‘hide_for_template’]))
      {
      // Get the active page template
      $active_template = pathinfo(get_page_template_slug($post->ID), PATHINFO_FILENAME);

      // Is there a page template set for this page?
      if(!empty($active_template))
      {
      // Does the active page template match what we want to hide?
      if (strpos($data[‘hide_for_template’], $active_template) !== false)
      {
      // Change meta-box access to user role: no-role
      $data[‘role’] = ‘no-role’;
      }
      }
      }
      return $data;
      }

    • #8365
      riotxoa
      Participant

      I have the same problem with both filters, and I think I’ve found the solution (or the real problem):

      1. As Steve says, you must use ‘piklist_part_data’ and ‘piklist_part_add’ filters.
      2. In ‘function my_custom_comment_block’ the key or slug of $data can’t contain chars as ‘_’. I don’t know why, but try using $data[‘hidefortemplate’] or $data[‘HideForTemplate’] instead of $data[‘hide_for_template’] as shown in the sample code.
      3. When ‘piklist_part_add’ filter is applied, the callback’s first parameter ($data) is sent empty (array()). This call is done in piklist/includes/class-piklist.php file (line 693), and there you can see the call:

        array_merge(apply_filters(‘piklist_part_add’, array(), $folder), self::$processed_parts[$folder][‘parts’]);

        The second parameter (array()) should be $data variable. If you change it manually (NOT RECOMMENDED, I’ve done it only for TESTING!!) it works

      Please, tell me if this diagnostic is correct and when you would publish an update with this correction or the real one (I suppose i could be wrong, I really don’t know Piklist as deep as a Piklist developer).

      Thank you!

    • #8367
      Steve
      Keymaster

      Sorry, about this. We updated some filters and didn’t do a good job documenting it. I updated the tutorial with the new filter names.

      Please change the filter names:

      piklist_get_file_data TO piklist_part_data
      piklist_add_part TO piklist_part_process_callback

      Let me know if that works.

    • #8368
      noobie
      Participant

      Dear Steve

      Sorry for the late reply but it still does not work.

    • #8373
      noobie
      Participant

      Dear Steve

      Do you have any other alternatives to resolve this?

    • #8374
      riotxoa
      Participant

      I’ve found that the code in the updated tutorial works if you use $data[‘data’][‘hide_for_template’] instead of $data[‘hide_for_template’] in my_hide_for_template function.

      In general, the code works using $data[‘data’][‘whatever’] structure instead of $data[‘whatever’] in that function (my_hide_for_template).

    • #8375
      noobie
      Participant

      Dear riotxoa

      Thanks you for your assistance but it does not work as per your suggestions. The meta box just wouldn’t hide from the specific page template.

      Codes as per below:

      add_filter('piklist_part_data', 'my_custom_comment_block', 10, 2);
      function my_custom_comment_block($data, $folder)
      {
          // If not a Meta-box section than bail
          if($folder!= 'meta-boxes')
          {
              return $data;
          }
      
          // Allow Piklist to read our custom comment block attribute: "Hide for Template", and set it to hide_for_template
          $data['hide_for_template'] = 'Hide for Template';
      
          return $data;
      }
      
      add_filter('piklist_part_process_callback', 'my_hide_for_template', 10, 2);
      function my_hide_for_template($data, $type)
      {
          global $post;
      
          // If not a meta box than bail
          if($type != 'meta-boxes')
          {
              return $data;
          }
      
          // Check if any page template is set in the comment block
          if (!empty($data['data']['hide_for_template']))
          {
              // Get the active page template
              $active_template = pathinfo(get_page_template_slug($post->ID), PATHINFO_FILENAME);
      
              // Is there a page template set for this page?
              if(!empty($active_template))
              {
                  // Does the active page template match what we want to hide?
                  if (strpos($data['data']['hide_for_template'], $active_template) !== false)
                  {
                      // Change meta-box access to user role: no-role
                      $data['role'] = 'no-role';
                  }
              }
          }
          return $data;
      }
      
      /* ===== Comment Block in meta-boxes folder ===== */
      /*
      Title: Right Bar Section
      Description: Customized right bar section
      Post Type: page, promo
      Hide for Template: page-home
      Order: 3
      */
      
    • #8376
      riotxoa
      Participant

      My last suggestion, and I’m not sure that it will work:

      – Try replacing $data[‘data’][‘hide_for_template’] to $data[‘data’][‘hidefortemplate’] (remove ‘_’ characters from array slug).
      – Try replacing $data[‘role’] = ‘no-role’; to return; (return void).

      Let me know if it works.

    • #8377
      noobie
      Participant

      Dear riotxoa

      Once again thank you for your assistance. Unfortunately, it still does not work as expected.

      Codes as below:

      add_filter('piklist_part_data', 'my_custom_comment_block', 10, 2);
      function my_custom_comment_block($data, $folder)
      {
          // If not a Meta-box section than bail
          if($folder!= 'meta-boxes')
          {
              return $data;
          }
      
          // Allow Piklist to read our custom comment block attribute: "Hide for Template", and set it to hide_for_template
          $data['hidefortemplate'] = 'Hide for Template';
      
          return $data;
      }
      
      add_filter('piklist_part_process_callback', 'my_hide_for_template', 10, 2);
      function my_hide_for_template($data, $type)
      {
          global $post;
      
          // If not a meta box than bail
          if($type != 'meta-boxes')
          {
              return $data;
          }
      
          // Check if any page template is set in the comment block
          if (!empty($data['data']['hidefortemplate']))
          {
              // Get the active page template
              $active_template = pathinfo(get_page_template_slug($post->ID), PATHINFO_FILENAME);
      
              // Is there a page template set for this page?
              if(!empty($active_template))
              {
                  // Does the active page template match what we want to hide?
                  if (strpos($data['data']['hidefortemplate'], $active_template) !== false)
                  {
                      // Change meta-box access to user role: no-role
                      return;
                  }
              }
          }
          return $data;
      }
      
    • #8378
      Steve
      Keymaster

      Wow! We really messed up on this one. The array has been totally updated and we didn’t do a good job communicating.

      I just updated the tutorial and tested, and now it’s guaranteed to work.

      Let me know >

    • #8379
      noobie
      Participant

      Dear Steve

      Sorry for the trouble, I used the same codes from tutorial but is still not working.
      Please advice.

    • #8381
      noobie
      Participant

      Dear Steve

      May I know how you got it working?

    • #8382
      Steve
      Keymaster

      @noobie– Just to make sure you are using this properly:

      1) Add a new page
      2) Choose page template
      3) Save page
      4) Metaboxes should be gone

      Is that what you are doing?

    • #8383
      noobie
      Participant

      Dear Steve

      Yes, I have a page “Home” using template “Home Page” (filename: page-home.php).
      But the metabox is still showing.

      comment block as per below:

      /*
      Title: Right Bar Section
      Description: Customized right bar section
      Post Type: page, cpt_promo, cpt_news_event
      Hide for Template: page-home
      Order: 3
      */
      
    • #8384
      noobie
      Participant

      Dear Steve

      Apologies, the metabox is still NOT showing.

    • #8385
      Steve
      Keymaster

      @noobie– please zip by your theme and piklist code and email to [email protected]

      We’ll take a look.

    • #8386
      noobie
      Participant

      Dear Steve

      I have sent you, the file.
      Please have a look.

    • #8388
      noobie
      Participant

      Dear Steve

      May I check if you have received the theme file?

    • #8389
      Steve
      Keymaster

      Received… I’ll try to look at it today.

    • #8390
      Steve
      Keymaster

      @noobie– I was able to take a look much sooner 😉

      The Full Page template worked fine for me, it was the default template that didn’t. The reason is because the default template is saved as an empty string.

      I updated the tutorial. You just need to update the function that’s being called by “piklist_part_process_callback”.

      Let me know if that works for you >

    • #8391
      noobie
      Participant

      Dear Steve

      Thank you so much. It works now.

    • #8392
      Steve
      Keymaster

      @noobie– Great! Thank you for helping us work through this.

      closing this ticket.

Viewing 25 reply threads
  • The topic ‘Hiding Meta Box for Specific Template’ is closed to new replies.