Skip Navigation

[Resolved] Need Help to Trigger a Function when editing the post.

This support ticket is created 3 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 3 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#2142005

Hi,
I need help to run my custom function just before a custom post is gonna be edited using the Cred Post edit form. We have a post type called "client" and we are fetching the posts data from an API. So currently below custom function triggers and insert or update all posts when a user logs in to website. as soon as the user login, API Function automatically updates/insert all posts data of the api into existing client posts or insert new posts to database if new posts found in API.

So we are actually looking to run this function, not only during the login, but in case user is already logged in and he/she closes the site but after some times when he opens the website homepage then that api function should trigger again. And Also when user wants to edit any post using the edit post link, then as soon as he clicks in Edit Post link, before displaying the post edit form, this function should run again to update the post data and then display in the edit form.

So I am wondering if toolset team has any action hook that can execute the below function. its an urgent task. So if any of you can respond, it would be great.


add_action( 'update_client_list', 'get_clients_from_api' );
add_action( 'wp_ajax_nopriv_get_clients_from_api', 'get_clients_from_api' );
add_action( 'wp_ajax_get_clients_from_api', 'get_clients_from_api' );
function get_clients_from_api() {

  $current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
  $clients = [];

  // Should return an array of objects
  $resultsJson = wp_remote_retrieve_body( wp_remote_get('<em><u>hidden link</u></em>') );

  // turn it into a PHP array from JSON string
  $results = json_decode( $resultsJson );
  
  // Either the API is down or something else spooky happened. Just be done.
  if( ! is_array( $results ) || empty( $results ) ){
    return false;
  }
    global $wpdb;

  foreach( $results as $client ){
    
     // $client_slug = slugify( $client["post_title"] );
      $rdata = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '".$client->{"wpcf-patient-id"}."'", ARRAY_A );
      //$existing_client = get_page_by_path( $client_slug, 'OBJECT', 'client' );
   
      $existing_client=null;
      if(is_array($rdata) && count($rdata)>0){
          $existing_client=$rdata[0]['post_id'];
      }
      
    if( $existing_client == null  ){
      
      $inserted_client = wp_insert_post( [
        'post_name' => $client->post_title,
        'post_title' => $client->post_title,
        'post_type' => 'client',
        'post_status' => 'pending'
      ] );
       
      if( is_wp_error( $inserted_client ) || $inserted_client === 0 ) {
        // die('Could not insert client: ' . $client_slug);
        // error_log( 'Could not insert client: ' . $client_slug );
        continue;
      }

      foreach( $client as $key => $value ) {
       // update_field( $key, $client->$name, $inserted_client );
          add_post_meta($inserted_client,$key,$value);
      }
      
    } else {
      
      $existing_client_id = $existing_client;
      $exisiting_client_timestamp = get_post_meta($existing_client_id,'updated_at',true);
        
        
        $d1 = new DateTime($client->updated_at);
        $d2 = new DateTime($exisiting_client_timestamp);
        
       
      if( $d1 >= $d2){

          
        foreach( $client as $key => $value ){
         // update_field( $name, $client->$name, $existing_client_id);
            
            update_post_meta($existing_client_id,$key,$value);
        }

      }

    }

  }
  
  $current_page = $current_page + 1;
  wp_remote_post( admin_url('admin-ajax.php?action=get_clients_from_api'), [
    'blocking' => false,
    'sslverify' => false, // we are sending this to ourselves, so trust it.
    'body' => [
      'current_page' => $current_page
    ]
  ] );
  
}

WP_CLI::add_command('jst-update-data', 'get_clients_from_api');

#2142211

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

Thank you for getting in touch. Reading through your thread, the only hook that we have that can be used on the frontend edit form that may work for your case is the one below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

This hook fires after the user has submitted the form but before any post data has been saved to the database.

Please let me know if this was able to help. Also the link above contains all the other hooks that are available for our Toolset Form.

Thanks,
Shane

#2142215

Hi,
No, that hook is not gonna work, becuase our idea is to trigger my above function before the edit form displays the available data, because the above function actually connects with an api and read the api data and after reading the api, if there is an updated data available in the api then it updates back to posts in wordpress, So if you don't have any action hook from toolset then maybe you can suggest if there is any action hook that can trigger the function when a user reload or revisit the homepage. Like for example, I was logged in to site but then i closed my site and moved to somewhere, but once again after sometimes, I opened the site again and when I visit or redirected to homepage then this function should be fired to fetch the api data and auto update/insert the data.

#2142225

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

While i'm not aware of a specific wordpress hook, you can have a look at the list of available action hooks that wordpress has available.
https://codex.wordpress.org/Plugin_API/Action_Reference

Perhaps there is one here that may be able to assist you.

Thanks,
Shane