- This topic has 25 replies, 3 voices, and was last updated 4 years, 4 months ago by
Steve.
-
AuthorPosts
-
-
August 17, 2017 at 11:49 pm #8359
noobieParticipantAs 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?
-
August 21, 2017 at 3:39 pm #8360
SteveKeymasterWhich version of Piklist are you using?
-
August 22, 2017 at 4:08 am #8362
noobieParticipantDear Steve
As per your questions, the below is the stated version.
Piklist Version 0.9.9.12
WordPress Helpers Version 1.17.0 -
August 22, 2017 at 11:35 am #8363
-
August 22, 2017 at 11:11 pm #8364
noobieParticipantDear 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;
} -
August 23, 2017 at 11:15 am #8365
riotxoaParticipantI have the same problem with both filters, and I think I’ve found the solution (or the real problem):
- As Steve says, you must use ‘piklist_part_data’ and ‘piklist_part_add’ filters.
- 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.
- 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!
-
August 27, 2017 at 7:50 am #8367
SteveKeymasterSorry, 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_callbackLet me know if that works.
-
August 29, 2017 at 12:12 am #8368
noobieParticipantDear Steve
Sorry for the late reply but it still does not work.
-
September 4, 2017 at 10:50 pm #8373
noobieParticipantDear Steve
Do you have any other alternatives to resolve this?
-
September 5, 2017 at 3:29 am #8374
riotxoaParticipantI’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).
-
September 5, 2017 at 4:04 am #8375
noobieParticipantDear 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 */ -
September 5, 2017 at 4:51 am #8376
riotxoaParticipantMy 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.
-
September 5, 2017 at 6:07 am #8377
noobieParticipantDear 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; } -
September 5, 2017 at 11:10 am #8378
SteveKeymasterWow! 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.
-
September 6, 2017 at 3:33 am #8379
noobieParticipantDear Steve
Sorry for the trouble, I used the same codes from tutorial but is still not working.
Please advice. -
September 8, 2017 at 4:06 am #8381
noobieParticipantDear Steve
May I know how you got it working?
-
September 8, 2017 at 10:22 am #8382
-
September 8, 2017 at 11:29 am #8383
noobieParticipantDear 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 */
-
September 8, 2017 at 12:05 pm #8384
noobieParticipantDear Steve
Apologies, the metabox is still NOT showing.
-
September 8, 2017 at 12:20 pm #8385
SteveKeymaster@noobie– please zip by your theme and piklist code and email to [email protected]
We’ll take a look.
-
September 10, 2017 at 11:34 pm #8386
noobieParticipantDear Steve
I have sent you, the file.
Please have a look. -
September 12, 2017 at 5:55 am #8388
noobieParticipantDear Steve
May I check if you have received the theme file?
-
September 12, 2017 at 10:36 am #8389
SteveKeymasterReceived… I’ll try to look at it today.
-
September 12, 2017 at 10:54 am #8390
SteveKeymaster@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”.
-
September 12, 2017 at 9:59 pm #8391
noobieParticipantDear Steve
Thank you so much. It works now.
-
September 14, 2017 at 2:16 pm #8392
-
-
AuthorPosts
- The topic ‘Hiding Meta Box for Specific Template’ is closed to new replies.