• Resolved shamshansudhar

    (@shamshansudhar)


    Hello There,

    We have Added custom fields using ACF and its showing up there in the Backend Post Editor. But we that that field to be set as BuddyPress form fields values like suppose.

    Here city is the field which exists in BuddyPress profile field and we want to set the custom field there when user creates new doc or edits etc.

    $myfield = xprofile_get_field_data( ‘city’, $bp->loggedin_user->id );
    Like below is the code we are using but its not working

    
    function run_when_post_published($post)
    {
        // your function code here
        global $wpdb;
          if(!wp_is_post_revision($post->ID) && $post->post_type == 'bp_doc') {
    		  global $bp;
    		$myfield = xprofile_get_field_data( ‘city’, $bp->loggedin_user->id );
    		  echo $myfield;
          add_post_meta($post->ID, 'city_meta', $myfield , true);
          }
    }
    
    // replace {custom_post_type_name} with the name of your post type
    add_action('new_to_publish', 'run_when_post_published');    
    add_action('draft_to_publish', 'run_when_post_published');    
    add_action('pending_to_publish', 'run_when_post_published');
    

    Please help it’s urgent

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author David Cavins

    (@dcavins)

    Hi @shamshansudhar-

    I would add the city post meta on the bp_docs_after_save or bp_docs_doc_saved hook:
    https://github.com/boonebgorges/buddypress-docs/blob/2.0.x/includes/query-builder.php#L613

    Then you can access the user data and if this is a new doc or not ($this->is_new_doc).

    Best,

    -David

    Thread Starter shamshansudhar

    (@shamshansudhar)

    Here is the complete code but still not working:

    function do_something_on_save( $new_status, $old_status, $post ) {
    $demo_field_get=xprofile_get_field_data( 2, bp_loggedin_user_id() );
    $field = get_field_object(‘sample12345’);
    //var_dump($field[“key”]); //returns ‘field_57cee83c43f92’

    update_field(
    $field[“key”],
    $demo_field_get,
    get_the_ID()
    );
    }
    add_action(‘bp_docs_after_save’,’do_something_on_save’);
    add_action(‘bp_docs_doc_saved’,’do_something_on_save’);

    Thread Starter shamshansudhar

    (@shamshansudhar)

    We are using ACF

    And when i update post from dashboard then it works with this code:

    
    function do_something_on_save( $new_status, $old_status, $post ) {
       	$demo_field_get=xprofile_get_field_data( 2, bp_loggedin_user_id() );
        $field = get_field_object('sample12345');
        //var_dump($field["key"]); //returns 'field_57cee83c43f92'
      
    	update_field(
            $field["key"],
            $demo_field_get,
    		get_the_ID()
        );
    }
    add_action( 'transition_post_status', 'do_something_on_save', 10, 3 );
    add_action('acf/save_post', 'do_something_on_save', 20 ,3);
    Plugin Author David Cavins

    (@dcavins)

    Check the link I posted above. Note that the arguments passed with the action don’t match those passed by transition_post_status. Your function will be like one of these:

    
    add_action( 'bp_docs_doc_saved', 'my_function' );
    // $bp_doc is an instance of BP_Docs_Query
    function my_function( $bp_doc ) {
    	// Do something with $bp_doc->doc_id maybe.
    	// Or maybe based on $bp_doc->is_new_doc
    }
    
    /**
     * @param int   $id   The ID of the recently saved doc.
     * @param array $args The passed and filtered parameters for the doc
     *                    that was just saved.
     */ 
     function my_other_function( $doc_id, $args ) {
    	// Do something with $doc_id.
    	// Or maybe dig around in $args. Who knows?
    }
    add_action( 'bp_docs_after_save', 'my_other_function', 10, 2 );
    

    -David

    • This reply was modified 7 years, 2 months ago by David Cavins.
    Plugin Author David Cavins

    (@dcavins)

    Also note that I recommend interacting with docs using the front end editor, not the back end editor.

    Thread Starter shamshansudhar

    (@shamshansudhar)

    Hello David,

    But i’m new to php and i really tried and below is the code but its still not working. I have even tried to debug the value of the doc_id its NULL

    
    add_action( 'bp_docs_doc_saved', 'my_function' );
    // $bp_doc is an instance of BP_Docs_Query
    function my_function( $bp_doc ) {
    	// Do something with $bp_doc->doc_id maybe.
    	// Or maybe based on $bp_doc->is_new_doc
    	$demo_field_get=xprofile_get_field_data( 2, bp_loggedin_user_id() );
        $field = get_field_object('sample12345');
        //var_dump($field["key"]); //returns 'field_57cee83c43f92'
      
    	/*update_field(
            $field["key"],
            $demo_field_get,
    		$bp_docs->doc_id
        );
    	
    }

    Can you please correct it and help me

    Thanks

    Thread Starter shamshansudhar

    (@shamshansudhar)

    Hello David,

    I also Found a Bug in Associated Item Taxonomy and it won’t work and always gives 404 error page when we try to view associated items.

    Thanks

    Thread Starter shamshansudhar

    (@shamshansudhar)

    Hey David,

    Can you please check the Associated Item Taxonomy issue. And you can check out using this url

    Plugin Author David Cavins

    (@dcavins)

    Hi @shamshansudhar-

    The associated item is used to more efficiently query who can access which docs. It’s not meant to be a public taxonomy, and will not create archive pages.

    -David

    Plugin Author David Cavins

    (@dcavins)

    Re your code snippet: I don’t use Advanced Custom Fields, so don’t know that the function part of that is correct. I can see that you’ve got a bad code-comment mark and a typo on a variable name, so the code is malformed. This might work, but I can’t test it:

    
    add_action( 'bp_docs_doc_saved', 'add_user_location_on_doc_save' );
    // $bp_doc is an instance of BP_Docs_Query
    function add_user_location_on_doc_save( $bp_doc ) {
      // 2 is the ID of the location xprofile field
      $location = xprofile_get_field_data( 2, bp_loggedin_user_id() );
      $field = get_field_object('sample12345');
      
      update_field(
        $field["key"],
        $location,
        $bp_doc->doc_id
      );
    }
    

    If you were working with post meta directly, it would look like this:

    
    add_action( 'bp_docs_doc_saved', 'add_user_location_on_doc_save' );
    // $bp_doc is an instance of BP_Docs_Query
    function add_user_location_on_doc_save( $bp_doc ) {
      // 2 is the ID of the location xprofile field
      $location = xprofile_get_field_data( 2, bp_loggedin_user_id() );
    
      update_post_meta( $bp_doc->doc_id, 'user_location', $location );
    }
    
    Thread Starter shamshansudhar

    (@shamshansudhar)

    Thanks it works

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to add Front End Custom Fields’ is closed to new replies.