Tagged: 

Viewing 6 reply threads
  • Author
    Posts
    • #2620
      msmith
      Member

      I am trying to create a shortcode that will list posts from a custom post type. I remember reading somewhere that with piklist I can create features that piklist doesn’t support using the same code as a non-piklist plugin. I have tried doing this but whenever I use a shortcode, it just shows the shortcode, nothing is rendered. I have my main function code below.

      <?php
      /*
      Plugin Name: MBA Membership
      Plugin URI: https://accessdrive.net/
      Description: Custom plugin designed for the Mississippi Beekeepers Association for managing their members.
      Version: 1.0
      Author: Michael Smith (Accessdrive)
      Author URI: https://accessdrive.net/
      Plugin Type: Piklist
      */
      
      add_action('init', 'my_init_function');
      
      function my_init_function()
      {
        if(is_admin())
        {
         include_once('pc.php');
       
         if (!piklist_checker::check(__FILE__))
         {
           return;
         }
      
         //Set Filter and Actions for Plugin
         add_filter('piklist_admin_pages', 'mba_member_settings');
         add_filter('piklist_post_types', 'mba_member_data_cpt');
         add_action('init', 'mba_list_members_shortcode');
         
        }
      }
      
      function mba_member_settings($pages){
       
            $pages[] = array(
              'page_title' => 'MBA Membership Settings'
              ,'menu_title' =>  'Membership Settings'
              ,'sub_menu' => 'options-general.php'
              ,'capability' => 'manage_options'
              ,'menu_slug' => 'mba_member'
              ,'setting' => 'mba_member'
              //,'icon_url' => plugins_url('piklist/parts/img/piklist-icon.png') 
              //,'icon' => 'piklist-page'
              //,'single_line' => false
              ,'default_tab' => 'General'
            );
         
            return $pages;
          
        }
      
      function mba_member_data_cpt($post_types){
      
        $post_types['member_data'] = array(
          'labels' => piklist('post_type_labels', 'Members')
        ,'title' => __(uniqid('',TRUE))
          ,'public' => true
          ,'capability_type' => 'post'
          /*, 'edit_columns' => array(
              'first_name' => __('First Name')
              ,'last_name' => __('Last Name')
              ,'status' => __('Status')
            )*/
          ,'hide_meta_box' => array(
              'slug'
              ,'author'
            )
          ,'menu_icon' => 'dashicons-groups'
          ,'status' => array(
              'draft' => array(
                  'label' => 'New'
                )
          ,'pending' => array(
            'label' => 'Pending'
            )
            
              ,'active' => array(
                  'label' => 'Active'
                )
              ,'inactive' => array(
                  'label' => 'Inactive'
                )
            )
          );
        return $post_types;
      }
        // Add Shortcode
      function mba_list_members_shortcode() {
      
      add_shortcode( 'mba-list-members', 'mba_members_list_code' );
        }
        
        // Shortcode Function
        function mba_members_list_code() {
         // WP_Query arguments
       $args = array (
          'post_status'            => 'active',
          'meta_query'             => array(
            array(
              'key'       => 'publicly_listed',
              'value'     => 'true',
              'compare'   => '=',
            ),
          ),
        );
      
        // The Query
        $member = new WP_Query( $args );
      
        // The Loop
        if ( $member->have_posts() ) {
          while ( $member->have_posts() ) {
            $member->the_post();
            // do something
      	  echo "<p>We found <i><b>something</b></i>!</p>";
          }
        } else {
          // no posts found
          echo "<p>No members are available to be shown. Please try again later</p>";
        }
      
        // Restore original Post Data
        wp_reset_postdata();
        }
      	
      
      ?>
    • #2621
      Jason
      Keymaster

      Why are you hooking the my_init_function to the init filter? I’d just cut the code out of the function and run it directly. The plugin file is running at its intended time, you don’t need to put all that in the init hook unless you’re doing something specific to that point. Chances are since you’re doing that the shortcode function isn’t running since you’re already in the init context when you go to add it.

      Hope this helps!

      ~Jason

    • #2622
      Steve
      Keymaster

      @msmith– You can’t hook to ‘init’ inside a function that hooks to ‘init’. Just include mba_list_members_shortcode() inside my_init_function().

      function my_init_function()
      {
        if(is_admin())
        {
         include_once('pc.php');
       
         if (!piklist_checker::check(__FILE__))
         {
           return;
         }
      
         //Set Filter and Actions for Plugin
         add_filter('piklist_admin_pages', 'mba_member_settings');
         add_filter('piklist_post_types', 'mba_member_data_cpt');
         
         mba_list_members_shortcode();
         
        }
      }
      

      Also, don’t forget Plugin type: piklist in your plugin comment block.

    • #2623
      msmith
      Member

      Thanks Jason and Steve. That solved my issue. I do have one more question regarding a drop down field. The only one I have in my plugin displays fine, but whenever I change the value from the first option and save, it updates the database, but it still shows that same option in the back end. I don’t understand if this is expected or if I have an error in my code. I posted a copy of the field code below.

      piklist('field', array(
      'type' => 'select'
      ,'scope' => 'membership_level'
      ,'field' => 'membership_level'
      ,'label' => 'Membership Level'
      ,'attributes' => array(
        'class' => 'text'
      )
      ,'choices' => array(
        '1' =>  'Associate ($' . $a . ')'
        ,'2' => 'Hobby ($' . $h . ')'
        ,'3' => 'Commercial ($' . $c . ')'
      )
      ));
    • #2624
      Steve
      Keymaster

      @msmith– Remove the scope parameter and let Piklist automatically set it. In most cases you never have to set the scope.

    • #2625
      msmith
      Member

      Thanks again Steve. That looks like it solved my second issue.

    • #2626
      Steve
      Keymaster

      Great! closing ticket.

Viewing 6 reply threads
  • The topic ‘Shortcode in Piklist’ is closed to new replies.