skip to Main Content

I’m trying to figure out if I can insert this piece of code into a wordpress shortcode in functions.php

<p id="<?php echo esc_attr( ( 'add-new-user' == $user ) ? 'wpua-upload-button' : 'wpua-upload-button-existing' ); ?>">
                <input name="wpua-file" id="<?php echo esc_attr( ( 'add-new-user' == $user ) ? 'wpua-file' : 'wpua-file-existing' ); ?>" type="file" />

                <button type="submit" class="button" id="<?php echo esc_attr( ( 'add-new-user' == $user ) ? 'wpua-upload' : 'wpua-upload-existing' ); ?>" name="submit" value="<?php esc_html_e( 'Upload', 'one-user-avatar' ); ?>">
                    <?php esc_html_e( 'Upload', 'one-user-avatar' ); ?>
                </button>
            </p>

The piece of code is part of a plugin, this is the file: https://github.com/onedesigns/one-user-avatar/blob/main/includes/class-wp-user-avatar.php#L253

Could some good Samaritan take a look and give me directions on how to move? Sorry, but I’m new to codes, I don’t have many skills and I’m a fan.

Edit: I modified the code in this way following the advice of the users that were given to me in the comments. The button is displayed correctly, but if I choose the image and click on load the saves are not carried out.

//CUSTOM SHORTCODE
function short_shortcode() {
 
 //Global
 global $current_user;
 
 $user = $current_user;
 
 ob_start();
?>
<form>
 <p id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-upload-button' : 'wpua-upload-button-existing'); ?>">
   <input name="wpua-file" id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-file' : 'wpua-file-existing'); ?>" type="file" />

   <button type="submit" class="button" id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-upload' : 'wpua-upload-existing'); ?>" name="submit" value="<?php esc_html_e('Upload', 'one-user-avatar'); ?>">
     <?php esc_html_e('Upload', 'one-user-avatar'); ?>
   </button>
 </p>
</form> 
<?php
 return ob_get_clean();
}
add_shortcode('short', 'short_shortcode');

2

Answers


  1. Chosen as BEST ANSWER

    I was able to find a solution to the problem. For anyone interested here it is below. What I did was add some variables to the global found from https://github.com/onedesigns/one-user-avatar/blob/main/includes/class-wp-user-avatar-shortcode.php as suggested by the author of the plugin. (Some may not be needed, but I have included them as I will be extending the shortcode functionality in the future).

    After that, I introduced the following function: do_action( 'wpua_update', $valid_user->ID ); (always taken from the file I linked above).

    Finally I added the code I was interested in the form tags and in ob_start (); - return ob_get_clean();

    Furthermore, in the html code taken by the plugin it was necessary to add a few lines of php for the form to save everything. <? php wp_nonce_field ('update-user_'. $ user-> ID); ?> <? php submit_button (__ ('Update Profile', 'one-user-avatar')); ?>

    Here is the final working code

    //SHORTCODE UPLOAD BUTTON ONE-USER-AVATAR
    function short_shortcode() {
        
    //Global
    global   $wpua_force_file_uploader, 
             $show_avatars, 
             $wpua_shortcode, 
             $wp_user_avatar, 
             $all_sizes, 
             $blog_id, 
             $post, 
             $wpdb, 
             $current_user, 
             $wp_user_avatar, 
             $wpua_allow_upload, 
             $wpua_edit_avatar, 
             $wpua_functions;   
    
    //Default user is current user
    $user = $current_user;
    
    //Function
    do_action( 'wpua_update', $user->ID );
    
      ob_start();
    ?>
        <form id="wpua-edit-<?php echo esc_attr( $user->ID ); ?>" class="wpua-edit" action="" method="post" enctype="multipart/form-data">
            <p id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-upload-button' : 'wpua-upload-button-existing'); ?>">
             <input name="wpua-file" id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-file' : 'wpua-file-existing'); ?>" type="file" />
             <button type="submit" class="button" id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-upload' : 'wpua-upload-existing'); ?>" name="submit" value="<?php esc_html_e('Upload', 'one-user-avatar'); ?>">
             <?php esc_html_e('Upload', 'one-user-avatar'); ?></button>
            </p>
            <?php wp_nonce_field( 'update-user_' . $user->ID ); ?>
            <?php submit_button( __( 'Update Profile', 'one-user-avatar' ) ); ?>
        </form>
            
    <?php
      return ob_get_clean();
    }
    add_shortcode('short', 'short_shortcode');
    

  2. You need to capture and return the output. Here is how you can do that:

    <?php
    
    function short_shortcode() {
      ob_start();
    ?>
      <p id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-upload-button' : 'wpua-upload-button-existing'); ?>">
        <input name="wpua-file" id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-file' : 'wpua-file-existing'); ?>" type="file" />
    
        <button type="submit" class="button" id="<?php echo esc_attr(('add-new-user' == $user) ? 'wpua-upload' : 'wpua-upload-existing'); ?>" name="submit" value="<?php esc_html_e('Upload', 'one-user-avatar'); ?>">
          <?php esc_html_e('Upload', 'one-user-avatar'); ?>
        </button>
      </p>
    <?php
      return ob_get_clean();
    }
    add_shortcode('short', 'short_shortcode');
    

    More info here: https://codex.wordpress.org/Shortcode_API#Output

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search