bg_metabox
Imaginet bg metabox
class head_bg { public function __construct() { if ( is_admin() ) { add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); } } public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) ); add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 ); } public function add_metabox() { add_meta_box( 'caseBg', __( 'Background', 'text_domain' ), array( $this, 'render_bg_metabox' ), array( 'post', ' case-studies' ), 'side', 'default' ); } public function render_bg_metabox( $post ) { // Retrieve an existing value from the database. $case_ = get_post_meta( $post->ID, 'case_', true ); // Set default values. if( empty( $case_ ) ) $case_ = ''; // Form fields. echo '<table class="form-table">'; echo ' <tr>'; echo ' <th><label for="case_" class="case__label">' . __( 'Image URL', 'text_domain' ) . '</label></th>'; echo ' <td>'; echo ' <input type="url" id="case_" name="case_" class="case__field" placeholder="' . esc_attr__( 'Enter URL', 'text_domain' ) . '" value="' . esc_attr( $case_ ) . '">'; echo ' <p class="description">' . __( 'Upload the image in media gallery and copy the URL', 'text_domain' ) . '</p>'; echo ' </td>'; echo ' </tr>'; echo '</table>'; } public function save_metabox( $post_id, $post ) { // Sanitize user input. $case_new_ = isset( $_POST[ 'case_' ] ) ? esc_url( $_POST[ 'case_' ] ) : ''; // Update the meta field in the database. update_post_meta( $post_id, 'case_', $case_new_ ); } } new head_bg;