woocommerce offtopic - Shortcodes in billing fields doesn't work

admin2025-06-06  10

WooCommerce: 3.5.1 WordPress: 4.9.8

I want to set automaticly billing fields with data from GeoIP shortcodes, but it doesn't work.

add_filter( 'woocommerce_checkout_fields', 'set_checkout_field_input_value_default' );

function set_checkout_field_input_value_default($fields) {
    $fields['billing']['billing_city']['default'] = '[geoip_detect2 property="city"]';
    $fields['billing']['billing_state']['default'] = '[geoip_detect2 property="mostSpecificSubdivision"]';
    return $fields;
}

This solution didn't help: Shortcode not working inside html input

Output

<input type="text" class="input-text " name="billing_city" id="billing_city" placeholder="" value="[geoip_detect2 property=&quot;city&quot;]" autocomplete="address-level2">

WooCommerce: 3.5.1 WordPress: 4.9.8

I want to set automaticly billing fields with data from GeoIP shortcodes, but it doesn't work.

add_filter( 'woocommerce_checkout_fields', 'set_checkout_field_input_value_default' );

function set_checkout_field_input_value_default($fields) {
    $fields['billing']['billing_city']['default'] = '[geoip_detect2 property="city"]';
    $fields['billing']['billing_state']['default'] = '[geoip_detect2 property="mostSpecificSubdivision"]';
    return $fields;
}

This solution didn't help: Shortcode not working inside html input

Output

<input type="text" class="input-text " name="billing_city" id="billing_city" placeholder="" value="[geoip_detect2 property=&quot;city&quot;]" autocomplete="address-level2">
Share Improve this question asked Nov 22, 2018 at 15:21 Viking777Viking777 31 bronze badge 1
  • 1 You can use do_shortcode() - e.g. do_shortcode( '[geoip_detect2 property="city"]' ), instead of adding the shortcodes "as-is" to the $fields array. – Sally CJ Commented Nov 22, 2018 at 15:35
Add a comment  | 

1 Answer 1

Reset to default 0

The "official" answer is that you need to use do_shortcode() to process the shortcode as you can't just simply apply a shortcode within the code. Here's your original code with the "official" do_shortcode() way:

 add_filter( 'woocommerce_checkout_fields', 'set_checkout_field_input_value_default' );

function set_checkout_field_input_value_default($fields) {
    $fields['billing']['billing_city']['default'] = do_shortcode( '[geoip_detect2 property="city"]' );
    $fields['billing']['billing_state']['default'] = do_shortcode( '[geoip_detect2 property="mostSpecificSubdivision"]' );
    return $fields;
}

While that approach will parse the shortcode, I personally would suggest that you NOT use do_shortcode() because it has to run through a fairly extensive regex to process. It's better to find the actual callback function for the shortcode in question and use that directly. Yes, sometimes that is difficult, and is tricky in some situations. But thankfully, there's a good article and a better solution at this link.

So here's how to do it the J.D. Grimes way (as discussed in the article, and including his utility function along with your code snippet revised to use it):

/**
 * Call a shortcode function by tag name.
 *
 * @author J.D. Grimes
 * @link https://codesymphony.co/dont-do_shortcode/
 *
 * @param string $tag     The shortcode whose function to call.
 * @param array  $atts    The attributes to pass to the shortcode function. Optional.
 * @param array  $content The shortcode's content. Default is null (none).
 *
 * @return string|bool False on failure, the result of the shortcode on success.
 */
function do_shortcode_func( $tag, array $atts = array(), $content = null ) {

    global $shortcode_tags;

    if ( ! isset( $shortcode_tags[ $tag ] ) )
        return false;

    return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
}

add_filter( 'woocommerce_checkout_fields', 'set_checkout_field_input_value_default' );

function set_checkout_field_input_value_default($fields) {
    $fields['billing']['billing_city']['default'] = do_shortcode_func( 'geoip_detect2', array( 'property' => "city" ) );
    $fields['billing']['billing_state']['default'] = do_shortcode_func( 'geoip_detect2', array( 'property' => "mostSpecificSubdivision" ) );
    return $fields;
}

Either way is "right" and should solve the issue (at least as far as the shortcode parsing goes). Hope this helps you.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749156974a316848.html

最新回复(0)