duannao1920 2015-03-06 17:57
浏览 32
已采纳

制作Wordpress作者按钮,需要缺少成分

So I'm trying to make a follow button in wordpress where users can press a button that allows them to follow an author they like. (No, I'm not interested in buddypress)

What I've done so far is use the code from Jon Masterson's Post Like System for WordPress http://hofmannsven.com/2013/laboratory/wordpress-post-like-system/ which pretty much adds a +1 update_post_meta when ever a user likes the post.

The problem I've ran into, is if you go all the way to the bottom you'll see that the like system uses update_post_meta to give a +1 to the count of the post meta, which is exactly what I need for the follow button. The problem is that I can't find a function update_author_meta to store the ++$author_follow_count for that specific author. If you know of a way for me to store and update the follow count for a specific author, or point me in the right direction, it would be greatly appreciated.

<?php
function follow_scripts() {
wp_localize_script( 'jk_like_post', 'ajax_var', array(
    'url' => admin_url( 'admin-ajax.php' ),
    'nonce' => wp_create_nonce( 'ajax-nonce' )
        )
    );
}
add_action( 'init', 'follow_scripts' );

/**
* (2) Save follow data
*/
add_action( 'wp_ajax_nopriv_jk-author-follow', 'jk_author_follow' );
add_action( 'wp_ajax_jk-author-follow', 'jk_author_follow' );
function jk_author_follow() {
   $nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
        die ( 'Nope!' );

if ( isset( $_POST['jk_author_follow'] ) ) {

    $author_id = $_POST['author_id']; // author id
    $author_follow_count = get_author_meta( $author_id,        "_author_follow_count", true ); // author follow count

    if ( function_exists ( 'wp_cache_post_change' ) ) { // invalidate WP Super Cache if exists
                $GLOBALS["super_cache_enabled"]=1;
                wp_cache_post_change( $post_id );
    }

    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id(); // current user
        $meta_AUTHORS = get_user_option( "_followed_authors", $user_id ); // author ids from user meta
        $meta_USERS = get_author_meta( $author_id, "_user_followed" ); //user ids from author meta
        $followed_AUTHORS = NULL; // setup array variable
        $followed_USERS = NULL; // setup array variable

        if ( count( $meta_Authors ) !=0 ) { // meta exists, set up values
            $followed_AUTHORS = $meta_AUTHORS;
        }

        if ( !is_array( $followed_AUTHORS ) )  // make array just in case
            $followed_AUTHORS = array();

        if ( count( $meta_USERS ) !=0 ) { // meta exists, set up values
            $followed_USERS = $meta_USERS[0];
        }

        if ( !is_array( $followed_USERS ) ) //make an array just in case
            $followed_USERS = array();

        $followed_AUTHORS['author-'.$author_id, "_user_followed"] = $author_id; // Add author id to user meta array
        $followed_USERS['user-'.$user_id] = $user_id; // add user id to author meta array
        $user_follows = count( $followed_AUTHORS ); // count user follows

 // *** Where the snag is ****
        if ( !AlreadyFollowed( $author_id ) ) { // follow the author
            update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta
            update_post_meta( $author_id, "_author_follow_count", ++$author_follow_count ); // +1 count author meta
            update_user_option( $user_id, "_followed_authors", $followed_Authors ); // Add author ID to user meta
            update_user_option( $author_id, "_author_follow_count", $user_follows ); // +1 count user meta
            echo $author_follow_count; // update count on front end
  • 写回答

1条回答 默认 最新

  • doutou7549 2015-03-06 18:08
    关注

    Posts have custom meta information as a feature and therefore you can add custom meta values. Author, as far as I know doesn't have a custom meta information.

    You could create a new table, and write custom update_author_meta function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路