• Hi, I’ve made a hack that lets you have different Magic Field groups for each Page depending on which template is chosen. (This has no effect on posts, only pages.) Perhaps you might consider adding this feature to a future version. Maybe even with a simple UI of some kind.
    — Thanks.

    File: mf_page_template_filter.php

    <?php
    /*
    Description: Allows your theme to control which Magic Fields groups are shown for which "Page" templates.
    Author:  Bill Friedrich
    License: GPL2
    */
    
    /*
    
      Installation:
      1) Copy this file (mf_page_template_filter.php) into the top level folder of your magic-fields-2 plugin folder.
      2) Insert the following code near the top of the Magic Fields' main.php file:
    
                  ...
    
                      along with this program; if not, write to the Free Software
                      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
                  @/
    
                  // ::Page Template Hack::
                  require_once('mf_page_template_filter.php');
                  // ::End Hack::
    
                  /@@
                   @ i18n
                   @/
                  global $mf_domain,$mf_pt_register;
                  ...
    
      3) Insert the following code in the mf_post_add_metaboxes() function
         in the admin/mf_post.php file (about line 55):
                  ...
    
                  //getting  the groups (each group is a metabox)
                  $groups = $this->get_groups_by_post_type($post_type);
    
                  // ::Page Template Hack::
                  if ($post_type == 'page')
                    mf_filter_groups_by_template($groups,$post->page_template);
                  // ::End Hack::
    
                  //creating the metaboxes
                  foreach( $groups as $group ) {
                  ...
    
      Usage:
      1) Add fields and groups to the "Page" post type in Magic Fields for all of your templates.
      2) Add as many mf_show_page_groups() function calls to your theme's functions.php as you need:
    
      for example:
    
        mf_show_page_groups('welcome-page.php', ['featured_image_content','extra_footer_content']);
        mf_show_page_groups('about-page.php', ['about_footer_content']);
    
        If you place the above code into your theme's functions.php file, any pages that use the
        "welcome-page.php" template will show the "featured_image_content" and "extra_footer_content" Magic
        Field groups.  Pages that use the "about-page.php" template will only have the "about_footer_content"
        group.  All other pages that use different templates will have no magic fields available.
    
        These have no affect on other post types.  Only "Pages" can be controlled with this hack.
    
        (Note, after selecting the template in the "Pages" admin UI, you will need to save the page in order to
        see the Magic Fields change.)
    */
    
    class mft_Template {
      public $visibleGroups = array();
    }
    
    $mft_templates = array();
    
    function mft_getTemplate($templateName) {
      global $mft_templates;
      $result = @$mft_templates[$templateName];
      if (!isset($result)) {
        $result = new mft_Template;
        $mft_templates[$templateName] = $result;
      }
      return $result;
    }
    
    function mf_filter_groups_by_template(&$groups,$page_template) {
      global $mft_templates;
      if (empty($mft_templates))
        return;
      $template = @$mft_templates[$page_template];
    
      foreach ( $groups as $key => $group ) {
        if (!
             (
               isset($template) &&
               in_array($group['name'],$template->visibleGroups)
             )
           )
          unset($groups[$key]);
    
      }
    }
    
    function mf_show_page_groups($templateName,$groupNames) {
      mft_getTemplate($templateName)->visibleGroups = $groupNames;
    }
    
    ?>

    https://www.ads-software.com/extend/plugins/magic-fields-2/

  • The topic ‘Hack to Use Different Groups for Different Page Templates’ is closed to new replies.