首先,我是wordpress和PHP的新手.我想要一个奖励模块,我可以有多个类别,每个类别有多个获奖者.
我已经在CI(CodeIgniter)中实现了这一点,但现在想在wordpress中实现类似的东西.我可以在哪里有一个类别,那个类别可以有多个获胜者,各自的位置.我将能够执行简单的crud功能. 有什么建议可以在wordpress中实现这个模型,或者是否有任何插件可用,功能有些类似?
我应该为此创建自己的插件,并且一开始我尝试过这个
$your_db_name = $wpdb->prefix . 'your_db_name';
// function to create the DB / Options / Defaults
function your_plugin_options_install() {
global $wpdb;
global $your_db_name;
// create the ECPT MetaBox database table
if($wpdb->get_var("show tables like '$your_db_name'") != $your_db_name)
{
$sql = "CREATE TABLE " . $your_db_name . " (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`field_1` mediumtext NOT NULL,
`field_2` tinytext NOT NULL,
`field_3` tinytext NOT NULL,
`field_4` tinytext NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.PHP');
dbDelta($sql);
}
}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'your_plugin_options_install');
有什么建议?提前致谢 解决方法: 无需创建自定义数据库表来执行此操作.
> Register a custom post type奖项,包括一些功能(标题,编辑和图像). >添加metabox以处理获奖者(registered users). >附上custom taxonomy奖品. >使用它,您可以使用默认的WP_Query 功能并创建常规主题模板或小部件或短代码来输出结果.
有很好的插件来管理CPT和自定义字段使这很容易,但这里是上面列出的小插件:
<?PHP
/**
* Plugin Name: (SO) Awards, prizes and winners
* Plugin URI: https://stackoverflow.com/q/25936506/1287812
* Version: 0.1
* Author: brasofilo
*/
class SO_25936506
{
private $cpt = 'reward';
public function __construct()
{
add_action( 'init', array( $this, 'init' ) );
add_action( 'save_post', array( $this, 'save' ), 10, 2 );
}
public function init() {
$labels = array(
'name' => _x( 'Rewards', 'post type general name' ),
'singular_name' => _x( 'Reward', 'post type singular name' ),
'add_new' => _x( 'Add New', 'reward' ),
'add_new_item' => __( 'Add New Reward' ),
'edit_item' => __( 'Edit Reward' ),
'new_item' => __( 'New Reward' ),
'all_items' => __( 'All Rewards' ),
'view_item' => __( 'View Reward' ),
'search_items' => __( 'Search Rewards' ),
'not_found' => __( 'No rewards found' ),
'not_found_in_trash' => __( 'No rewards found in Trash' ),
'parent_item_colon' => '',
'menu_name' => __( 'Rewards' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => _x( 'reward', 'URL slug' ) ),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'taxonomies' => array('prize'),
'supports' => array( 'title', 'editor', 'thumbnail' ),
'register_Meta_Box_cb' => array( $this, 'add_MetaBox' )
);
register_post_type( $this->cpt, $args );
register_taxonomy('prize', $this->cpt, array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the wordpress Admin UI
'labels' => array(
'name' => _x( 'Prizes', 'taxonomy general name' ),
'singular_name' => _x( 'Prize', 'taxonomy singular name' ),
'search_items' => __( 'Search Prizes' ),
'all_items' => __( 'All Prizes' ),
'parent_item' => __( 'Parent Prize' ),
'parent_item_colon' => __( 'Parent Prize:' ),
'edit_item' => __( 'Edit Prize' ),
'update_item' => __( 'Update Prize' ),
'add_new_item' => __( 'Add New Prize' ),
'new_item_name' => __( 'New Prize Name' ),
'menu_name' => __( 'Prizes' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'prizes', //
'with_front' => false,
'hierarchical' => true
),
));
}
public function add_MetaBox()
{
add_Meta_Box(
'winners',
'Winners',
array( $this, 'do_MetaBox' ),
$this->cpt,
'normal',
'default',
array( // to build the custom fields
'_winner_gold' => array( 'title'=>'Gold', 'desc' => 'G desc' ),
'_winner_silver' => array( 'title'=>'Silver', 'desc' => 'S desc' ),
'_winner_bronze' => array( 'title'=>'Bronze', 'desc' => 'B desc' )
)
);
}
// back function of add Meta Box that displays the Meta Box in the post edit screen
public function do_MetaBox( $post, $Box )
{
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_so_25936506' );
foreach( $Box['args'] as $field => $value )
$this->print_field( $field, $value, $post->ID );
}
public function print_field( $field, $value, $post_id )
{
$post_Meta = get_post_meta( $post_id, $field, true);
$selected = ($post_Meta) ? $post_Meta : false;
$users_dd = wp_dropdown_users(array(
'name' => 'author',
'echo'=>false,
'name'=>$field,
'show_option_none'=>'Select winners'
));
printf(
'<label>%s: </label>%s <small>%s</small><br/>',
$value['title'],
$users_dd,
$value['desc']
);
}
public function save( $post_id, $post_object )
{
// Verify auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Security
if (
!isset( $_POST['noncename_so_25936506'] )
|| !wp_verify_nonce( $_POST['noncename_so_25936506'], plugin_basename( __FILE__ ) )
)
return;
if ( $this->cpt !== $post_object->post_type )
return;
// Process post data
foreach( array('_winner_gold','_winner_silver','_winner_bronze') as $field )
{
if ( isset( $_POST[$field] ) )
update_post_Meta( $post_id, $field, $_POST[$field] );
else
delete_post_Meta( $post_id, $field );
}
}
}
new SO_25936506();
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|