2

I am using wordpress with woocommerce and I am trying to manipulate a copy of myaccount.php The default code uses some variables of some sort that I am not familiar with nor have I been able to find documentation on. The variables in question are %1$s, %2$s and %s

<p class="myaccount_user">
<?php
printf(
    __( 'Hello <strong>%1$s</strong> (not %1$s? <a href="%2$s">Sign out</a>).', 'woocommerce' ) . ' ',
    $current_user->display_name,
    wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) )
);
?>

<?php
printf( __( 'From this page you can view your recent orders, manage your shipping and billing addresses and <a href="%s">edit your password and account details</a>.', 'woocommerce' ),
    wc_customer_edit_account_url()
);
?>
</p>

How can I identify the variables, what they represent and how to use them?

Thank you.

2

The %1$s and %2$s are standard "conversion specifications" (ie. placeholders) used with the printf() family of PHP functions (nothing to do with WordPress or WooCommerce). They are replaced with the argument values that are passed to the printf() function.

So, in the first example, %1$s is replaced with the value of $current_user->display_name (1st argument) and %2$s is replaced with the value returned from wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) (2nd argument).

The placeholder specifies how the value is displayed.

  • % indicates the start of the placeholder.

  • The number 1 that follows is a reference to which argument, either 1 or 2 in this example.

  • s indicates a string.

Further reference:
http://php.net/manual/en/function.sprintf.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.