使用mycred wordpress插件在个人资料图片上指向

时间:2019-02-04 15:56:16

标签: wordpress

当用户更新个人资料图片但我的代码无法正常工作时,我试图向用户奖励积分。尽管我使用的代码和方式与mycred插件团队在下面的链接中所述

https://www.mycred.me/tutorials/how-to-make-your-custom-hook/

我也在输入我尝试输入的代码,请检查我是否使用了错误的代码或错误的方式 我正在使用下面的代码行来获取用户是否上传图片

if ( empty( $_POST[’uploadedfile’] ) ) return;

我正在尝试使用以下代码

add_filter( 'mycred_setup_hooks', 'register_my_custom_hook' );
function register_my_custom_hook( $installed )
{
    $installed['complete_profile'] = array(
        'title'       => __( 'Profile Image', 'textdomain' ),
        'description' => __( 'add 1000 points on adding profile image', 'textdomain' ),
        'callback'    => array( 'my_custom_hook_class' )
    );
    return $installed;
}

class my_custom_hook_class extends myCRED_Hook {

    /**
     * Construct
     */
    function __construct( $hook_prefs, $type ) {
        parent::__construct( array(
            'id'       => 'complete_profile',
            'defaults' => array(
                'creds'   => 1,
                'log'     => '%plural% for completing your profile'
            )
        ), $hook_prefs, $type );
    }

    /**
     * Hook into WordPress
     */
    public function run() {
        // Since we are running a single instance, we do not need to check
        // if points are set to zero (disable). myCRED will check if this
        // hook has been enabled before calling this method so no need to check
        // that either.
        add_action( 'personal_options_update',  array( $this, 'profile_update' ) );
        add_action( 'edit_user_profile_update', array( $this, 'profile_update' ) );
    }

    /**
     * Check if the user qualifies for points
     */
    public function profile_update( $user_id ) {
        // Check if user is excluded (required)
        if ( $this->core->exclude_user( $user_id ) ) return;

        // Check to see if user has filled in their first and last name
        if ( empty( $_POST['uploadedfile'] ) ) return;

        // Make sure this is a unique event
        if ( $this->has_entry( 'completing_profile', '', $user_id ) ) return;

        // Execute
        $this->core->add_creds(
            'completing_profile',
            $user_id,
            $this->prefs['creds'],
            $this->prefs['log'],
            0,
            '',
            $m
        );
    }



    /**
     * Add Settings
     */
     public function preferences() {
        // Our settings are available under $this->prefs
        $prefs = $this->prefs; ?>

<!-- First we set the amount -->
<label class="subheader"><?php echo $this->core->plural(); ?></label>
<ol>
    <li>
        <div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo esc_attr( $prefs['creds'] ); ?>" size="8" /></div>
    </li>
</ol>
<!-- Then the log template -->
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
    <li>
        <div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="long" /></div>
    </li>
</ol>
<?php
    }

    /**
     * Sanitize Preferences
     */
    public function sanitise_preferences( $data ) {
        $new_data = $data;

        // Apply defaults if any field is left empty
        $new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
        $new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];

        return $new_data;
    }

}

0 个答案:

没有答案