Tagged: ,

Viewing 6 reply threads
  • Author
    Posts
    • #8960
      mhweb
      Participant

      So, currently, I’m using a piklist list radio options, which I use to select and create posts with different formats depending on the option I select.

       piklist('field', array(
          'type' => 'radio'
          ,'field' => 'article_format'
          ,'label' => 'Normal'
          ,'description' => 'Choose article format. Default is Standard.'
          ,'value' => 'format-1' 
          ,'choices' => array(
            'format-1' => 'Standard'
            ,'format-2' => 'Background'
      ,'format-10' => 'Affiliate'
            ,'format-99' => 'Legacy' 
          )
          ,'on_post_status' => array(
            'value' => 'lock'
          )
        ));

      However, now I’m trying to create a post format that will include other options using custom fields, such as buy button, and other blocks, but I don’t want to show the options in the post editor unless the post format is select (radio button using the above code).

      How can I do this?

      I was trying this, but it doesn’t work:

      Note: I know the piklist array needs other options, I just put it as an example.

      <?php if ($article_format_ids == 'format-10') :
      /*
      Title: Affiliate Link
      Post Type: post
      Collapse: false
      Capability: manage_options
      Context: normal
      Priority: low
      Locked: false
      */
       piklist('field', array(
          'type' => 'text'
          ,'field' => 'affiliate_link1'
          ,'label' => 'Affiliate Link'
          ,'description' => 'Buy button'
          ,'value' => ''
          ,'help' => 'Character limit is 40.'
          )
        ));
      endif; ?>

      Any help will be appreciated.
      Thanks,

    • #8966
      Steve
      Keymaster

      Here are the docs are conditionally showing fields. It should help.

    • #8969
      mhweb
      Participant

      It works partially. Now, when I select the option, the fields will hide, but an empty metabox will remain visible (see image). Is there a way to hide the box as well?

      Thank you so much for the help.

      Attachments:
      You must be logged in to view attached files.
    • #8982
      Steve
      Keymaster

      Currently, conditions don’t work with meta boxes. I suggest you move all your code into ONE metabox.

    • #8996
      mhweb
      Participant

      Can use piklist_part_process so that the meta-box will only appear if the condition of another field is true after the post has been saved?

      https://piklist.github.io/docs/actions-filters/filters/piklist_part_process/

      For example:

      add_filter('piklist_part_process', function($part) {
        global $post;
      
        // Make sure this is a post edit screen
        if ( $post && 'post' === $post->post_type )
        {
          // Check that this is the right folder and part
          if ( 'meta-boxes' === $folder && 'field-affiliate-buy-button.php' === $part['part'] )
          {
            if ( 'format-10' !== get_post_meta($post->ID, 'pit_article_format', true) )
            {
              return false;
            }
          }
        }
        
        return $part;
      });

      If yes, where I write the filter? Does all functions go in the functions.php regardless?

      Thanks,

    • #8997
      Steve
      Keymaster

      Functions.php or your plugin file

    • #8998
      mhweb
      Participant

      OK, I thinking I’m not doing the right thing here.
      I’ve tried the piklist_part_process so that if another field is true on save, it will hide a meta-box block in the editor, but what I’m doing isn’t working, actually nothing happens when I save the post.

      Here’s a better context:

      I have a meta-box in the post editor backend with with radio options, which when on of the options is selected it loads post format for that particular post.
      Now, I need to add a format that includes addition options, such as product pricing and buy button, but I need these options to appear only if a particular format is selected. The options can appear on-demand or after a save the post.

      The problem that, as you said earlier, the conditional options for Piklist won’t remove the actual meta-box block if the format that triggers the options is not selected, and I want to avoid putting everything on a single mete-box, because I need to have the ability to organize and place metaboxes on different locations for a batter workflow.

      This is the metabox with the new options that need to appear only when a new format option is selected:
      File name: field-affiliate.php

      <?php
      /**
      Title: Affiliate
      Post Type: post
      Collapse: false
      */
       piklist('field', array(
         'type' => 'group'
      	 ,'field' => 'pit_affiliate_buy_button'
         ,'label' => 'Button'
         ,'description' => 'Affiliate button'
         ,'fields' => array(
                          array(
                          'type' => 'text'
                          ,'value' => 'button title'
                          ,'field' => 'button_title'
                          ,'label' => 'Affiliate button title'
                          ),
                         array(
                          'type' => 'text'
                          ,'value' => 'button url'
                          ,'field' => 'button_url'
                          ,'label' => 'URL'
                        ),
                         array(
                          'type' => 'text'
                          ,'value' => 'price now'
                          ,'field' => 'price_current'
                          ,'label' => 'Current Price'
                        ),
                        array(
                          'type' => 'text'
                          ,'value' => 'list price'
                          ,'field' => 'list_price'
                          ,'label' => 'List Price'
                        ),
                        array(
                          'type' => 'text'
                          ,'value' => 'discount price'
                          ,'field' => 'discount_price'
                          ,'label' => 'You Save'
                        )
         )// end of fields array
      
        ));
      ?>

      This is the metabox that appears in the right-side of the editor, and includes the radio options:
      File name: field-radio-formats.php

      <?php
      /*
      Title: Article Formats
      Post Type: post
      Collapse: false
      Capability: manage_options
      Context: side
      Priority: low
      Locked: false
      */
        piklist('field', array(
          'type' => 'radio'
          ,'field' => 'pit_article_format'
          ,'label' => 'Normal'
          ,'description' => 'Choose article format. Default is Standard.'
          ,'value' => 'format-1' 
          ,'choices' => array(
            'format-1' => 'Standard'
            ,'format-2' => 'Background'
            ,'format-3' => 'Content Video'
            ,'format-10' => 'Affiliate Single'
            ,'format-99' => 'Legacy' 
          )
          ,'on_post_status' => array(
            'value' => 'lock'
          )
        ));
        
      ?>

      Question 1: Is there any way to other than conditional option to show the entire metabox block only when a particular radio option is selected using the Article Formats metebox?

      Question 2: If piklist_part_process should work to accomplish the behavior, is there something wrong with this code:

      add_filter('piklist_part_process', function($part) {
        global $post;
      
        // Make sure this is a post edit screen
        if ( $post && 'post' === $post->post_type )
        {
          // Check that this is the right folder and part
          if ( 'meta-boxes' === $folder && 'field-affiliate-buy-button.php' === $part[''] )
          {
            if ( 'format-10' !== get_post_meta($post->ID, 'pit_article_format', true) )
            {
              return false;
            }
          }
        }
        
        return $part;
      });

      Thanks again.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.