• Resolved redmi87

    (@redmi87)


    Hi! How can I show photos uploading custom fields; I want to show it through shortcode ; and that it be displayed in my custom template through shortcode meta key

    function mostrar_foto_personalizada_usuario_shortcode() {
    ob_start();
    $user_id = get_current_user_id();
    $photo_url = get_user_meta($user_id, ‘photo’, true);
    if (!empty($photo_url)) {
    echo ‘‘;
    } else {
    echo ‘No se ha cargado ninguna imagen en este campo.’;
    }
    return ob_get_clean();
    }
    add_shortcode(‘mostrar_foto_personalizada_usuario’, ‘mostrar_foto_personalizada_usuario_shortcode’);

    used this but it doesn’t work

Viewing 5 replies - 1 through 5 (of 5 total)
  • missveronica

    (@missveronicatv)

    @redmi87

    You will probably only get the photo file name by the get_user_meta function call.

    Thread Starter redmi87

    (@redmi87)

    function um_user_img_shortcode($atts) {
        if (!class_exists('UM') || !is_user_logged_in()) {
            return '';
        }
    
        $atts = shortcode_atts(array(
            'user_id' => get_current_user_id(),
            'meta_key' => '',
        ), $atts);
    
        if (empty($atts['meta_key'])) {
            return '';
        }
    
        $user_id = absint($atts['user_id']);
        $meta_key = sanitize_key($atts['meta_key']);
    
        $image_name = get_user_meta($user_id, $meta_key, true);
        if (empty($image_name)) {
            return '';
        }
    
        $uploads_dir = wp_upload_dir();
        $image_url = $uploads_dir['baseurl'] . '/' . $image_name;
        if (empty($image_url)) {
            return '';
        }
    
        $output = '<img src="' . esc_url($image_url) . '" alt="Imagen" />';
        return $output;
    }
    add_shortcode('um_user_img', 'um_user_img_shortcode');
    Thread Starter redmi87

    (@redmi87)

    I am using this but the image is not displayed can you help me

    missveronica

    (@missveronicatv)

    @redmi87

    If you update the line where your Image URL is created
    with this code your code snippet will work.

    $image_url = $uploads_dir['baseurl'] . '/ultimatemember/' . $user_id . '/' . $image_name;

    Thread Starter redmi87

    (@redmi87)

    thanks it works

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘show photos upload custom fields via shortcode’ is closed to new replies.