Support » Plugin: Food and Drink Menu » Adding meta boxes to sections

  • Resolved TheNewGuy_14

    (@thenewguy_14)



    Hey so i found this simple plugin to add meta boxes to taxonomies since you stated in one of your threads that the section uses the WordPress taxonomy. he uses the options tables to save the data. Its super nice cause i can add any meta i want, check box text image repeater boxes, to my categories in WordPress but i cant get it to work with your sections i thought maybe if i added it it would add the fields to your sections but that wasn’t the case. So now i am just wondering where i can add his plugin code to you plugin to get the boxes added.

    so obviously i would have to add his main class file and then add the rest of the set up here is the code

    //include the main class file
    require_once("/Tax-meta-class/Tax-meta-class.php");
    if (is_admin()){
    	/*
         * prefix of meta keys, optional
         */
    	$prefix = 'ba_';
    	/*
         * configure your meta box
         */
    	$config = array(
    		'id' => 'meta_box',          // meta box id, unique per meta box
    		'title' => 'Meta Box',          // meta box title
    		'pages' => array('category'),        // taxonomy name, accept categories, post_tag and custom taxonomies
    		'context' => 'normal',            // where the meta box appear: normal (default), advanced, side; optional
    		'fields' => array(),            // list of meta fields (can be added by field arrays)
    		'local_images' => false,          // Use local or hosted images (meta box images for add/remove)
    		'use_with_theme' => false          //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
    	);
    
    	/*
         * Initiate your meta box
         */
    	$menu_section_meta =  new Tax_Meta_Class($config);
    
    	/*
         * Add fields to your meta box
         */
    
    	//text field
    	$menu_section_meta->addText($prefix.'text_field_id',array('name'=> __('My Text ','tax-meta'),'desc' => 'this is a field desription'));
    	//textarea field
    	$menu_section_meta->addTextarea($prefix.'textarea_field_id',array('name'=> __('My Textarea ','tax-meta')));
    	//checkbox field
    	$menu_section_meta->addCheckbox($prefix.'checkbox_field_id',array('name'=> __('My Checkbox ','tax-meta')));
    	//select field
    	$menu_section_meta->addSelect($prefix.'select_field_id',array('selectkey1'=>'Select Value1','selectkey2'=>'Select Value2'),array('name'=> __('My select ','tax-meta'), 'std'=> array('selectkey2')));
    	//radio field
    	$menu_section_meta->addRadio($prefix.'radio_field_id',array('radiokey1'=>'Radio Value1','radiokey2'=>'Radio Value2'),array('name'=> __('My Radio Filed','tax-meta'), 'std'=> array('radionkey2')));
    	//date field
    	$menu_section_meta->addDate($prefix.'date_field_id',array('name'=> __('My Date ','tax-meta')));
    	//Time field
    	$menu_section_meta->addTime($prefix.'time_field_id',array('name'=> __('My Time ','tax-meta')));
    	//Color field
    	$menu_section_meta->addColor($prefix.'color_field_id',array('name'=> __('My Color ','tax-meta')));
    	//Image field
    	$menu_section_meta->addImage($prefix.'image_field_id',array('name'=> __('My Image ','tax-meta')));
    	//file upload field
    	$menu_section_meta->addFile($prefix.'file_field_id',array('name'=> __('My File ','tax-meta')));
    	//wysiwyg field
    	$menu_section_meta->addWysiwyg($prefix.'wysiwyg_field_id',array('name'=> __('My wysiwyg Editor ','tax-meta')));
    	//taxonomy field
    	$menu_section_meta->addTaxonomy($prefix.'taxonomy_field_id',array('taxonomy' => 'category'),array('name'=> __('My Taxonomy ','tax-meta')));
    	//posts field
    	$menu_section_meta->addPosts($prefix.'posts_field_id',array('args' => array('post_type' => 'page')),array('name'=> __('My Posts ','tax-meta')));
    
    	/*
         * To Create a repeater Block first create an array of fields
         * use the same functions as above but add true as a last param
         */
    
    	$repeater_fields[] = $menu_section_meta->addText($prefix.'re_text_field_id',array('name'=> __('My Text ','tax-meta')),true);
    	$repeater_fields[] = $menu_section_meta->addTextarea($prefix.'re_textarea_field_id',array('name'=> __('My Textarea ','tax-meta')),true);
    	$repeater_fields[] = $menu_section_meta->addCheckbox($prefix.'re_checkbox_field_id',array('name'=> __('My Checkbox ','tax-meta')),true);
    	$repeater_fields[] = $menu_section_meta->addImage($prefix.'image_field_id',array('name'=> __('My Image ','tax-meta')),true);
    
    	/*
         * Then just add the fields to the repeater block
         */
    	//repeater block
    	$menu_section_meta->addRepeaterBlock($prefix.'re_',array('inline' => true, 'name' => __('This is a Repeater Block','tax-meta'),'fields' => $repeater_fields));
    	/*
         * Don't Forget to Close up the meta box deceleration
         */
    	//Finish Meta Box Decleration
    	$menu_section_meta->Finish();
    }

    is there a way to add this to your code or can i just extend your class with this? I am a little new to this but i have made some nice widgets and nice custom websites i just don’t want to hack up you files to much in case you update them.

    thanks brian

    https://wordpress.org/plugins/food-and-drink-menu/

Viewing 5 replies - 1 through 5 (of 5 total)
  • So i just uploaded ACF and it is great cause i can easily add the image field but it has a lot of stuff i don’t really need it would be great to get other plugin to extend your class with the other plugin if it is possible…..the other plugin came from
    Bainternet
    and its just a lighter code so if you have any suggestions to get this to work that would be great but for now i am gonna customize the templates to get the ACF to work….thanks and let me know

    ok so i cant even get ACF to show the image i have searched every where but no ones answers are working….i have tried adding the get_field and all there code into my own template but it wont show up and even tried customizing your template to get it to show but nothing will work. I can get the image upload button to show up in the admin and even the thumbnail but it wont display on the front end…you got any ideas on how to get this image to show.

    Plugin Author NateWr

    (@natewr)

    Hi TheNewGuy_14,

    I’d recommend going with ACF or Pods or CMB2. Although they may seem like more than you need, they’re all well-coded plugins respected by experienced developers.

    The problem you’re going to run into, though, is that the Menu template in this plugin mashes together posts and taxonomies in a non-standard way for WordPress. That means that when a section is being rendered, you’re not within the traditional taxonomy loop that you would be in if you were viewing, for instance, the archive.php template.

    Instead what happens is that WordPress loads the single Menu post template. Then the templating handler for this plugin loads up each Menu Section assigned to that Menu. Each of these Menu Sections has a View controller which loads it’s own data.

    So you have two options for pulling in your custom field related to the taxonomy term.

    1. You can hook into fdm_load_section and add the data to the $this object. You’ll then be able to access that data from the menu-section.php template by going straight to the $this object.

    2. If the framework you’re using has a helper function that allows you to fetch the custom meta by passing in the taxonomy term, you can use that directly in the menu-section.php template.

    For instance, ACF has a the_field() function which you can use to print a custom meta field for a taxonomy term. If you were using ACF and your custom meta field was called my-custom-meta-field, you could do this in the menu-section.php template to print your custom field value:

    <?php the_field( 'my-custom-meta-field', 'fdm-menu-section_' . $this->id ); ?>

    Hope that gives you a bit of a start in the right direction.

    You’ll find some information in the developer documentation for Food and Drink Menu. But it’s still pretty sparse.

    Just so people know I couldn’t get the ACF to work i ended up using a plugin called Tax-Meta-Class you load activate the plugin and go into the tax-add-meta.php file and you can comment out all the fields you don’t want then you have to change the $config array so that he pages is set to what you want in this case i need to add images and a few more fields to the fdm-menu-section post page so i simple just changed that setting to fdm-menu-section so that my custom meta boxes were added there.

    heres the full code for that tax-add-metabox.php page

    <?php
    /*
    Plugin Name: Taxonomy meta class
    Plugin URI: https://en.bainternet.info
    Description: Tax meta class usage demo
    Version: 2.1.0
    Author: Bainternet, Ohad Raz
    Author URI: https://en.bainternet.info
    */
    
    //include the main class file
    require_once("Tax-meta-class.php");
    if (is_admin()){
      /*
       * prefix of meta keys, optional
       */
      $prefix = 'ba_';
      /*
       * configure your meta box
       */
      $config = array(
        'id' => 'meta_box',          // meta box id, unique per meta box
        'title' => 'Meta Box',          // meta box title
        'pages' => array('fdm-menu-section'),  // taxonomy name, accept categories, post_tag and custom taxonomies
        'context' => 'normal',            // where the meta box appear: normal (default), advanced, side; optional
        'fields' => array(),            // list of meta fields (can be added by field arrays)
        'local_images' => false,          // Use local or hosted images (meta box images for add/remove)
        'use_with_theme' => false          //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
      );
    
      /*
       * Initiate your meta box
       */
      $menu_section_meta =  new Tax_Meta_Class($config);
    
      /*
       * Add fields to your meta box
       */
    
      //text field
      $menu_section_meta->addText($prefix.'menu_section_navText',array('name'=> __('Navigation Text','tax-meta'),'desc' => 'Enter text for section navigation'));
      /*//textarea field
      $menu_section_meta->addTextarea($prefix.'textarea_field_id',array('name'=> __('My Textarea ','tax-meta')));
      //checkbox field
      $menu_section_meta->addCheckbox($prefix.'checkbox_field_id',array('name'=> __('My Checkbox ','tax-meta')));*/
      //select field
      $menu_section_meta->addSelect($prefix.'select_menu_id',array(null=>null,'selectLunch'=>'Lunch','selectDinner'=>'Dinner'),array('name'=> __('Menu Selection','tax-meta'), 'std'=>null));
      /*//radio field
      $menu_section_meta->addRadio($prefix.'radio_field_id',array('radiokey1'=>'Radio Value1','radiokey2'=>'Radio Value2'),array('name'=> __('My Radio Filed','tax-meta'), 'std'=> array('radionkey2')));
      //date field
      $menu_section_meta->addDate($prefix.'date_field_id',array('name'=> __('My Date ','tax-meta')));
      //Time field
      $menu_section_meta->addTime($prefix.'time_field_id',array('name'=> __('My Time ','tax-meta')));
      //Color field
      $menu_section_meta->addColor($prefix.'color_field_id',array('name'=> __('My Color ','tax-meta')));*/
      //Image field
      $menu_section_meta->addImage($prefix.'menu_section_image',array('name'=> __('Menu Section Image ','tax-meta')));
     /* //file upload field
      $menu_section_meta->addFile($prefix.'file_field_id',array('name'=> __('My File ','tax-meta')));
      //wysiwyg field
      $menu_section_meta->addWysiwyg($prefix.'wysiwyg_field_id',array('name'=> __('My wysiwyg Editor ','tax-meta')));
      //taxonomy field
      $menu_section_meta->addTaxonomy($prefix.'taxonomy_field_id',array('taxonomy' => 'category'),array('name'=> __('My Taxonomy ','tax-meta')));
      //posts field
      $menu_section_meta->addPosts($prefix.'posts_field_id',array('args' => array('post_type' => 'page')),array('name'=> __('My Posts ','tax-meta')));
    */
      /*
       * To Create a repeater Block first create an array of fields
       * use the same functions as above but add true as a last param
    
      $repeater_fields[] = $menu_section_meta->addText($prefix.'re_text_field_id',array('name'=> __('My Text ','tax-meta')),true);
      $repeater_fields[] = $menu_section_meta->addTextarea($prefix.'re_textarea_field_id',array('name'=> __('My Textarea ','tax-meta')),true);
      $repeater_fields[] = $menu_section_meta->addCheckbox($prefix.'re_checkbox_field_id',array('name'=> __('My Checkbox ','tax-meta')),true);
      $repeater_fields[] = $menu_section_meta->addImage($prefix.'image_field_id',array('name'=> __('My Image ','tax-meta')),true);
      */
      /*
       * Then just add the fields to the repeater block
       */
      //repeater block
      //$menu_section_meta->addRepeaterBlock($prefix.'re_',array('inline' => true, 'name' => __('This is a Repeater Block','tax-meta'),'fields' => $repeater_fields));
      /*
       * Don't Forget to Close up the meta box deceleration
       */
      //Finish Meta Box Decleration
      $menu_section_meta->Finish();
    }

    Then the hard part was getting it to loop and show what I wanted I had to my my own database query and do a bunch of stuff to get it to work right. But some might be smarter then me so you can find out how to query for the application of your choosing basically just look at your database and see how it is being saved and query that….

    I dont know why I couldn’t get the other plugins to work but this is my solution…

    Plugin Author NateWr

    (@natewr)

    Thanks for sharing TheNewGuy_14!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding meta boxes to sections’ is closed to new replies.