TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Tạo CPT với Pods Framework

 
Pods framework  là giải pháp tổng thể cho bạn tạo Custom Post Type, tạo kiểu dữ liệu mới tách ra ngoài bảng wp_posts. Bạn có thể làm mọi thứ với Pods Framework như:
- Sửa/thêm post type: post, page, comment,...
- Hỗ trợ nhiều kiểu trường: email, phone, wysiwyg, currency, date-time, syntax highlighting, media image-attachment (single-multi)
- Thiết kế template cho nội dung post type.
- Tạo trang cài đặt Theme options.
- Kiểu nội dung lưu ở bảng ngoài, không thuộc chuẩn wordpress. ie: wp_posts
 
 
Để tạo post type mới, bạn nhấn vào 'Pods Admin > Add New'.
 
Quản lý Pod ở trang riêng.
Nhấc Pod content Manager ra page menu riêng, không sử dụng mặc định menu quản lý post type tạo bởi Pods, bạn có thể tùy biến quản lý nội dung post type thông qua hàm pods_ui.
Lợi ích: bạn có thể tùy biến pods manager như ẩn actions bulk, thêm action links , giới hạn hiển thị fields. Lúc này bạn có toàn quyền cho những thành phần nào hiển thị, mặc định nếu ko chỉ định tất cả những thành phần đều show ra.
Tạo menu sử dụng wordpress API. Ví dụ sau quản lý kiểu dữ liệu Book.
//books submenu
add_submenu_page('lg-pods', 'Books', 'Books', 'manage_options', 'lg-books', 'display_books_page');
/**
* Defines books page = management and edit
*/
function display_books_page() {
    $object = pods('books');
 
    //for this pod type we will also use all available fields
    $fields = array();
    foreach($object->fields as $field => $data) {
        $fields[$field] = array('label' => $data['label']);
    }
 
    //adding few basic parameters
    $object->ui = array(
        'item'   => 'book',
        'items'  => 'books',
        'fields' => array(
            'add'       => $fields,
            'edit'      => $fields,
            'duplicate' => $fields,
            'manage'    => $fields
        ),
       'actions_bulk' => array(
            //adds custom function for our own bulk action - empty books' stock
            'zero-me' => array(
                'label' => 'Empty stock',
                'callback' => 'empty_stock'
            ),
            //adds built in delete function
            'delete' => array(
                'label' => 'Delete',
            ),
        ),
    );
 
    pods_ui($object);
}
/**
* Bulk action function - empties stock for all books selected by user
*/
function empty_stock($obj) {
 
    //checks if parameter exists
    //if yes - it means that we successfully updated data entries and we can display message to the user
    //if no - it means that we are running function for the first time
    if ( ! isset( $_GET['updated_bulk'] )) {
 
        //$obj->bulk contains all IDs selected by user
        $ids = $obj->bulk;
 
        //if we successfully change our items we need to redirect user afterwards
        //false by default
        $success = false;
 
        //lets count how many items were affected by our function
        $items_affected = 0;
 
        if(!empty( $ids )) {
           foreach( $ids as $pod_id ) {
 
                //sanitizing ID
                $pod_id = pods_absint( $pod_id );
 
                //if the ID is empty - jump out of the current loop iteration
                if(empty($pod_id)) {
                    continue;
                }
 
                //getting our pod data entry
                $book_pod = pods( 'mytable', $pod_id );
 
                //setting the stock to desired number
                $book_pod->save( 'stock', 0 );
 
                //we have successfully changed item ( so we need to redirect later )
                $success = true;
 
                //increase the number of updated items
                $items_affected++;
            }
        }
 
        //checking if our bulk action completed successfully
        if ( $success ) {
 
            //if yes - redirect our user to manage page
            //this redirect us again to our bulk action handler
            pods_redirect( pods_var_update( array( 'action_bulk' => 'zero-me', 'updated_bulk' => $items_affected ), array( 'page', 'lang', 'action', 'id' ) ) );
 
        } else {
 
            //otherwise display an error
            $obj->error( __( "<strong>Error:</strong> Cannot update stock.", 'localization-domain' ) );
 
        }
 
 
    } else { //if our get parameter was set we can display 'success' message
 
            $obj->message( __( "<strong>Success!</strong> We have successfully updated stock numbers for <strong>". $_GET['updated_bulk'] ."</strong> entry(-es).", 'localization-domain' ) );
            unset( $_GET[ 'updated_bulk' ] );
 
    }
 
    //clean up
    $obj->action_bulk = false;
    unset( $_GET[ 'action_bulk' ] );
 
    //show manage screen
    $obj->manage();
}
Ghi nhớ:
 
Quản lý Action.
 
Bulk Action.
Xem: http://lowgravity.pl/blog/bulk-actions-in-pods-framework-2-2/
 
Actions Links:
Ở phiên bản mới nhất của Pods, sử dụng tham số 'actions_custom'.
$object->ui = array(
   'actions_custom' => array(
          'link1' => array(
               'label'=>'Link 1',
               'link'=>'your-link'          #note: this is important,for this you can able to display link. Otherwise your link never show
          ),
          'link2' => array(
               'label'=>'Link 2',
               'link'=>'your-link'
          ),
          ...
     ),
);
 
pods_ui($object);
 
- Sử dụng biến template.
Ví dụ tạo liên kết với pod item hiện tại trong loop.
$object->ui = array(
   'actions_custom' => array(
          'link1' => array(
               'label'=>'Link 1',
               'link'=>'http://localhost/wp1/mytable/{@permalink}/{@id}'
          )
     ),
);
pods_ui($object);
 
- Bạn cũng có thể xắp xếp lại hoặc thêm mới action link thông qua filter 'pods_ui_row_actions'. Tuy nhiên bạn không được phép sử dụng magic tag cho dữ liệu pods item hiện tại như cách đưa trực tiếp vào actions_custom ở trên.
add_filter( 'pods_ui_row_actions', 'reorder_pods_ui_actions' );
function reorder_pods_ui_actions ( $actions) {
    $reordered_actions = array(
        'delete' => 'Delete', // this will get merged into and replaced with the link
        'edit' => 'Edit', // this will get merged into and replaced with the link
          'new_link'=>'<span class="edit action-link1"><a href="#">Link title</a></span>'
    );
 
    $actions = array_merge( $reordered_actions, $actions ); // merge links, but keep the original order
 
    return $actions;
}
 
- Sử dụng hàm callback bạn muốn gọi khi mỗi item được chọn.
'actions_bulk' => array(
    //adds custom function for our own bulk action - empty books' stock
    'zero-me' => array(
        'label' => 'Empty stock',
        'callback' => 'empty_stock'
    ),
)
Chi tiết: https://github.com/kamilgrzegorczyk/pods-framework-examples/blob/master/pods-ui/simple%20one-file%20plugin/index.php
 
Sửa dữ liệu Pods.
Chúng ta có thể sửa quản lý pods đã tồn tại bằng cách sử dụng filter "pods_admin_ui_{$pod_name}". Ví dụ:
function pods_ui_test($object) {
     $object['actions_custom']['link1']= array('label'=>'link 1','link'=>"http://hoangweb.com");
    return $object;
}
add_filter( 'pods_admin_ui_mytable', 'pods_ui_test' );
 
Chú ý: Bạn có thể in biến $object để xem các tham số định nghĩa quản lý pods, và thay đổi lại nếu muốn.
Ví dụ, tùy chỉnh thông tin trường hiển thị trong giao diện quản lý Pods.
function pods_ui_test ( $ui ) {
    // Test on UI Column
    $ui[ 'fields' ][ 'manage' ] = array(
        'city' => 'City',
        'region' => 'Region'
    );
 
    return $ui;
}
 
add_filter( 'pods_admin_ui_cities', 'pods_ui_test' );
Made with help of Dr.Explain

Unregistered version