You can use a variable to define a meta box title, however you cannot use a variable in translation functions like __(). Translations are pre-defined and thus cannot know what a variable value might be.
Another possible issue with variable titles is: will that variable’s value be available when the meta box is registered? If you get the value from the DB, it ought to be. However, in your example the WP_User object ($user) is not defined, so you cannot access author meta based on it. The meta boxes action does pass the current WP_Post object, so the post’s author can be retrieved, it’s just that you’ve not done so π
If you need the variable value to be translatable, you’ll need to obtain it through other means, you cannot use __() and the gettext translation scheme. There are possibly other translation APIs you access to get random translations, but using them will likely slow down your page load time.
ok, there is a way to print that variable as label of the field?
or anyway just printing the variable somewhere near the field, above is better
Ok I changed the experiment, why the following isnt working?
function prezzo_primo_meta_box_render( $post ){
// Get the number and display it in a numeric field
$number = get_post_meta( $post->ID, "function_prezzo_primo", true );
echo'<label>esc_attr( get_the_author_meta( '1_per_hol', $user->ID ) )</label>' . '<div> <input type="number" name="function_prezzo_primo" value="'.$number.'" placeholder="prezzo primo periodo" /></div>';
}
I think THIS is the piece of code I have to “transplant” near the field of the meta box:
esc_attr( get_the_author_meta( '1_per_hol', $user->ID ) )
Or in the place of the title of the meta box here:
add_action( 'add_meta_boxes_post', "prezzo_primo_add_meta_box" );
function prezzo_primo_add_meta_box()
$periodo_listino= (get_the_author_meta( '1_per_hol', $user->ID ) );
add_meta_box(
"prezzo_primo_meta_box", // An ID for the Meta Box. Make it unique to your plugin
__( "$periodo_listino", 'textdomain' ), // The title for your Meta Box
"prezzo_primo_meta_box_render", // A callback function to fill the Meta Box with the desired UI in the editor
"post", // The screen name - in this case the name of our custom post type
"side" // The context or placement for the meta box. Choose "normal", "side" or "advanced"
);
}
any idea?
If you want it to be translatable via __(), you simply cannot use a variable where ever you place it. The system cannot accommodate variable values. Untranslated, you lots of options.
One reason prezzo_primo_meta_box_render() isn’t working is because $user is undefined as you have it. You need to get the author’s ID from the passed $post object. Then you may get their ‘1_per_hol’ meta value.
The other problem is you have your PHP code declared as a static string. The PHP part’s return value needs to be concatenated:
'<label>'. esc_attr( get_the_author_meta( '1_per_hol', $post->post_author ) ) .'</label>'
This is working, as <label>. I wonder if I can display the same function as placeholder
$number = get_post_meta( $post->ID, "function_prezzo_secondo", true );
echo '<label>'. esc_attr( get_the_author_meta( '2_periodo', $post->post_author ) ) .'</label>' . '<div> <input type="number" name="function_prezzo_secondo" value="'.$number.'" placeholder="prezzo secondo periodo" /></div>';
}
Concatenate the PHP return value in the right place:
echo '<div> <input type="number" name="function_prezzo_secondo" value="'.$number.'" placeholder="'. esc_attr( get_the_author_meta( '2_periodo', $post->post_author ) ) .'" /></div>';
I displayed both the value content as label and placeholder, I think I’ll keep just the placeholder. There is a little problem. I see these values only when I edit an already created post, when I want to edit a new post I see just the fields, but I cant see any label or placeholder. This behaviour force me to create first a post and work on all the other fields, than save and then re-edit the same post only for price fields. Is there a sulution?
Sorry, it’s the normal behaviour. The fields dont show something until I set an author for the post
Yeah, WP cannot know what author meta to use until the post author is assigned.
Will the post author eventually be the current user and not yourself? Your author meta code could first try to get author meta. If it fails due to no assigned author, it likely means it’s a new post and you could instead get the current user’s meta data.
If you will always create posts and assign another author, there’s no help for it. You need to assign an author to get the meta values.