草庐IT

php - 在 WordPress 中有多个获奖者的奖励

coder 2024-04-17 原文

首先,我是 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');

有什么建议吗?提前致谢

最佳答案

无需创建自定义数据库表即可执行此操作。

有很好的插件来管理 CPT 和自定义字段,使这很容易,但这里是上面概述的小插件:

<?php
/**
 * Plugin Name: (SO) Awards, prizes and winners
 * Plugin URI:  http://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();

关于php - 在 WordPress 中有多个获奖者的奖励,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25936506/

有关php - 在 WordPress 中有多个获奖者的奖励的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  5. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  6. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

  7. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  8. ruby - ruby 中有 each_if 吗? - 2

    假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其

  9. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

  10. ruby-on-rails - before_filter 运行多个方法 - 2

    是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://

随机推荐