• Resolved ojasya

    (@ojasya)


    I want to remove the currency symbol from return value by a shortcode

    I am using following code

    function oj_total( $atts ) {
    extract(
        shortcode_atts(
            array(
                'cat_id'=>235,            
            ), 
            $atts
        )
    );    
    $t = do_shortcode( '[give_totals cats="4" message="{total}"]', $ignore_html = false );  //returns $89,695.00
    $t = filter_var($t, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
    return $t;  //should return 89695.00 but returns 3689695.00
    }
    add_shortcode( 'oj_total', 'oj_total' );

    Value of $t should return 89695.00 but in my case returning 3689695.00

    i.e $ get replaced by 36

    Can you please help

    thanks

    • This topic was modified 3 years, 6 months ago by ojasya.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Rick Alday

    (@mrdaro)

    Hi @ojasya,

    Happy to help.

    The shortcode does not only output the amount, it outputs some wrapper HTML too.
    So
    do_shortcode( '[give_totals cats="4" message="{total}"]', $ignore_html = false );

    will actually output something like:

    <div class="give-totals-shortcode-wrap">$89,695.00</div>

    So you need some regex magic to strip out the div element:

    function oj_total( $atts ) {
    extract(
        shortcode_atts(
            array(
                'cat_id'=>235,            
            ), 
            $atts
        )
    );    
    $t = do_shortcode( '[give_totals cats="4" message="{total}"]', $ignore_html = $regex = '/(?s)(?<=<div\sclass="give-totals-shortcode-wrap">\n).*(?=<\/div>)/';
    preg_match($regex, $t, $matches);
    $t = filter_var($matches[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    return $t; 
    }
    add_shortcode( 'oj_total', 'oj_total' );

    IMHO, you can rewrite a custom and simpler version of the give_totals function: https://github.com/impress-org/givewp/blob/master/includes/shortcodes.php#L582

    Plugin Support Rick Alday

    (@mrdaro)

    @ojasya,

    Apparently the code I pasted lost its formatting. Try this instead:

    function oj_total( $atts ) {
    extract(
        shortcode_atts(
            array(
                'cat_id'=>235,            
            ), 
            $atts
        )
    );    
    $t = do_shortcode( '[give_totals cats="4" message="{total}"]', $ignore_html = false );
    $regex = '/(?s)(?<=<div\sclass="give-totals-shortcode-wrap">\n).*(?=<\/div>)/';
    preg_match($regex, $t, $matches);
    $t = filter_var($matches[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    return $t; 
    }
    add_shortcode( 'oj_total', 'oj_total' );
    Thread Starter ojasya

    (@ojasya)

    Thanks @mrdaro

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Replace a symbol in value returned by shortcode’ is closed to new replies.