Tagged: Shortcode
- This topic has 6 replies, 3 voices, and was last updated 7 years, 3 months ago by
Steve.
-
AuthorPosts
-
-
October 24, 2014 at 1:25 pm #2620
msmithMemberI 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(); } ?> -
October 24, 2014 at 3:18 pm #2621
JasonKeymasterWhy 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
-
October 25, 2014 at 8:56 pm #2622
SteveKeymaster@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: piklistin your plugin comment block. -
October 27, 2014 at 10:38 pm #2623
msmithMemberThanks 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 . ')' ) )); -
October 27, 2014 at 10:58 pm #2624
-
October 27, 2014 at 11:27 pm #2625
msmithMemberThanks again Steve. That looks like it solved my second issue.
-
October 28, 2014 at 9:23 am #2626
SteveKeymasterGreat! closing ticket.
-
-
AuthorPosts
- The topic ‘Shortcode in Piklist’ is closed to new replies.