Copy file from Profile to new site

I am using BuddyPress on my site. I have a logo upload field. I have a custom function that copies profile information into my new site. New site is using Divi theme. I am copying the filename but not the actual file. How can I copy the actual file to the new site so it attaches to the image?

  • Adam Czajczyk

    Hello Buddy

    I hope you're well today and thank you for your question!

    I'm not quite sure if I'm following you on this. I understand that there's file upload field in your BuddyPress where users can upload some image/logo to their profiles and that is working fine, correct?

    Then, there's this custom code that's supposed to copy data from profile to "somewhere" on your site and it is working fine except image part. Can you explain that a bit more please? I mean:

    - where does it copy information from BP profile to?
    - what do you mean by "so it attaches to the image" - should that be added to some post content, set as featured image, copied to some custom profile?
    - it would also be great if you could share that custom code of yours so we could take a look at it; you can put it e.g. on pastebin.com and share a link here

    Kind regards,
    Adam

  • Buddy

    Hi there,

    Yes. BuddyPress image upload is working just fine. But my goal is to make their logo automatically be used as the Logo in their new Divi site.

    The Divi theme options has a logo upload field. I am trying to auto-populate this field. In the user copy function, I am also transferring their social profile information into the new divi theme but getting the BuddyPress profile data and inserting that into the unserialized JSON info from the Divi options from the database and updating the database. This works fine. I can change the logo field to be the correct image path etc, however, I just need to actually move the image somewhere. I am not quite sure where or how to hook that up to the textual logo field so divi options thinks it was organizally uploaded via the Divi options.

  • Adam Czajczyk

    Hello Buddy

    Thanks for response and explanation.

    If you're able to fetch the image URL why not just modify that "on the fly" on users' sites using Divi's "et_html_logo_container" filter? Since Divi 3.1 you can do something like that:

    add_filter( 'et_html_logo_container', 'et_custom_logo' );
    function et_custom_logo( $logo_container ) {
    
    	$logo_container = '<img src="URL_OF_LOGO_IMAGE">';
    
    	return $logo_container;
    }

    That could be slightly modified to dynamically fetch logo URL:

    $logo_container = my_fetch_logo_function();

    where "my_fetch_logo_function()" would be your custom code that reads the logo URL from user's profile and returns html img tag (or even more tags to nicely position it etc) to display it.

    Such code could be added as an MU plugin and would be automatically executed on all sites that use Divi theme on your Multisite. Did you consider going that direction?

    Best regards,
    Adam

        • Buddy

          Hi Adam,

          I am successfully fetching the url to the image I want to use.
          It is not running the et_custom_logo function. Here is my code:

          <?php
          /**
          Plugin Name: Customize Divi Logo
          */
          global $wpdb;
          $blog_id = get_current_blog_id();
          $owner_id = get_blog_option( $blog_id, 'blog_owner');
          $src_table    = $wpdb->base_prefix.'bp_xprofile_data';
          $broker_logo = $wpdb->get_var('SELECT value FROM ' . $src_table . ' WHERE field_id = 10 AND user_id = '.$owner_id);
          
          // echo $user_id;
          if ( $broker_logo == '') {
              $broker_logo = "UPLOAD A LOGO IN YOUR PROFILE!";
          }else{
              // echo "broker logo set";
          }
          
          function et_custom_logo( $logo_container ) {
          	$logo_container = '<img src="'.network_site_url().$broker_logo.'">';
          
          	return $logo_container;
          }
          add_filter( 'et_html_logo_container', 'et_custom_logo');

          Divi theme is: 3.0.92

          • Buddy

            I am receiving this error multiple times in the my debug.log even though the sql is returning the correct result.

            [23-May-2018 17:07:53 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 for query SELECT value FROM aa_bp_xprofile_data WHERE field_id = 10 AND user_id = made by require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), include_once('/mu-plugins/customize_divi_logo.php')

            ****EDITED****
            Fixed by putting the SELECT statement in a $sql variable and using $wpdb->get_var( $sql );

        • Buddy

          I realized my Divi theme was 3.0.9 and not 3.1 where the filter is added. Now it runs. However, I still don't have it just yet. Here is my new code so far:

          <?php
          /**
          Plugin Name: Customize Divi Logo
          */
          global $wpdb;
          $blog_id = get_current_blog_id();
          $owner_id = get_blog_option( $blog_id, 'blog_owner');
          $src_table    = $wpdb->base_prefix.'bp_xprofile_data';
          $sql = 'SELECT value FROM <code>' . $src_table . '</code> WHERE <code>field_id</code> = "10" AND <code>user_id</code> = "'.$owner_id.'"';
          $broker_logo = $wpdb->get_var( $sql );
          
          // echo $user_id;
          if ( $broker_logo == '') {
              $broker_logo = "UPLOAD A LOGO IN YOUR PROFILE!";
          }else{
              // echo "broker logo set";
          }
          
          // Change logo for Divi templates
          function et_custom_logo( $logo_container ) {
              global $broker_logo;
              echo $logo_container;
              $logo_container  = '<div class="logo_container buddy">';
              $logo_container .= '<span class="logo_helper"></span>';
              $logo_container .= '<a href="https://testpost.agentassets.com/">';
              $logo_container .= '<img src="'.network_site_url().$broker_logo.'" alt="Site Logo" id="logo">';
          	$logo_container .= '</a>';
          	$logo_container .= '</div>';
              echo $logo_container;
          	// return $logo_container;
          }
          add_filter( 'et_html_logo_container', 'et_custom_logo');
        • Buddy

          Hi Adam,

          I managed to pretty much get it done except for the fact that for some reason, the logo gets overwritten by divi immediately on page load.

          I think I may need to somehow transfer the actual image to the new site when created so it's solid. Any reason you can think of why it ovewrites the image tag? It doesn't overwrite the other surrounding HTML that I alter (I did some tests). Here is a test page for you to see it in action. Open the page and view page source and compare the logos. Open view page source and reload it.

          • Adam Czajczyk

            Hello Buddy

            In your code above you're first "echoing" the $logo_container before doing any changes to it - that's printing out the original one then so it's not necessary.

            Then, near the bottom of the function you're again "echoing" the $logo_container instead of returning it. It's a filter and if you echo() you're printing to the source but then the original one is used anyway.

            Try removing the first "echo" line and replacing the second one with commented out "return" line and if the rest of the code is working that should do the trick.

            Kind regards,
            Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.