简体   繁体   中英

How to implement custom logic in WordPress?

I'm working on my website and I want to add two things.

  • A link when a user clicks on it then his/her user id and the current post id get and inserted in the database and the counter increases.

  • Let's suppose user logs in and opens the post and the link is like this: I am Interested (0) here 0 is the count value of the total number of unique logged in people who have clicked on this link. The second link will be like: I want to work (0) same logic applies here.**

And finally need to show the no of interested and working people for a specific post(custom post type: job).

There is a table in which each row has the title of the post (job) I can get the value of the post by WordPress native functions and then compare that with the post id in the database and then retrieving it status (noofinterested and noofworking) and then inject via AJAX using jQuery.

I have created the table for this purpose but I'm having difficulty:

  1. Getting POST ID and USER ID via jquery as for some reasons I cant modify the current custom post template as I'm using wp job manager for custom job posts.
  2. All I need is to find a way to get unique people to count on the specific posts

So far I thought of a way to append the two links via jQuery and the add click function to the link via jQuery and in that add ajax to get data and input to the PHP file which I have coded and in that PHP file it will insert into the table via the post values and then return response. And in the second page where I want to insert the status in the table row, I will use jQuery for this.

This is currently I'm having on hand.

PHP

<?php
global wpdb;
if(isset($_POST['if_intested'])&&isset($_POST['if_working'])&&isset($_POST['user_id'])&&isset($_POST['post_id'])){
  if($wpdb->insert('wp_custom_jobmanager', array ( "post_id" => $_POST['post_id'], "user_id" => $_POST['user_id'], "if_interested" => $_POST['if_interested'] , "if_working" => $_POST['if_working']))){
    return "Thanks your feedback has been saved";
  }else {
    return "Sorry something went wrong Please Try Logging in again and try again.";
  }
}
?>

jQuery

 jQuery("#link").click(function(){
    if(jQuery("body").hasClass("single")){
    jQuery('<p id="stauts-interested">interested 24</p><br><p id="status-work">Working 43</p>').appendTo(".job-manager-single-alert-link");
    }
});

Now the number of interested and working should be fetch from database and also get incremented for unique logged in users on click.

Why don't you just add a custom field to your Custom Post Type instead of using a custom table? This is almost a prime example of when it's appropriate to use it. Just add the current user id to an array using get_post_meta and update_post_meta on the WP Ajax hooks.

add_action( 'wp_ajax_increment_cpt_interests' );
add_action( 'wp_ajax_nopriv_increment_cpt_interests' );

function increment_cpt_interests(){
    extract( $_POST );

    // Get current interested people
    $interested_people = get_post_meta( $post_id, 'interested', true );
    $interested_user   = get_current_user_id();

    // If current user isn't already "interested"
    if( !in_array( $interested_user, $interested_people ){
        // Add them to the array of interested people
        update_post_meta( $post_id, 'interested_people', $interested_people[$interested_user] );
    }
}

Just pass the post ID, Ajax URL (and current user id if you want) using wp_localize_script (Read more here ) to the button you want clicked on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM