-
I need to redefine the wc_price variable, or better yet, delete it altogether and register a new one instead.
-
Hello, I understand you want to redefine the
wc_price
variable in Rank Math. To redefine or delete existing variables and register new ones, you can use the provided filter hooks in Rank Math.Here is a general guide on how to redefine variables in Rank Math:
-
Redefine Existing Variables:
- You can modify the existing variables using the
rank_math/vars/replacements
filter. Here is an example snippet to update thewc_price
variable:
add_filter( 'rank_math/vars/replacements', function( $vars ) { // Modify the existing variables here return $vars; });
- You can modify the existing variables using the
-
Register New Variables:
- To register a new variable, you can use the
rank_math/vars/register_extra_replacements
action hook. Here is an example snippet to register a new custom variable:
add_action('rank_math/vars/register_extra_replacements', function () { rank_math_register_var_replacement( 'custom_variable_slug', array( 'name' => esc_html__('Custom variable name.', 'rank-math'), 'description' => esc_html__('Custom variable description.', 'rank-math'), 'variable' => 'custom_variable_slug', 'example' => 'custom_variable_callback()', ), 'custom_variable_callback' ); });
- To register a new variable, you can use the
Please replace
custom_variable_slug
,Custom variable name.
,Custom variable description.
, andcustom_variable_callback()
with your desired values and callback function.If you encounter any issues or need further assistance, feel free to ask. Thank you for reaching out with your query!
Sources:
Using Variables in the SEO Title and Description » Rank Math
Image SEO Options in Rank Math
How to Make Your Theme Rank Math Compatible » Rank MathCode in rank-math.php, i tried in functions.php. Filter not working
add_filter( ‘rank_math/vars/replacements’, function( $vars ) {
error_log(“Это сооррр debug.log”);
echo “help!”;
var_dump($vars);
if ( isset( $vars[‘wc_price’] ) ) {
$vars[‘wc_price’][‘callback’] = ‘get_product_price’;
}
error_log( ‘Hello World!’ );
return $vars;
} );
function get_product_price() {
global $product;
return ”; //
}Hello,
Thank you for contacting support.
You cannot delete this variable because it’s added directly in the plugin source code. If you need the output to be different or perform any transformation on the prices you can register a new one with a different name using the following filter: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables
All the modifications to the new variable should be done via the callback function that can be seen on the filter.
Here’s an example of using this function to get the price of a WooCommerce product dynamically and associate it with the variable
%price%
:add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'price', [ 'name' => esc_html__( 'Price', 'rank-math' ), 'description' => esc_html__( 'Output the price of the product', 'rank-math' ), 'variable' => 'price', 'example' => price_callback(), ], 'price_callback' ); }); function price_callback() { $product = wc_get_product(get_queried_object_id()); if( $product ) { $price_vat = wc_get_price_including_tax( $product ); $price = number_format($price_vat, 2); } return $price; }
Don’t hesitate to get in touch if you have any other questions.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.
-
The ticket ‘How do I redefine the wc_price variable’ is closed to new replies.