Thanks for checking this question out: I am writing a WordPress theme and I want to use object Oriented programming to make the class reusable for several metaboxes I am creating. However, I get errors on trying to use variables to concatenate to my function names. Please help me save some hours, I have failed to find something in documentation and at PHP: Variable in a function name
class custom_metabox
{
private $cm_name_id;
private $cm_name;
private $cpt_name;
function __construct( $cm_name_id, $cm_name, $cpt_name ) {
$this->cm_name_id = $cm_name_id;
$this->cm_name = $cm_name;
$this->cpt_name = $cpt_name;
add_action( 'add_meta_boxes', array( $this, $cm_name_id . '_add_metabox_box' ) );
add_action( 'save_post', array( $this, $cm_name_id . '_box_save_postdata' ) );
}
/* Add metabox */
function $cm_name_id . _add_metabox_box() {
add_meta_box(
$cm_name_id . '_box_id', //ID for box
$cm_name, //Name for the box
$cm_name_id . '_box', //function for input
$cpt_name, //id for CPT
'normal', //location of input
'high' //priority
);
}
/* Prints the box content */
function $cm_name_id . _box( $post ) { ?>
<input class="widefat"
placeholder="option goes here"
name="<?php echo $cm_name_id; ?>_box_field"
id="<?php echo $cm_name_id; ?>_box_field"
value="<?php echo esc_html( get_post_meta( $post->ID, $cm_name_id . '_box_meta_value_key', true ) ); ?>" />
<?php }
/* Saves the value for the box content */
function $cm_name_id . _box_save_postdata( $post_id ) {
if ( array_key_exists('client_box_field', $_POST ) ) {
update_post_meta( $post_id, 'client_box_meta_value_key', $_POST['client_box_field'] );
}
}
}
$this->cm_nameon the first two lines of your constructor. I think the first line should be$this->cm_name_id = $cm_name_idinstead of only$this->cm_name.. Also, why you do this? Woudln't it be simpler to always ahve the same name of the function, and then pass the parameters in as normal?$cm_name_idand all those variables are never defined. So your class variabels also haven't any values.