I am building a proprietary LMS(learning management system), and am attempting to add a simple bookmarking function that saves the $post->ID
for the last page visited within my "course" CPT(custom post type). It seems like this should be simply a matter of saving the post id for the current page as a custom meta data value for the user, but without fail the update_user_meta()
function saves the the post id for a post that is at the same level in the hierarchy that has been created for this "course" CPT.
Here's the code:
function nb_course_post_actions(){
global $current_user, $post;
echo "I'm about to give up! Post type is: {$post->post_type}.";
if( $post->post_type === 'course' ){
$sid = $current_user->ID;
$bookmark_id = $post->ID;
print( $bookmark_id );
if( update_user_meta( $sid, 'course_bookmarks', $bookmark_id ) === FALSE ){
echo "Failed to update metadata";
} else {
echo "Metadata was updated: ";
if ( get_user_meta($sid, 'course_bookmarks', true ) !== $bookmark_id )
wp_die('An error occurred');
}
}
}
add_action( 'wp_footer', 'nb_course_post_actions' );
So here's what is suppose to happen: the logged-in user visits a page within the course CPT, on each new page load the additional action of recording the current post id is added to the user's meta data, so that when a user leave and comes back at a later date in time, they can click on a link that takes them to the page they were last on within the "course" CPT.
The problem is that update_user_meta()
inevitably records the wrong post_id and the function goes to the wp_die()
function everytime. What's stranger is that it seems to have some intelligence to be saving an id that is relative to the original post, such as a sibling of the same CPT nested at the same level hierarchically, when there is no other information being sent or no other information that should be interfering with the metadata in question. The meta_key "course_bookmark" does not exist anywhere else in the code. For all I know, the update_user_meta()
function doesn't even know that it is saving a POST id as the only thing that it is receiving is a NUMERIC value. It could be saving someone's age or zip code for all that the code should known, it's just a numeric value with no other references to that fact that it is a post id.
It shouldn't know that that this is a post id, and it shouldn't know that it has any relationship to other post_ids that happen to be the same CPT, the same hierarchical level, and that is a published ID, not a revision.
My original function was much more elaborate and I have moved this code all over in my plugin, only realize that the issue does in fact appear to be with wordpress's update_user_meta()
function, and not the position in which I am calling it or anything else for that matter. The right ID is being sent to the function, but the wrong ID is being recorded (I checked the recorded value in PHPMyAdmin) and returned. But why?
UPDATE:
If you follow the comments for this post and Pieter's suggestions, this is where I've gotten to:
For some reason it appears that this bug seemed to be directly related to the <link rel='next' href='path\to\next\post\' />
which WordPress generates in the <head>
section of the html. I'm assuming this is placed there to help with SEO. I am not running any SEO-related plugins on this particular site, but there is something either in my code (not likely, as I'm not that sophisticated) or the theme that I'm using (Pinnacle by Kadence Themes - wonderful theme, by the way) that is taking that information and causing the next page to run via a $_GET
command according to my browser's network monitor. As an interim solution, I'm following the suggestion provided in this post by Hamergil and have added this block of code to my LMS plugin:
add_filter( 'index_rel_link', 'disable_stuff' );
add_filter( 'parent_post_rel_link', 'disable_stuff' );
add_filter( 'start_post_rel_link', 'disable_stuff' );
add_filter( 'previous_post_rel_link', 'disable_stuff' );
add_filter( 'next_post_rel_link', 'disable_stuff' );
function disable_stuff( $data ) {
return false;
}
Not sure if this is the best way to approach this or not, so I'm still leaving this open as unanswered.
update_user_meta()
function all together and doing a direct call to the$wpdb::update()
method. Very frustrating. I'm almost ready to create a new database table to see if the behavior still exists.