- This topic has 4 replies, 2 voices, and was last updated 7 years, 5 months ago by
Steve.
-
AuthorPosts
-
-
August 25, 2014 at 12:35 pm #2296
ralmestroMemberHey…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
-
August 25, 2014 at 7:48 pm #2298
ralmestroMemberreading 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!
-
August 26, 2014 at 10:40 am #2299
SteveKeymaster@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 >
-
August 26, 2014 at 12:33 pm #2305
-
August 26, 2014 at 12:42 pm #2308
SteveKeymaster@ralmestro– Great! Let us know if you need anything else. Closing ticket.
-
-
AuthorPosts
- The topic ‘Metabox for pages except for a custom page template?’ is closed to new replies.