I want to centralise the prices on our website, so that I can simply update one place, and once updated it propagates to all pages.
I don't want to use a plugin.
The idea I have is to create an array of prices, then print the prices using a function, then register shortcodes with that function.
$prices = array(
'price1' => '100',
'price2' => '150'
)
This function doesn't work, but the idea is:
function printPrice($price) {
return $prices->$price
}
Then I would register the shortcodes as follows:
add_shortcode('first_price', 'printPrice(price1)');
add_shortcode('second_price', 'printPrice(price2)');
Then I would use the shortcodes as follows in a page or post:
The first price is: [first_price]
The second price is: [second_price]
Which would render on the frontend as:
The first price is: 100
The second price is: 150
If there is a better and clearer, more WordPress specific way of doing this, that doesn't involve any of the above steps, but solves the same problem, please advise. The solution doesn't need to look like what I have suggested above.
If this solution is viable, please assist me in fixing the above function.
Edit:
I found the following function which works exactly as I want, but is very cumbersome, as I have to create a function for each price:
function function_price1($atts, $content = null) {
return '100';
}
add_shortcode('price1', 'function_price1');
function function_price2($atts, $content = null) {
return '150';
}
add_shortcode('price2', 'function_price2');
After reading https://developer.wordpress.org/apis/handbook/shortcode/ I then tried:
// [price price1="100"]
function price_func( $atts ) {
$a = shortcode_atts( array(
'price1' => '100',
'price2' => '150',
), $atts );
return "{$a['price1']}";
}
add_shortcode( 'price1', 'price1_func' );
but this allows the user to change the price with [price price1='200'], which is not what I want.
I want the prices to be fixed, and have the user choose from a predefined list of options. Example, like this, which is shown on the shortcode API page:
[ gallery id="123" size="medium" ]
Nowhere on the shortcode API page, does it show you how to setup that shortcode, where the user can only choose from, presumably, small, medium and large.
On the page, if I use the following, the page doesn't load:
[price key="price1"]
SallyCJ suggested: But actually, a simple callback could start with function my_shortcode_callback( $atts ) where $atts will contain the shortcode attributes like key if it's specified when adding the shortcode - e.g. [print-price key="price1"] has that attribute, but [print-price] does not have it. So your callback would only need to return the price based on the value of $atts['key'], if it's set.
I do not understand this response unfortunately. I don't know how to take this, and turn it into working code.
key
as in[print-price key="price1"]
to get the specific price only. But once again, please read the linked documentation and try to come up with your own code first - if you got stuck, then you can come back here to edit your post accordingly or just post a new question, but be as detailed as possible with your code included.