$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - Shortcodes in billing fields doesn't work|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - Shortcodes in billing fields doesn't work

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far