Support » Fixing WordPress » styling plugin page like posts.php style

  • Resolved saeb

    (@czdah13)


    Hi there, I use gravity forms
    in form I have a city field ,
    and I want make every admin of site just can see only entries of one city;
    gravity forms doesn’t have such a feature , so I am trying to make a plugin to do that.
    from gravity api documents:
    https://docs.gravityforms.com/api-functions/#get-entries

    I used this command to get my filtered entries:

    $search_criteria = array ('status'        => 'active',
        'field_filters' => array(
            'mode' => 'any',
            array(
                'key'   => '5',
                'value' => 'حاجی آباد'
            )
    ));
    $entries = GFAPI::get_entries( '3', $search_criteria );

    and it returns an array,
    Now I wanna make a page to show these to user.
    and I want to know how I can make a page with post screen style:
    https://codex.wordpress.org/Posts_Screen
    I know how to make a page , I don’t know how to make it to have Posts screen style

    • This topic was modified 2 years, 11 months ago by saeb.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Do you want people to be able to edit those entries, or just see the values?

    Are you making a new admin page, or a front-end page?

    If you’re making a new admin page, take a look at the WP_List_Table class. This is used to create the “post list” admin page types (and is even used by Gravity Forms to display entries and notifications, etc).

    Moderator bcworkz

    (@bcworkz)

    Sorry for a dumb question, but I’m unfamiliar with Gravity Forms: The code returns an array of what? I’m guessing post objects. Thus you want these posts to be listed in the same manner as the default posts screen. Do you want this page to appear in the front end or the back end admin area?

    Do you need all the “bells and whistles” of the posts screen? I.E. different views, bulk actions, filters, etc.? Or do you just want the actual table itself? Does the list need to paginated or will all on one page suffice?

    If you need it all, you should extend the WP_List_Table class just as is done for the posts list table. This can get rather involved, but it’s the best way to get it all. If you just need the table itself, it may be easier to create a foreach loop that outputs the table rows and cells using the exact same classes and structure the posts list table does so applying the admin stylesheet to your page results in the same appearance, sans all the “bells and whistles”.

    yes I just want to show them . but I want to can order them or filter the number of items per page,
    my array is like this :

    $entry_list = [];
    
    for ($i = 0; $i < count($entries); $i++ )
    {
    	array_push ($entry_list,array(
    	$entries[$i]['id'],
    	$entries[$i]['date_created'],
    	$entries[$i]['1.3'],
    	$entries[$i]['1.6'],
    	$entries[$i][19],
    	$entries[$i][10],
    	$entries[$i][37]
    						));
    }

    sorry for my weak English , and I am beginner , actually this is my first time to write a plugin or even using php!

    Hi @czdah13

    Are you able to display the entries in a different way (e.g. in a simple HTML table?). This is a fairly complex thing you’re trying to do, so maybe smaller steps will make it easier.

    You could also take a look at the Gravity View plugin (https://gravityview.co/), which will allow you to display GForm entries to users based on filters and permissions.

    @bcworkz, Gravity Forms creates a series of custom tables, which include the forms and entry fields. They are displaying this information in a WP_List_Table which they’ve extended with some neat things (like column selection).

    • This reply was modified 2 years, 11 months ago by ancawonka.

    @ancawonka
    so far I wrote this for test :

    <?php
    /* Plugin Name: Gravity forms access level 
    Plugin URI: https://sae13.ir/archives/tag/wordpress
    Description: Show Only part of Gravity form data which user hass allow to access
    Version: 0.0.0.1
    Author:Saeb Molaee
    Author URI:http://twitter.com/sae13
    License:     GPL2
     
    {Plugin Name} is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    any later version.
     
    {Plugin Name} is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
     
    You should have received a copy of the GNU General Public License
    along with {Plugin Name}. If not, see {License URI}.
    
    */
    
    //add_action( 'admin_notices', 'main_gf_access_level' );
    /* how to define page in admin menu:
    add_menu_page(
        string $page_title,
        string $menu_title,
        string $capability,
        string $menu_slug, 
        callable $function = '',
        string $icon_url = '',
        int $position = null
    );
    
    */
    $plugin_dir_path = dirname(__FILE__);
    include($plugin_dir_path.'/class-wp-list-table.php');
    
    function main_gf_access_level(){
    $forms = GFAPI::get_forms();
    var_dump( $forms );}
    function gf_access_level_page_menu()
    {
        add_menu_page(
            'مشاهده داده ها',
            'نمایش داده ها',
            'read',
            'wporg',
            'gf_access_level_entity_page',
            plugin_dir_url(__FILE__) . 'images/icon_wporg.png',
            20
        );
    }
    add_action('admin_menu', 'gf_access_level_page_menu');
    
    ?>
    <?php
    function gf_access_level_entity_page()
    {
    
    $form_id = '3';
    //$entry = GFAPI::get_entries( $form_id );
    //var_dump($entry);
    /*
    foreach($entry[0] as $key => $value){
    echo "key = <br>".$key."<br>"."and value = <br>".$value."<br><br>";}
    if ($entry[0][5]=="حاجی آباد"){
    echo "city is حاجی آباد";} else {echo 1;}
    */
    // echo strpos('123456','0');
    // if (strpos('saeb','s')){echo 1;}else {echo 2;}
    $search_criteria = array ('status'        => 'active',
        'field_filters' => array(
            'mode' => 'any',
            array(
                'key'   => '5',
                'value' => 'حاجی آباد'
            )
    ));
    $entries = GFAPI::get_entries( '3', $search_criteria );
    // foreach ($entries as $key => $value){
    // echo '<br>'.$entries[$key][5];
    // }
    $entry_list = [];
    
    for ($i = 0; $i < count($entries); $i++ )
    {
    	array_push ($entry_list,array(
    	$entries[$i]['id'],
    	$entries[$i]['date_created'],
    	$entries[$i]['1.3'],
    	$entries[$i]['1.6'],
    	$entries[$i][19],
    	$entries[$i][10],
    	$entries[$i][37]
    						));
    }
    ?>
    <table class="wp-list-table widefat fixed striped posts">
    	<thead>
    <tr>
    <td>
     "شماره فرم" 
     </td>
     <td>
    "تاریخ"
     </td>
     <td>
    "نام"
     </td>
    </tr>
    </thead>
    <tbody id="the-list">
    <?php
    
    for($i=0;$i < count($entry_list);$i++){
    echo "<tr><td><a href='".site_url()."/wp-admin/admin.php?page=gf_entries&view=entry&id=3&lid="
    .$entry_list[$i][0]."'>".$entry_list[$i][0]."</a></td><td>".
    "<a href='".site_url()."/wp-admin/admin.php?page=gf_entries&view=entry&id=3&lid="
    .$entry_list[$i][0]."'>".date_i18n( get_option( 'date_format' ), strtotime($entry_list[$i][1])).
    "</a></td><td><a href='".site_url()."/wp-admin/admin.php?page=gf_entries&view=entry&id=3&lid="
    .$entry_list[$i][0]."'>".$entry_list[$i][2]."</td></tr></a>";
    
    }
    ?></tbody></table><?php
    }
    
    ?>

    it works. till now,
    https://screenshots.firefox.com/7Djn73h2orDdJhEu/localhost

    but It doesnt have order by and limit item number per page

    my plugin code :
    https://beepaste.io/view/arU8dI
    array example :
    https://beepaste.io/view/VCYu1A
    I try to make table base of this:
    https://www.smashingmagazine.com/2011/11/native-admin-tables-wordpress/

    • This reply was modified 2 years, 11 months ago by saeb.
    • This reply was modified 2 years, 11 months ago by saeb.
    • This reply was modified 2 years, 11 months ago by saeb.

    I`m trying to make better list base of this:
    https://www.smashingmagazine.com/2011/11/native-admin-tables-wordpress/

    I tried to change prepare_item() for array :

    $plugin_dir_path = dirname(__FILE__);
    include($plugin_dir_path.'/class-wp-list-table.php');
    class Link_List_Table extends WP_gf_List_Table {
    	
    	   /**
    		* Constructor, we override the parent to pass our own arguments
    		* We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
    		*/
    		public $gf_filter;
    		function __construct() {
    		   parent::__construct( array(
    		  'singular'=> 'wp_list_text_link', //Singular label
    		  'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
    		  'ajax'   => false //We won't support Ajax for this table
    		  ) );
    		}
    		function get_columns() {
    			return $columns= array(
    			   'col_gf_id'=>__('شماره درخواست'),
    			   'col_gf_fname'=>__('نام'),
    			   'col_gf_lname'=>__('نام خانوادگی'),
    			   'col_gf_phone'=>__('شماره تماس'),
    			   'col_gf_socialid'=>__('کدملی')
    			);
    		 }
    		 /**
     * Decide which columns to activate the sorting functionality on
     * @return array $sortable, the array of columns that can be sorted by the user
     */
    public function get_sortable_columns() {
    	return $sortable = array(
    		'col_gf_id' => 'gf_entry_id',
    		'col_gf_lname'=>'gf_entry_lname',
    	);
    	function prepare_items() {
    		global $_wp_column_headers;
    		$search_criteria = array ('status'        => 'active',
    		'field_filters' => array(
    			'mode' => 'any',
    			array(
    				'key'   => '5',
    				'value' => $gf_filter
    			)
    	));
    	$entries = GFAPI::get_entries( '3', $search_criteria );
    	$entry_list = [];
    	
    	for ($i = 0; $i < count($entries); $i++ )
    	{
    		array_push ($entry_list,array(
    		$entries[$i]['id'],
    		$entries[$i]['date_created'],
    		$entries[$i]['1.3'],
    		$entries[$i]['1.6'],
    		$entries[$i][19],
    		$entries[$i][10],
    		$entries[$i][37]
    							));
    	}
    
    		$screen = get_current_screen();
    	 
    		/* -- Preparing your query -- */
    		
    		
    		   //The pagination links are automatically built according to those parameters
    	 
    		/* -- Register the Columns -- */
    		   $columns = $this->get_columns();
    		   $_wp_column_headers[$screen->id]=$columns;
    	 
    		/* -- Fetch the items -- */
    		   $this->items = $entry_list;
    	 }
    	 function display_rows() {
    		
    		   //Get the records registered in the prepare_items method
    		   $records = $this->items;
    		
    		   //Get the columns registered in the get_columns and get_sortable_columns methods
    		   list( $columns, $hidden ) = $this->get_column_info();
    		
    		   //Loop for each record
    		   if(!empty($records)){foreach($records as $rec){
    		
    			  //Open the line
    				echo '< tr id="record_'.$rec[0].'">';
    			  foreach ( $columns as $column_name => $column_display_name ) {
    		
    				 //Style attributes for each col
    				 $class = "class='$column_name column-$column_name'";
    				 $style = "";
    				 if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
    				 $attributes = $class . $style;
    		
    				 //edit link
    				 $editlink  = '/wp-admin/admin.php?page=gf_entries&view=entry&id=3&lid='.(int)$rec[0];
    		
    				 //Display the cell
    				 switch ( $column_name ) {
    					case "col_gf_id":  echo '< td '.$attributes.'>'.stripslashes($rec[0]).'< /td>';   break;
    					case "col_gf_fname": echo '< td '.$attributes.'>'.stripslashes($rec[1]).'7< /td>'; break;
    					case "col_gf_lname": echo '< td '.$attributes.'>'.stripslashes($rec[2]).'< /td>'; break;
    					case "col_gf_phone": echo '< td '.$attributes.'>'.$rec[3].'< /td>'; break;
    					case "col_gf_socialid": echo '< td '.$attributes.'>'.$rec[4].'< /td>'; break;
    				 }
    			  }
    		
    			  //Close the line
    			  echo'< /tr>';
    		   }}
    		}
    
    

    and calling class by :

    $wp_list_table = new Link_List_Table();
    $wp_list_table->gf_filter="حاجی آباد";
    $wp_list_table->prepare_items();
    $wp_list_table->display();

    but I got this error :

    
    function WP_List_Table::prepare_items() must be over-ridden in a sub-class.

    my complete plugin code:
    https://beepaste.io/view/jkYyr2
    and example of my array :
    https://beepaste.io/view/VCYu1A

    • This reply was modified 2 years, 11 months ago by saeb.
    • This reply was modified 2 years, 11 months ago by saeb.

    Take a look at the function WP_List_Table::prepare_items() – you need to add a method to your class (Link_List_Table) that takes the data from the gravity forms array and converts it to the arrays used in the list table.

    The Gravity Forms Plugin has a class that inherits from WP_List_Table – take a look at what they did in their prepare_items() functions.

    Edit: I see that you did all this already. You have a missing } before the function declaration for prepare_items on line 72 – you didn’t close your get_sortable_columns function, so you declared the prepare_items function inside another function.

    • This reply was modified 2 years, 11 months ago by ancawonka.
    • This reply was modified 2 years, 11 months ago by ancawonka.

    @ancawonka thanks a lot,
    this article helped me to make something. but I need make $paging function to get arrays 20 by 20 from get_entries() and show them,
    http://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/
    for now my code is like this :
    https://beepaste.io/view/NOeJhD

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘styling plugin page like posts.php style’ is closed to new replies.